C++ Program to Encrypt and Decrypt the String (Source Code Explained) (2024)

C++ program to encrypt and decrypt the string

In this example, you will learn simple C++ program to encrypt and decrypt the string using two different encryption algorithms i.e. Caesar Cypher and RSA.

Encryption/Decryption using Caesar Cypher Algorithm
Encryption/Decryption using RSA Algorithm

C++ Program to Encrypt and Decrypt the String (Source Code Explained) (1)

Encryption basically means encoding a particular message or information so that it can’t be read by other person and decryption is the process of decoding that message to make it readable.

Method 1: C++ program to encrypt and decrypt the string using Caesar Cypher Algorithm.

We have used a simple method of adding and subtracting a key value for encryption and decryption.

For encrypting a string, key-value ‘2’ is added to the ASCII value of the characters in the string.

Similarly, for decrypting a string, key-value ‘2’ is subtracted from the ASCII value of the characters.

Let’s take a deeper look at the source code:

//Simple C++ program to encrypt and decrypt a string#include <iostream>using namespace std;int main(){ int i, x; char str[100]; cout << "Please enter a string:\t"; cin >> str; cout << "\nPlease choose following options:\n"; cout << "1 = Encrypt the string.\n"; cout << "2 = Decrypt the string.\n"; cin >> x; //using switch case statements switch(x) { //first case for encrypting a string case 1: for(i = 0; (i < 100 && str[i] != '\0'); i++) str[i] = str[i] + 2; //the key for encryption is 3 that is added to ASCII value cout << "\nEncrypted string: " << str << endl; break; //second case for decrypting a string case 2: for(i = 0; (i < 100 && str[i] != '\0'); i++) str[i] = str[i] - 2; //the key for encryption is 3 that is subtracted to ASCII value cout << "\nDecrypted string: " << str << endl; break; default: cout << "\nInvalid Input !!!\n"; } return 0;}

Output

#Encrypting
C++ Program to Encrypt and Decrypt the String (Source Code Explained) (2)

#Decrypting
C++ Program to Encrypt and Decrypt the String (Source Code Explained) (3)

Explanation

In the above program, if you change the value of key then the encrypted value will be different.

If the user enters other value than 1 or 2 it will show Invalid Input.

Method 2: C++ program to encrypt and decrypt the string using RSA algorithm.

RSA algorithm is bit complex than Ceaser Cypher. It involves the use of public and private key, where the public key is known to all and used for encryption.

On the other hand, Private key is only used to decrypt the encrypted message.

Here is the deeper look at the steps of encryption algorithm:

1: Creating Keys

  • Select two large prime numbers x and y
  • Compute n = x * y
    where n is the modulus of private and the public key
  • Calculate totient function, ø (n) = (x − 1)(y − 1)
  • Choose an integer e such that e is coprime toø(n) and 1 < e < ø(n).
    e is the public key exponent used for encryption
  • Now choose d, so that d · e mod ø (n) = 1, i.e., >code>d is the multiplicative inverse of e in mod ø (n)

2: Encrypting Message

Messages are encrypted using the Public key generated and is known to all.

The public key is the function of bothe and n i.e.{e,n}.

If M is the message(plain text), then ciphertext

C = M ^ n( mod n )

3: Decrypting Message

The private key is the function of both d and n i.e {d,n}.

If C is the encrypted ciphertext, then the plain decrypted text M is

M = C ^ d ( mod n )

You might be wondering how to write a source code for this program.

So let’s look at the program.

