Vigenere cipher

Vigenere cipher is an encryption technique consisting of a sequence of Caesar cipher each based on the letters of a secret text called a keyword. This mechanism is classified as polyalphabetic substitution.

In Caesar substitution each letter was shifted a number of positions down the alphabet. For example choosing the secret key to be 5, A would become F, B would become G and so on. In Vigenere cipher each letter is shifted a different number of positions namely a number equal to the position in the alphabet of the correspond letter in the keyword. The main difference in resulting text is that in Caesar cipher each letter in the alphabet had a unique encryption while Vigenere cipher produces different encryptions for the same letter.

Let’s encrypt the plaintext: KEEPTHESECRET with the keyword: CODE.
First we have to extend the keyword by repeating it until the number of letters in the keyword matches the number of letters in the plaintext. The keyword becomes: CODECODECODEC.

Than we get the positions in the alphabet of the letters of the keyword: 2 14 3 5 2 14 3 5 2 14 3 5 2 and perform a series of Caesar ciphers using these values as keys.
To do this easier a table of alphabets can be used, this table is called a Vigenère square and it look like this:

    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

Basically it consists of the alphabet written out 26 times, each alphabet shifted one additional position to the left compared to the alphabet above it, corresponding to the 26 possible Caesar ciphers.
At this point all we have to do to encrypt a letter in the plaintex is look into the corresponding alphabet of each letter in the keyword.

K E E P T H E S E C R E T
C O D E C O D E C O D E C

For K we look into the C alphabet (the alphabet that starts with C) and take the letter corresponding to K which is M.
For E we look into the O alphabet and take the letter corresponding to E which is S.
For the next E we look into the D alphabet and take the letter corresponding to E which is H.
And so on. KEEPTHESECRET becomes MSHTVVHWGQUIV.

Mathematically this would be Ci=(Pi+Ki) mod 26.

The PHP code for this is:

1
2
3
4
5
6
7
8
$plain_text=str_split('KEEPTHESECRET');
$n=count($plain_text);
$key=str_split('CODE');
$m=count($key);
$encrypted_text='';
for ($i=0; $i<$n;$i++)
	$encrypted_text.=chr(((ord($plain_text[$i])-65+ord($key[$i%$m])-65)%26) +65);
echo $encrypted_text;

Similarly the decription would be Ci=(Pi-Ki) mod 26.

The PHP code for decryption is:

1
2
3
4
5
6
7
8
$encrypted_text=str_split($encrypted_text);
$n=count($encrypted_text);
$key=str_split('CODE');
$m=count($key);
$decrypted_text='';
for ($i=0; $i<$n;$i++)
	$decrypted_text.=chr(((26+ord($encrypted_text[$i])-65-ord($key[$i%$m])+65)%26) +65);
echo $decrypted_text;

Breaking Vigenere cipher is a lot harder than breaking the Caesar cipher (especially for long keywords) as encrypting the same letter in multiple modes interferes with the frequency analysis. Still some characteristics of the letter frequencies of the plaintexts persist. Though breaking it proved difficult for the modest calculation power of the 19th century and it earned the name le chiffre indéchiffrable (French for ‘the unbreakable cipher’), using a modern personal computer is becomes relatively easy.

En fin as I promised a new riddle: it seems we received words from the country of the good wine and blue cheese. The unknown key is a short word and well known tower. Have fun!
PQGJVEIMLFPTXMKWEEIZSNXP

1 comment to Vigenere cipher

  • Don

    I ran across this researching for the new interactive novel The Quest: Mystery Of the Professor’s Notebook. I may reference it on my website. Thanks.

Leave a Reply

  

  

  

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Categories

Archives

Calendar

May 2018
M T W T F S S
« Jul    
 123456
78910111213
14151617181920
21222324252627
28293031