Vigenere Cipher

In cryptography, a 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.

The Vigenère cipher has been reinvented many times. The method was originally described by Giovan Battista Bellaso in his 1553 book La cifra del. Sig. Giovan Battista Bellaso; however, the scheme was later misattributed to Blaise de Vigenère in the 19th century, and is now widely known as the Vigenère cipher.

This scheme of cipher uses a text string (say, a word) as a key, which is then used for doing a number of shifts on the plaintext.

For example, let’s assume the key is 'cipher'. Each alphabet of the key is converted to its respective numeric value: In this case,

c → 2, i → 8, p → 15, h → 7, e → 4, r → 17.

Thus, the key is: 2 8 15 7 4 17.

 

Algorithm

  • The sender and the receiver decide on a key. Say 'cipher' is the key. Numeric representation of this key is '2 8 15 7 4 17'
  • The sender wants to encrypt the message, say 'cryptography'. He will arrange plaintext and numeric key as follows −
c r y p t o g r a p h y
2 8 15 7 4 17 2 8 15 7 4 17

 

  • He now shifts each plaintext alphabet by the number written below it to create ciphertext as shown below −
c r y p t o g r a p h y
2 8 15 7 4 17 2 8 15 7 4 17
e z n w x f i z p w l p

 

  • Here, each plaintext character has been shifted by a different amount – and that amount is determined by the key. The key must be less than or equal to the size of the message.
  • For decryption, the receiver uses the same key and shifts received ciphertext in reverse order to obtain the plaintext.
e z n w x f i z p w l p
2 8 15 7 4 17 2 8 15 7 4 17
c r y p t o g r a p h y

 

Security

Vigenere Cipher was designed by tweaking the standard Caesar cipher to reduce the effectiveness of cryptanalysis on the ciphertext and make a cryptosystem more robust. It is significantly more secure than a regular Caesar cipher.

In the history, it was regularly used for protecting sensitive political and military information. It was referred to as the unbreakable cipher due to the difficulty it posed to the cryptanalysis.



									/*****Please include following header files*****/
// string
// ctype.h
/***********************************************/

/*****Please use following namespaces*****/
// std
/*****************************************/

static int Mod(int a, int b)
{
	return (a % b + b) % b;
}

static string Cipher(string input, string key, bool encipher)
{
	int keyLen = key.size();

	for (int i = 0; i < keyLen; ++i)
		if (!isalpha(key[i]))
			return ""; // Error

	string output = "";
	int nonAlphaCharCount = 0;
	int inputLen = input.size();

	for (int i = 0; i < inputLen; ++i)
	{
		if (isalpha(input[i]))
		{
			bool cIsUpper = isupper(input[i]);
			char offset = cIsUpper ? 'A' : 'a';
			int keyIndex = (i - nonAlphaCharCount) % keyLen;
			int k = (cIsUpper ? toupper(key[keyIndex]) : tolower(key[keyIndex])) - offset;
			k = encipher ? k : -k;
			char ch = (char)((Mod(((input[i] + k) - offset), 26)) + offset);
			output += ch;
		}
		else
		{
			output += input[i];
			++nonAlphaCharCount;
		}
	}

	return output;
}

static string Encipher(string input, string key)
{
	return Cipher(input, key, true);
}

static string Decipher(string input, string key)
{
	return Cipher(input, key, false);
}
								


Example

									string text = "Hello, World!";
string cipherText = Encipher(text, "cipher");
string plainText = Decipher(cipherText, "cipher");
								


Output

									cipherText:	"Jmass, Nqzak!"
plainText:	"Hello, World!"