C++ Program to Implement the Vigenere Cipher
Posted by Superadmin on August 08 2022 06:16:51

C++ Program to Implement the Vigenere Cipher

 

 

This is a C++ Program to implement Vigenere cipher. The Vigenère cipher is a method of encrypting alphabetic text by using a series of different Caesar ciphers based on the letters of a keyword. It is a simple form of polyalphabetic substitution.

 

Here is source code of the C++ Program to Implement the Vigenere Cypher. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. class Vigenere
  6. {
  7.     public:
  8.         string key;
  9.  
  10.         Vigenere(string key)
  11.         {
  12.             for (int i = 0; i < key.size(); ++i)
  13.             {
  14.                 if (key[i] >= 'A' && key[i] <= 'Z')
  15.                     this->key += key[i];
  16.                 else if (key[i] >= 'a' && key[i] <= 'z')
  17.                     this->key += key[i] + 'A' - 'a';
  18.             }
  19.         }
  20.  
  21.         string encrypt(string text)
  22.         {
  23.             string out;
  24.  
  25.             for (int i = 0, j = 0; i < text.length(); ++i)
  26.             {
  27.                 char c = text[i];
  28.  
  29.                 if (c >= 'a' && c <= 'z')
  30.                     c += 'A' - 'a';
  31.                 else if (c < 'A' || c > 'Z')
  32.                     continue;
  33.  
  34.                 out += (c + key[j] - 2 * 'A') % 26 + 'A';
  35.                 j = (j + 1) % key.length();
  36.             }
  37.  
  38.             return out;
  39.         }
  40.  
  41.         string decrypt(string text)
  42.         {
  43.             string out;
  44.  
  45.             for (int i = 0, j = 0; i < text.length(); ++i)
  46.             {
  47.                 char c = text[i];
  48.  
  49.                 if (c >= 'a' && c <= 'z')
  50.                     c += 'A' - 'a';
  51.                 else if (c < 'A' || c > 'Z')
  52.                     continue;
  53.  
  54.                 out += (c - key[j] + 26) % 26 + 'A';
  55.                 j = (j + 1) % key.length();
  56.             }
  57.  
  58.             return out;
  59.         }
  60. };
  61.  
  62. int main()
  63. {
  64.     Vigenere cipher("VIGENERECIPHER");
  65.  
  66.     string original =
  67.             "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!";
  68.     string encrypted = cipher.encrypt(original);
  69.     string decrypted = cipher.decrypt(encrypted);
  70.  
  71.     cout << original << endl;
  72.     cout << "Encrypted: " << encrypted << endl;
  73.     cout << "Decrypted: " << decrypted << endl;
  74. }

Output:

$ g++ VigenereCipher.cpp
$ a.out
 
Beware the Jabberwock, my son! The jaws that bite, the claws that catch!
Encrypted: WMCEEIKLGRPIFVMEUGXQPWQVIOIAVEYXUEKFKBTALVXTGAFXYEVKPAGY
Decrypted: BEWARETHEJABBERWOCKMYSONTHEJAWSTHATBITETHECLAWSTHATCATCH
 
------------------
(program exited with code: 0)
Press return to continue