﻿ Vigenère cipher

Vigenère cipher is a simple polyalphabetic cipher, in which the ciphertext is obtained by modular addition of a (repeating) key phrase and an open text (both of the same length).

## Encryption

The encryption can be described by the following formula: Ci - i-th character of the ciphertext
Ti - i-th character of the open text
Ki - i-th character of the key phrase (if the key phrase is shorter than the open text, which is usual, than the keyphrase is reapeated to math the length of the open text)
m - length of the alphabet

## Decryption

The process of decryption is analogous. The key phrase is modularly subtracted from the ciphertext. Ci - i-th character of the ciphertext
Ti - i-th character of the open text
Ki - i-th character of the key phrase (if the key phrase is shorter than the open text, which is usual, than the keyphrase is reapeated to math the length of the open text)
m - length of the alphabet

## Vigenère square

In order to simplify the encryption and decryption process, we may use Vigenère square (tabula recta). Each row of tabula recta consists of all letters of the English alphabet. The first row starts with the letter a, and each following row is shifted by one letter (second row starts with b, third with c...).

 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z A A B C D E F G H I J K L M N O P Q R S T U V W X Y Z B B C D E F G H I J K L M N O P Q R S T U V W X Y Z A C C D E F G H I J K L M N O P Q R S T U V W X Y Z A B D D E F G H I J K L M N O P Q R S T U V W X Y Z A B C E E F G H I J K L M N O P Q R S T U V W X Y Z A B C D F F G H I J K L M N O P Q R S T U V W X Y Z A B C D E G G H I J K L M N O P Q R S T U V W X Y Z A B C D E F H H I J K L M N O P Q R S T U V W X Y Z A B C D E F G I I J K L M N O P Q R S T U V W X Y Z A B C D E F G H J J K L M N O P Q R S T U V W X Y Z A B C D E F G H I K K L M N O P Q R S T U V W X Y Z A B C D E F G H I J L L M N O P Q R S T U V W X Y Z A B C D E F G H I J K M M N O P Q R S T U V W X Y Z A B C D E F G H I J K L N N O P Q R S T U V W X Y Z A B C D E F G H I J K L M O O P Q R S T U V W X Y Z A B C D E F G H I J K L M N P P Q R S T U V W X Y Z A B C D E F G H I J K L M N O Q Q R S T U V W X Y Z A B C D E F G H I J K L M N O P R R S T U V W X Y Z A B C D E F G H I J K L M N O P Q S S T U V W X Y Z A B C D E F G H I J K L M N O P Q R T T U V W X Y Z A B C D E F G H I J K L M N O P Q R S U U V W X Y Z A B C D E F G H I J K L M N O P Q R S T V V W X Y Z A B C D E F G H I J K L M N O P Q R S T U W W X Y Z A B C D E F G H I J K L M N O P Q R S T U V X X Y Z A B C D E F G H I J K L M N O P Q R S T U V W Y Y Z A B C D E F G H I J K L M N O P Q R S T U V W X Z Z A B C D E F G H I J K L M N O P Q R S T U V W X Y
Vigenère square

### Encryption

To encrypt the open text, we have to sum together the first letters of the open text and key phrase, the second letters, third and so on. To encrypt the n-th letter of the open text (assume “L“) using the Vigenère square, we find the letter on the horizontal axis of the table and we find n-th letter of the key phase on the vertical axis (assume “T“). At the intersection of the given row and column the resulting n-th letter of the ciphertext is located (“E“).

### Decryption

The decryption works the other way around. We find in the row corresponding to the n-th letter of the key phrase (“T”) a cell in which the n-th letter of the ciphertext (“E”) resides. Its column is denoted by the n-th letter of the open text (“L“).

## Frequency analysis of Vigenère cipher

If we know the length (n) of the repeating key phrase, we are able to perform frequency analysis on every n-th letter. The process is equivalent to frequency analysis of Caesar cipher – by comparison of frequencies of letters in open text and ciphertext we obtain the shift (letter of the key phrase) and we are immediately able to decrypt the cipher.

## Example

### Encryption

Open text: ATTACK AT DAWN
Key phrase: CAT
Length of the alphabet: 26

A + C = 0  + 2  = 2  = C
T + A = 19 + 0  = 19 = T
T + T = 19 + 19 = 12 = M
A + C = 0  + 2  = 2  = C
C + A = 2  + 0  = 2  = C
K + T = 10 + 19 = 3  = D
A + C = 0  + 2  = 2  = C
T + A = 19 + 0  = 19 = T
D + T = 3  + 19 = 22 = W
A + C = 0  + 2  = 2  = C
W + A = 22 + 0  = 22 = W
N + T = 13 + 19 = 6  = G


### Decryption

Ciphertext: ATMCCDCTWCWG
Key phrase: CAT
Length of the alphabet: 26

C – C = 2  – 2  = 0  = A
T – A = 19 – 0  = 19 = T
M – T = 12 – 19 = 19 = T
C – C = 2  – 2  = 0  = A
C – A = 2  – 0  = 2  = C
D – T = 3  – 19 = 10 = K
C – C = 2  – 2  = 0  = A
T – A = 19 – 0  = 19 = T
W – T = 22 – 19 = 3  = D
C – C = 2  – 2  = 0  = A
W – A = 22 – 0  = 22 = W
G - T = 6  – 19 = 13 = N


## Code

/**
* Vigenere cipher
* @author Pavel Micka
*/
public class VigenereCipher {
/**
* Encrypt using Vigenere cipher
* @param s open text
* @param key key phrase (only capital letters)
* @return ciphertext (only capital letters)
*/
public static String encipher(String s, String key){
StringBuilder builder = new StringBuilder();
for(int i = 0; i < s.length(); i ++){
if(s.charAt(i) < 65 || s.charAt(i) > 90){ //ASCII character (capital letter)
throw new IllegalArgumentException("" +
"Open text must contain only capital letters");
}
char encyphered = s.charAt(i) + getShift(key, i) > 90 ? (char)((s.charAt(i) + getShift(key, i)) - 26) : (char)(s.charAt(i) + getShift(key, i));
builder.append(encyphered);
}
return builder.toString();
}
/**
* Decrypt using Vigenere cipher
* @param s cipher text (only capital letters)
* @param key key phrase (only capital letters)
* @return open text
*/
public static String decipher(String s, String key){
StringBuilder builder = new StringBuilder();
for(int i = 0; i < s.length(); i ++){
if(s.charAt(i) < 65 || s.charAt(i) > 90){ //ASCII character (capital letter)
throw new IllegalArgumentException("" +
"Ciphertext must contain only capital letters");
}
//subtract shift modularly
char decyphered = s.charAt(i) - getShift(key, i) < 65 ? (char)((s.charAt(i) - getShift(key, i)) + 26) : (char)(s.charAt(i) - getShift(key, i));
builder.append(decyphered);
}
return builder.toString();
}
/**
* Get shift
* @param key key phrase
* @param i position in the text
* @return shift
*/
private static int getShift(String key, int i) {
if(key.charAt(i % key.length()) < 65 || key.charAt(i % key.length()) > 90){
throw new IllegalArgumentException("" +
"Key phrase must contain only capital letters");
}
return ((int)key.charAt(i % key.length())) - 65;
}

public static void main(String[] args){
String text = "ATTACKATDAWN";
String key = "CAT";
String enciphered = encipher(text, key);
System.out.println(enciphered);
System.out.println(decipher(enciphered, key));
}
}