//C++ program for encryption and decryption#include<iostream>#include<stdlib.h>#include<math.h>#include<string.h>using namespace std;int x, y, n, t, i, flag;long int e[50], d[50], temp[50], j;char en[50], m[50];char msg[100];int prime(long int); //function to check for prime numbervoid encryption_key();long int cd(long int);void encrypt();void decrypt();int main(){ cout << "\nENTER FIRST PRIME NUMBER\n"; cin >> x; //checking whether input is prime or not flag = prime(x); if(flag == 0) { cout << "\nINVALID INPUT\n"; exit(0); } cout << "\nENTER SECOND PRIME NUMBER\n"; cin >> y; flag = prime(y); if(flag == 0 || x == y) { cout << "\nINVALID INPUT\n"; exit(0); } cout << "\nENTER MESSAGE OR STRING TO ENCRYPT\n"; cin >> msg; for(i = 0; msg[i] != NULL; i++) m[i] = msg[i]; n = x * y; t = (x - 1) * (y - 1); encryption_key(); cout << "\nPOSSIBLE VALUES OF e AND d ARE\n"; for(i = 0; i < j - 1; i++) cout << "\n" << e[i] << "\t" << d[i]; encrypt(); decrypt(); return 0;} //end of the main programint prime(long int pr){ int i; j = sqrt(pr); for(i = 2; i <= j; i++) { if(pr % i == 0) return 0; } return 1; }//function to generate encryption keyvoid encryption_key(){ int k; k = 0; for(i = 2; i < t; i++) { if(t % i == 0) continue; flag = prime(i); if(flag == 1 && i != x && i != y) { e[k] = i; flag = cd(e[k]); if(flag > 0) { d[k] = flag; k++; } if(k == 99) break; } }}long int cd(long int a){ long int k = 1; while(1) { k = k + t; if(k % a == 0) return(k/a); }}//function to encrypt the messagevoid encrypt(){ long int pt, ct, key = e[0], k, len; i = 0; len = strlen(msg); while(i != len) { pt = m[i]; pt = pt - 96; k = 1; for(j = 0; j < key; j++) { k = k * pt; k = k % n; } temp[i] = k; ct= k + 96; en[i] = ct; i++; } en[i] = -1; cout << "\n\nTHE ENCRYPTED MESSAGE IS\n"; for(i=0; en[i] != -1; i++) cout << en[i];}//function to decrypt the messagevoid decrypt(){ long int pt, ct, key = d[0], k; i = 0; while(en[i] != -1) { ct = temp[i]; k = 1; for(j = 0; j < key; j++) { k = k * ct; k = k % n; } pt = k + 96; m[i] = pt; i++; } m[i] = -1; cout << "\n\nTHE DECRYPTED MESSAGE IS\n"; for(i = 0; m[i] != -1; i++) cout << m[i]; cout << endl;}

Output

C++ Program to Encrypt and Decrypt the String (Source Code Explained) (4)

This is all about encryption and decryption program in C++.

C++ Program to Encrypt and Decrypt the String (Source Code Explained) (2024)
Top Articles
Sam Bankman-Fried stole at least $10 billion, prosecutors say in fraud trial
NIPRNet and SIPRNet Control Room Furniture and AV Walls
The Blackening Showtimes Near Century Aurora And Xd
Lifebridge Healthstream
Jonathon Kinchen Net Worth
New Slayer Boss - The Araxyte
Crossed Eyes (Strabismus): Symptoms, Causes, and Diagnosis
Phenix Food Locker Weekly Ad
Craigslist Nj North Cars By Owner
House Share: What we learned living with strangers
U.S. Nuclear Weapons Complex: Y-12 and Oak Ridge National Laboratory…
Alaska: Lockruf der Wildnis
Wisconsin Women's Volleyball Team Leaked Pictures
2015 Honda Fit EX-L for sale - Seattle, WA - craigslist
Comics Valley In Hindi
Dumb Money, la recensione: Paul Dano e quel film biografico sul caso GameStop
Grayling Purnell Net Worth
Vintage Stock Edmond Ok
Our History
Reptile Expo Fayetteville Nc
Theater X Orange Heights Florida
Teen Vogue Video Series
Jeffers Funeral Home Obituaries Greeneville Tennessee
Rust Belt Revival Auctions
E32 Ultipro Desktop Version
Parkeren Emmen | Reserveren vanaf €9,25 per dag | Q-Park
Mdt Bus Tracker 27
Soul Eater Resonance Wavelength Tier List
Gunsmoke Tv Series Wiki
Movies - EPIC Theatres
Martins Point Patient Portal
Publix Coral Way And 147
Chilangos Hillsborough Nj
The Vélodrome d'Hiver (Vél d'Hiv) Roundup
Viewfinder Mangabuddy
How To Get Soul Reaper Knife In Critical Legends
Casamba Mobile Login
Seven Rotten Tomatoes
Best Restaurants West Bend
11 Best Hotels in Cologne (Köln), Germany in 2024 - My Germany Vacation
National Weather Service Richmond Va
Academic Notice and Subject to Dismissal
Ehc Workspace Login
Searsport Maine Tide Chart
Cch Staffnet
Motorcycles for Sale on Craigslist: The Ultimate Guide - First Republic Craigslist
Leland Westerlund
Mikayla Campinos Alive Or Dead
What Does the Death Card Mean in Tarot?
Craigslist Indpls Free
Epower Raley's
Texas Lottery Daily 4 Winning Numbers
Latest Posts
Article information

Author: Jonah Leffler

Last Updated:

Views: 6120

Rating: 4.4 / 5 (45 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Jonah Leffler

Birthday: 1997-10-27

Address: 8987 Kieth Ports, Luettgenland, CT 54657-9808

Phone: +2611128251586

Job: Mining Supervisor

Hobby: Worldbuilding, Electronics, Amateur radio, Skiing, Cycling, Jogging, Taxidermy

Introduction: My name is Jonah Leffler, I am a determined, faithful, outstanding, inexpensive, cheerful, determined, smiling person who loves writing and wants to share my knowledge and understanding with you.