Vigenère Cipher Algorithm Program in C/C++

In cryptography, a cipher (or cypher) is an algorithm for performing encryption or decryption—a series of well-defined steps that can be followed as a procedure. An alternative, less common term is encipherment. To encipher or encode is to convert information into cipher or code. In common parlance, “cipher” is synonymous with “code“, as they are both a set of steps that encrypt a message; however, the concepts are distinct in cryptography, especially classical cryptography.

Codes generally substitute different length strings of character in the output, while ciphers generally substitute the same number of characters as are input. There are exceptions and some cipher systems may use slightly more, or fewer, characters when output versus the number that was input.

In this post, we will discuss the Vigenère Cipher. The Vigenère cipher is a cryptographic algorithm that is used to encrypt or decrypt a message. It employs a form of polyalphabetic substitution in which the current letters of the key and the message is used to determine the encrypted message’s letter.

Algorithm

It is similar to Caesar Cipher. In a Caesar cipher, each letter of the alphabet is shifted along by some number of places. For example, in a Caesar cipher of shift 3, A would become DB would become EY would become B and so on. The Vigenère cipher has several Caesar ciphers in sequence with different shift values.

To encrypt, a table of alphabets can be used, termed tabula rectaVigenère square or Vigenère table. It has the alphabet written out 26 times in different rows, each alphabet shifted cyclically to the left compared to the previous alphabet, corresponding to the 26 possible Caesar ciphers. At different points in the encryption process, the cipher uses a different alphabet from one of the rows. The alphabet used at each point depends on a repeating keyword.

The Vigenère square or Vigenère table, also known as the tabula recta, can be used for encryption and decryption.

For example, suppose that the plaintext to be encrypted isATTACKATDAWN.

The person sending the message chooses a keyword and repeats it until it matches the length of the plaintext, for example, the keyword “LEMON”:LEMONLEMONLE

Each row starts with a key letter. The rest of the row holds the letters A to Z (in shifted order). Although there are 26 key rows shown, a code will use only as many keys (different alphabets) as there are unique letters in the key string, here just 5 keys: {L, E, M, O, N}. For successive letters of the message, successive letters of the key string will be taken and each message letter enciphered by using its corresponding key row. The next letter of the key is chosen, and that row is gone along to find the column heading that matches the message character. The letter at the intersection of [key-row, msg-col] is the enciphered letter.

For example, the first letter of the plaintext, A, is paired with L, the first letter of the key. Therefore, row L and column A of the Vigenère square are used, namely L. Similarly, for the second letter of the plaintext, the second letter of the key is used. The letter at row E and column T is X. The rest of the plaintext is enciphered in a similar fashion:

Plaintext:ATTACKATDAWN
Key:LEMONLEMONLE
Ciphertext:LXFOPVEFRNHR

Decryption is performed by going to the row in the table corresponding to the key, finding the position of the ciphertext letter in that row and then using the column’s label as the plaintext. For example, in row L (from LEMON), the ciphertext L appears in column A, which is the first plaintext letter. Next, in row E (from LEMON), the ciphertext X is located in column T. Thus T is the second plaintext letter.

We will use C++ to write this algorithm due to the standard template library support. Hence, we will write the program of the Vigenère Cipher algorithm in C++, although, it’s very similar to C.

Encryption

INPUT:
line 1: message (s)
line 2: key (key)

OUTPUT:
line 1: Encrypted message (encrypt)

The following is the Caesar Cipher encryption algorithm program in C++.

#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main(){
	int i,j,k,n;
	vector<vector<char> > a(26,vector<char>(26));
	k=0;
	n=26;
	for(i=0;i<n;i++){
		k=i;
		for(j=0;j<n;j++){
			a[i][j]='A'+k;
			k++;
			if(k==26)
				k=0;
		}
	}
	cout<<"Enter the message\n";
	string s;
	cin>>s;
	cout<<"Enter the key\n";
	string key;
	cin>>key;
	k=0;
	int mod = key.size();
	for(i=key.size();i<s.size();i++){
		key+=key[k%mod];
		k++;
	}
	string encrypt;
	for(i=0;i<s.size();i++){
		encrypt+= a[s[i]-'A'][key[i]-'A'];
	}
	cout<<"Encrypted message: "<<encrypt<<'\n';
	return 0;
}

OUTPUT:

Enter the message
HELLOWORLD
Enter the key
TEST
Encrypted message: AIDEHAGKEH

Decryption

INPUT:
line 1: message (s)
line 2: key (key)

OUTPUT:
line 1: decrypted message (decrypt)

The following is the Vigenère Cipher decryption algorithm program in C++.

#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main(){
	int i,j,k,n;
	vector<vector<char> > a(26,vector<char>(26));
	k=0;
	n=26;
	for(i=0;i<n;i++){
		k=i;
		for(j=0;j<n;j++){
			a[i][j]='A'+k;
			k++;
			if(k==26) k=0;
		}
	}
	cout<<"Enter the encrypted message\n";
	string s;
	cin>>s;
	cout<<"Enter the key\n";
	string key;
	cin>>key;
	k=0;
	for(i=key.size();i<s.size();i++){
		key+=key[k];
		k++;
	}
	string decrypt;
	for(i=0;i<s.size();i++){
		for(j=0;j<n;j++){
			if(a[j][key[i]-'A']==s[i]){
				decrypt += 'A'+j;
				break;
			}
		}
	}
	cout<<"Decrypted message: "<<decrypt<<'\n';
	return 0;
}

OUTPUT:

Enter the encrypted message
AIDEHAGKEH
Enter the key
TEST
Decrypted message: HELLOWORLD

Other cryptography algorithms:

Let us know in the comments if you are having any questions regarding this cryptography cipher Algorithm.

And if you found this post helpful, then please help us by sharing this post with your friends. Thank You

One comment

  1. it doesnt work for none capital letters

Leave a Reply

%d bloggers like this: