|
| 1 | +# Vigenere Cipher |
| 2 | + |
| 3 | +The Vigenere cipher is a famous toy cipher. It was invented in 1553 by the Italian cryptographer Giovan Battista Bellaso but for centuries was attributed to the 16th-century French cryptographer Blaise de Vigenère, who devised a similar cipher in 1586. |
| 4 | + |
| 5 | +It is easy to encrypt and decrypt but at the same time, it is easy to intercept and crack this cipher. It is a polyalphabetic substitution cipher, meaning that the same letter of the alphabet can be replaced by different letters depending on the key. The key is a word or phrase that is repeated to match the length of the message. Then the letters of the key are used to shift the letters of the message. |
| 6 | + |
| 7 | +## Improvement over Caesar cipher |
| 8 | + |
| 9 | +The Vigenere cipher is an improvement over the Caesar cipher. The Caesar cipher uses a single character as the key to shift the message. In contrast, the Vigenere cipher uses a word or a phrase as the key. This means that the same character occurring at different positions in the message will be shifted by different amounts. This makes it harder to crack the cipher. |
| 10 | + |
| 11 | +## Setup |
| 12 | + |
| 13 | +1. Choose the message space you are going to use. In our case, we will use all lowercase letters of the English language. But we can use ASCII characters as well. Let us denote the size of the message space as $n$. In our case, $n = 26$. |
| 14 | +2. Assign a numerical value to each letter of the message space. For example, $a=1, b=2, c=3$ and so on. |
| 15 | +3. Choose a key. |
| 16 | + |
| 17 | +## Encryption |
| 18 | + |
| 19 | +1. Repeat the key to match the length of the message. For example, if the message is `checktheking` and the key is `chess`, the key is repeated to match the length of the message. The key is now `chesschessch`. |
| 20 | +2. Now, the letters of the key are used to shift the letters of the message. Let $a$ be $1$, $b$ be $2$ and so on. Then, the message is encrypted using the following formula: $C_i = (V(M_i) + V(K_i)) \mod n$ where $C_i$ is the encrypted letter at position $i$, $V(M_i)$ is the numerical value of the letter at position $i$ and $V(K_i)$ is the numerical value of the letter at position $i$, and $n$ is the size of the message space. |
| 21 | + |
| 22 | +| Message | c | h | e | c | k | t | h | e | k | i | n | g | |
| 23 | +|---------|---|---|---|---|---|---|---|---|---|---|---|---| |
| 24 | +| Value | 3 | 8 | 5 | 3 | 11| 20| 8 | 5 | 11| 9 | 14| 7 | |
| 25 | +| Key | c | h | e | s | s | c | h | e | s | s | c | h | |
| 26 | +| Shift | 3 | 8 | 5 | 19| 19| 3 | 8 | 5 | 19| 19| 3 | 8 | |
| 27 | +| Cipher | f | p | j | q | z | w | p | j | z | z | c | p | |
| 28 | + |
| 29 | +So the encrypted message is `fpjqzwjpzzcp`. |
| 30 | + |
| 31 | +## Decryption |
| 32 | + |
| 33 | +Decryption is the inverse of encryption. It is is done by subtracting the shift value from the cipher value. The formula is $M_i = L((C_i - V(K_i)) \mod n)$ where everything is defined as above and $L$ is additionally defined as a function which converts a numerical value back to a letter. |
| 34 | + |
| 35 | +| Cipher | f | p | j | q | z | w | p | j | z | z | c | p | |
| 36 | +|---------|---|---|---|---|---|---|---|---|---|---|---|---| |
| 37 | +| Key | c | h | e | s | s | c | h | e | s | s | c | h | |
| 38 | +| Shift | 3 | 8 | 5 | 19| 19| 3 | 8 | 5 | 19| 19| 3 | 8 | |
| 39 | +| Value | 3 | 8 | 5 | 3 | 11| 20| 8 | 5 | 11| 9 | 14| 7 | |
| 40 | +| Message | c | h | e | c | k | t | h | e | k | i | n | g | |
| 41 | + |
| 42 | +So the decrypted message is `checktheking`, as expected. |
| 43 | + |
| 44 | +## Complexity Analysis |
| 45 | + |
| 46 | +### Encryption |
| 47 | + |
| 48 | +The encryption is done by adding the shift value to the message value. So the **time complexity is $O(n)$** where $n$ is the length of the message. |
| 49 | +We use a linear data structure such as an array to store the message and the key. So the **space complexity is $O(n)$**. |
| 50 | + |
| 51 | +### Decryption |
| 52 | + |
| 53 | +Decryption is similar to encryption (except for the subtraction operation). |
| 54 | +**So time and space complexity are the same as for encryption - $O(n)$**. |
| 55 | + |
| 56 | +## Cryptanalysis and caution |
| 57 | + |
| 58 | +1. **It is a toy cipher. It is easy to crack. It is not secure.** |
| 59 | +2. There are several key (length) finding methods such as |
| 60 | + - [Kasiski examination](https://en.wikipedia.org/wiki/Kasiski_examination) |
| 61 | + - [Index of coincidence](https://en.wikipedia.org/wiki/Index_of_coincidence) |
| 62 | +3. Once the key length is found, [frequency analysis](https://en.wikipedia.org/wiki/Frequency_analysis) can be used to find the key and hence crack the cipher. |
| 63 | +4. Therefore, this cipher should not be used to encrypt any important data. |
| 64 | + |
| 65 | +## Implementations |
| 66 | + |
| 67 | +- [C++](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/ciphers/vigenere_cipher.cpp) |
| 68 | +- [Python](https://github.com/TheAlgorithms/Python/blob/master/ciphers/vigenere_cipher.py) |
| 69 | +- [JavaScript](https://github.com/TheAlgorithms/JavaScript/blob/master/Ciphers/VigenereCipher.js) |
0 commit comments