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

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
encrypt a string

#Decrypting
decrypt a string

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 both e 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 number
void 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 program

int 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 key
void 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 message
void 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 message
void 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++ encryption and decryption program

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