# Autokey Cipher

The Autokey Cypher is a cypher made by Blaise de Vigenère, the author of the popular Vigenère Cypher. Blaise de Vigenère created the Autokey Cypher in 1586! One cool feature of this cypher is that it makes use of the mod function in math. Here is an example of how to encode and decode one!

## Encoding

The first step to encode an autokey cypher is to give the plaintext (flag) values based on the table:

 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 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

As an example, I will use the plaintext, Hello how are you, and encode it.

Plaintext (P)= H E L L O H O W A R E Y O U

Plaintext (P’s Values) = 7,4,11,11,14,7,14,22,0,17,4,24,14,20

Now we will need to make the keystream which uses the Key (K) and the Plaintext (P’s Values).

Key (K) = 12 (In this example)

Plaintext (P’s Values) = P1,P2,P3,...

Keystream = K, P1,P2,P3,...

You take off the last value for the keystream to even it out on both halves. This will make sense later.

In this example it would be:

Plaintext (P’s Values) = 7,4,11,11,14,7,14,22,0,17,4,24,14,20

Keystream (K’s Values) = 12, 7,4,11,11,14,7,14,22,0,17,4,24,14

Now to encrypt it further, because this would be too easy to decipher!

We use this formula to encrypt:

C = (P+K)Mod26

So

P1 (7) + K1 (12) = 19

Finding mod (modulo) 26 is simple, It is finding what the remainder would be when multiplied by the dividend (In most cases you can just minus them, including in some of these) (This is used in higher-level math).:

19/26 = ~ 0.7307

If the ending result has a whole number in the front, minus it. In this case, we don’t. Now we take that number and multiply it by 26 to get:

(19)Mod 26 = 19

C1 = 19

Just for practice, I will use another example.

C2 = (P2+K2)Mod26

P2 (4) + K2 (7) = 11

11/26 = ~ 0.423

(Remove the whole numbers if possible, it is not applicable here) Then multiply that value by 26:

(11)Mod 26 = 11

C2 = 11

Now you put them all in order to get C’s Values:

C’s Values = C1,C2,C3,...

C’s Values = 19,11,15,22,25,21,21,10,22,17,21,2,12,8

Now we plug these values into the bak into the table we used before! (To get the ciphertext)

 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 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

Ciphertext = TLPWZVVKWRVCMI

This is what you will be given plus the key.

## Decoding

To decode the Autokey Cypher you will need the keystream and the ciphertext.

First, you will take the Ciphertext and revert the values using the table to get C’s Values.

 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 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

Ciphertext = TLPWZVVKWRVCMI

To get:

C’s Values = 19,11,15,22,25,21,21,10,22,17,21,2,12,8

We now use this formula to decode this into the P’s Values:

P = (C-K)Mod 26

C’s Values = 19,11,15,22,25,21,21,10,22,17,21,2 ,12, 8

Keystream (K’s Values) = 12, 7, 4, 11,11,14, 7, 14,22, 0,17, 4, 24,14

(I evened them out so it would be easier for you to see.) (You can usually minus just like in the encryption process.)

So…

P1 = (C1 (19) - K1 (12) )Mod 26

P1 = 7

Continue this until you reach the end.

P’s Values (P) = 7, 4, 11, 11, 14, 7, 14, 22, 0, 17, 4, 24, 14, 20.

When you decode it, the last value is added by using the formula!

Now you just use the table like before to decode P’s Values:

 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 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

Plaintext (P) = HELLOHOWAREYOU

That is the flag for this example!

Without the C’s Values, you will not be able to find out the last value of the Plaintext!

Happy encoding and decoding!