# Vigenere Cipher and its implementation in C++ The Vigenere Cipher follows its name from a French cryptographer Blaise de Vigenere. This cipher is a substitution cipher that supports encryption and decryption of only alphabetic text.

There is a Vigenere’s Table which is responsible for encrypting the plaintext with the help of a key.

## Vigenere Cipher Table

The table consists of 26 rows and columns with each cell storing a single letter. The storage of the letter starts from A-Z in the first row, and a left shift operation influences each successive row.

The ciphertext is formed by letters indexed out from the above table using the row and column names.

## Encryption Process of Vigenere Cipher

Let us run an example through the encryption process for the better understanding of the Vigenere Cipher.

We need a plaintext and a key before starting the encryption process.

In order to create the ciphertext, we pick the first letter of each of the above values. `'J'` from the plaintext and `'B'` from the key. We find the table entry for which `'J'` is the row name, and `'B'` is the column name.

For every letter of the plaintext, we need a letter from the key. If length of the key is small, the algorithm repeats the key until the it matches the length.

I’ve highlighted the table entries used during the process:

## Decryption Process

The decryption process involves two elements: The ciphertext, and the key.

One thing to note here is that the key must not change during the encryption and decryption process.

In order to decode each letter, we pick the first letter of the key `'B'` and go through column `B's` values. As soon as we encounter the letter from ciphertext `'K'`, we check the row name of that table entry. The row name is the decoded letter for the corresponding alphabet of the ciphertext.

We follow the similar steps for each alphabet and end up decoding the complete ciphertext.

## Implementation of Vigenere Cipher in C++

```#include<iostream>
using namespace std;

// Function to extend the key to the length of plaintext
string get_full_key(string pt, string k){
if(k.size() >= pt.size())
return k;
else{
int psize = pt.size()-k.size();
int ksize = k.size();
while(psize >= ksize){
k += k;
psize -= ksize;
}

k += k.substr(0, psize);
return k;
}
}

// Encryption Function
string get_encryption(string pt, string k){

string ct = "";
for(int i=0;i<pt.size();i++)
ct += (char) (((int)pt[i]-'A' + (int)k[i]-'A') % 26) + 'A';

return ct;
}

// Decryption Function
string get_decryption(string ct, string k){

string pt = "";
for(int i=0;i<ct.size();i++)
pt += (char) ((((int)ct[i]- 'A' - (k[i] -'A')) + 26) % 26) + 'A';

return pt;
}

int main(){

// The Plaintext
string plaintext = "JOURNALDEV";

// The key
string key = "BEST";

// Function call to extend the key size
key = get_full_key(plaintext, key);

cout<<" THE PLAINTEXT: "<< plaintext <<endl;

cout<<" THE KEY: "<< key <<endl;

// Function call to encode the data
string ciphertext = get_encryption(plaintext, key);

cout<<" THE CIPHERTEXT: "<< ciphertext <<endl;

// Function call to decode the data
plaintext = get_decryption(ciphertext, key);

cout<<" THE DECODED PLAINTEXT: "<< plaintext <<endl;

return 1;
}
```

Output:

``` THE PLAINTEXT: JOURNALDEV
THE KEY: BESTBESTBE
THE CIPHERTEXT: KSMKOEDWFZ
THE DECODED PLAINTEXT: JOURNALDEV
```

## The insight behind the implementation

Vigenere’s Table table was built with a lot of thought behind it. We can exploit certain properties of the table for an efficient implementation of the Vigenere Cipher.

The sophisticated arrangement of letters leads to the following two mathematical properties:

### Decoding Principle

The above equations mean that if we consider any cell, then the alphabet contained in that cell can be obtained using the row and column numbers, and vice versa.

## Conclusion

The world of cryptography has no limits to the ideas behind such kind of substitution ciphers. Curious readers can pick up topics like Ceaser Cipher and Playfair Cipher in order to get closer to the realm of cryptography.

1. Anushka says: