Vigenere Cipher and its implementation in C++

Vigenere Cipher Featured Image

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.

Vigenere Table 1
Vigenere’s Table

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.

Vigenere Cipher Plaintext And Key
Plaintext and Key

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.

Vigenere Cipher Ciphertext Edited
Creation of Ciphertext

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.

Vigenere Cipher Complete Ciphertext
Encryption of ‘JOURNALDEV’

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

Vigenere Cipher Values Edited 1
Vigenere’s Table Entries used

Decryption Process

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

Vigenere Cipher Ciphertext Key
Ciphertext and 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.

Vigenere Cipher Values Decoded Edited 1
Decoding ciphertext using Vigenere’s Table

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:

Encoding Principle

Vigenere Cipher Encoding
Encoding Principle

Decoding Principle

Vigenere Cipher Decoding
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.

We hope this article was easy to follow. Do drop in your comment if you loved this article. We’d love to hear your thoughts!

Comments

  1. Anushka says:

    Apt and Simple. Gooood Job!!!!!

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages