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.
function Mod($a, $b)
{
return ($a % $b + $b) % $b;
}
function Cipher($input, $key, $encipher)
{
$keyLen = strlen($key);
for ($i = 0; $i < $keyLen; ++$i)
if (!ctype_alpha($key[$i]))
return ""; // Error
$output = "";
$nonAlphaCharCount = 0;
$inputLen = strlen($input);
for ($i = 0; $i < $inputLen; ++$i)
{
if (ctype_alpha($input[$i]))
{
$cIsUpper = ctype_upper($input[$i]);
$offset = ord($cIsUpper ? 'A' : 'a');
$keyIndex = ($i - $nonAlphaCharCount) % $keyLen;
$k = ord($cIsUpper ? strtoupper($key[$keyIndex]) : strtolower($key[$keyIndex])) - $offset;
$k = $encipher ? $k : -$k;
$ch = chr((Mod(((ord($input[$i]) + $k) - $offset), 26)) + $offset);
$output .= $ch;
}
else
{
$output .= $input[$i];
++$nonAlphaCharCount;
}
}
return $output;
}
function Encipher($input, $key)
{
return Cipher($input, $key, true);
}
function Decipher($input, $key)
{
return Cipher($input, $key, false);
}
Example
$text = "Hello, World!";
$cipherText = Encipher($text, "cipher");
$plainText = Decipher($cipherText, "cipher");
Output
cipherText: "Jmass, Nqzak!"
plainText: "Hello, World!"