Coders Packet

Encryption and Decryption of plaintext using Caesar Cipher in C++

By Sri Sakthi M

Implementation of Encryption and Decryption of plain text using C++. Caesar Cipher is the simplest form of substitution cipher.

Each letter in the plain text is replaced by another letter by a fixed number of positions down the alphabet.

This method was used by Julius Caesar to send messages secretly.

Algorithm :

The inputs are message and key.

Encryption

  • Input: caesar
  • Output: fdhvu

Decryption

  • Input: fdhvu
  • Output: caesar

For encryption

The plaintext is traversed from left to right and the position of the alphabet is shifted to a certain number of positions according to the key provided.

Begin
   For i = 0 to m[i] != '\0'
      c = m[i]
//encrypt for lowercase letter If (c >= 'a' and ch <= 'z') c = c + key if (c > 'z') c = c - 'z' + 'a' - 1 done m[i] = c //encrypt for uppercase letter else if (c >= 'A' and c <= 'Z') c = c + key if (c > 'Z') c = c - 'Z' + 'A' - 1 done m[i] = ch done done Print the Encrypted message End

For decryption

The receiver receives the cipher text and the cipher text is traversed from left to right and the position of the alphabet is shifted according to the secret key which is already known by the receiver.

Begin
   For i = 0 to m[i] != '\0'
      c = m[i]
   //decrypt for lowercase letter
      if(c >= 'a' and c <= 'z')
         c = c - key
         if (c < 'a')
            c = c +'z' - 'a' + 1
      done
      m[i] = c
   //decrypt for uppercase letter
      else if (c >= 'A' and c <= 'Z')
         c = c - key
         if (c < 'A')
            c = c + 'Z' - 'A' + 1
         done
         m[i] = c
      done
   done
   Print decrypted message
End

Example

#include<bits/stdc++.h>
using namespace std;
int main() {
   cout<<"Enter the message:\n";
   char m[100];
   cin.getline(m,100); //input plaintext
   int i, j, length,choice,key;
   cout << "Enter key: ";
   cin >> key; //intput key
   length = strlen(m);
   cout<<"Enter your choice \n1. Encryption \n2. Decryption \n";
   cin>>choice;
   if (choice==1){ //encryption
      char c;
      for(int i = 0; m[i] != '\0'; ++i) {
         c = m[i];
         //encrypt lowercase letter
         if (c >= 'a' && ch <= 'z'){
            c = c + key;
            if (c > 'z') {
               c = c - 'z' + 'a' - 1;
            }  
            m[i] = h;
         }
         //encrypt uppercase letter
         else if (c >= 'A' && c <= 'Z'){
            c = c + key;
            if (c > 'Z'){
               c = c - 'Z' + 'A' - 1;
            }
            m[i] = c;
         }
      }
      printf("Encrypted message: %s", m);
   }
   else if (choice == 2) { //decryption
      char c;
      for(int i = 0; m[i] != '\0'; ++i) {
         c = m[i];
         //decrypt lowercase letter
         if(c >= 'a' && c <= 'z') {
            c = c - key;
            if(c < 'a'){
               c = c + 'z' - 'a' + 1;
            }
            m[i] = c;
         }
         //decrypt uppercase letter
         else if(c >= 'A' && c <= 'Z') {
            c = c - key;
            if(c < 'A') {
               c = c + 'Z' - 'A' + 1;
            }
            m[i] = c;
         }
      }
      cout << "Decrypted message: " << m;
   }
}

 

Output

For encryption:
Enter the message:
tutorial
Enter key: 3
Enter your choice
1. Encryption
2. Decryption
1
Encrypted message: fdhvu

For decryption:
Enter the message:
wxwruldo
Enter key: 3
Enter your choice
1. Encryption
2. Decryption
2
Decrypted message: caesar

 

Download Complete Code

Comments

No comments yet