Auto Key Cipher


Autokey (Autoclave) Cipher is a Symmetric polyalphabetic (Polyceaser) substitution cipher. This algorithm is about changing plaintext letters based on secret key letters. Each letter of the message is shifted along some alphabet positions. The number of positions is equal to the place in the alphabet of the current key letter.

Note: Cipher text is also called as encrypted text(don't confuse). And we're assuming index starting from 0 not 1.

Example Test Cases

Case-1:

Input:
Enter the Single Key word: L //In A-Z, L will be at 11 index.
Enter the Plain Text: hello

Output:
The Plain Text is: hello
The Text after Encryption(Cipher text) is: SLPWZ
After Decryption the Text is: hello

Case-2:

Input:
Enter the Single Key word: Q //In A-Z, L will be at 11 index.
Enter the Plain Text: harry

Output:
The Plain Text is: harry
The Text after Encryption(Cipher text) is: XHRIP
After Decryption the Text is: harry

How this Algorithm works?

Formula

Encryption

Ei = (Pi + Ki) mod 26

Decryption

Di = (Ei - Ki + 26) mod 26

Ei: Encrypted text
Pi: Plain Text values
Ki: Plain Text values after adding key.

Encryption

First of all, we need a key and a plaintext. Key can be a alphabet or a number.

Step-1: Now, the first step is to convert all letter in plaintext either to lowercase or uppercase. And now get the equivalent alphabetic values of plaintext.
Consider,
Plaintext: HELLO,
Key: 11.

text[i] = toupper(text[i]);
int value = text[i] - 'A';

Step-2: Now, shift the values of plaintext towards right and add key(11) at first. Last value of plaintext will discarded. (7 4 11 11 14) are the values of plaintext. Now, the cipher text will be (11 7 4 11 11).

Step-3: Now, add both the above number. Cipher values will be (18 11 15 22 25). Now, convert this values to equivalent characters. Here, if the number/sum exceed 26, then we need to start from beginning.

Then the resultant will be our Cipher Text or Encrypted Text.
Cipher Text: S L P W Z

Encryption Analysis:

Plain Text: H E L L O
Equi Alphabet No.: 7 4 11 11 14
Key: 11 7 4 11 11 (Added Key at first index and right shift)
+
Cipher Values: 18 11 15 22 25 (Equivalent Alphabet Number)
Cipher Key: S L P W Z

Decryption

Step-4: Now, Decryption is Same as Encryption except one change. In Encryption, we shift the values towards right by adding/placing key at first index. Here in Decryption, we shift the values towards left by adding/placing key at last index.
Now, repeat the Step-1 and Step-2 for the cipher text by implementing this change.

Then the resultant will be our Plain Text or Decrypted Text.
Decrypted Text: H E L L O

Decryption Analysis:

Plain Text: S L P W Z
Equi Alphabet No.: 18 11 15 22 25
Key: 11 15 22 25 11 (Added Key at last index and left shift)
-
Cipher Values: 7 4 11 11 14 (Equivalent Alphabet Number)
Cipher Key: H E L L O

Complexity and Logic

From all this step, we're accessing same letters and values at multiple times for multiple operations. below, few lines will complete all the operations.

For Encryption:

keyvalue = value;
value = text[i] - 'A';
text[i] = (text[i] - 'A' + keyvalue) % 26 + 'A';

For Decryption:

keyvalue = value;
result = (text[i] - 'A' - keyvalue) % 26 + 'A';
text[i] = result < 'A' ? (result + 26) : (result);
value = text[i] - 'A';

Here, we accessing each value/letter at one time in Encryption and in Decryption. So the Time Complexity for Each will be ***O(n)***.

Security of Auto Key Cipher

Autokey cipher is more secure than any other poly-alphabetic ciphers that uses defined keys since the key does not repeat again in cipher text/message. So, some methods like Kasiski examination/index of coincidence analysis will not work on this type of ciphertext's, except for similar ciphers that uses a single repeated key.
A main weakness of the system is that the plaintext is also a part of the key. which means that the key will likely to contain common words at multiple/various positions. The key can be attacked by using a dictionary of common words, bigrams, trigrams(any brute force)..etc, and by attempting the decryption of the message by moving that word through the key until the actual readable text appears.

Question

In Encryption, If Text values="6 18 18 14 2"(gssoc) Key = 18(S), Then what will be the result after placing key ?

6 18 18 14 2
6 18 18 14 18
18 6 18 18 14
18 6 18 14 18
6 18 18 14 2
18 6 18 18 14
We have to right shift the plaintext values and place key at first index.

Question

In Encryption, If Text values="6 18 18 14 2"(gssoc) Key = 18(S), Then what will be the Cipher Text ?

QYKYG
GYKGY
QYYKG
YYKGQ
6 18 18 14 2
18 6 18 18 14 #RightShift
24 24 10 6 16#Addition
Y Y K G Q #EquivalentLetters

With this article at OpenGenus, you must have the complete idea of Auto Key Cipher. Enjoy.