A keyword cipher is a form of monoalphabetic substitution. A keyword is used as the key, and it determines the letter matchings of the cipher alphabet to the plain alphabet. Repeats of letters in the word are removed, then the cipher alphabet is generated with the keyword matching to A, B, C, etc. until the keyword is used up, whereupon the rest of the ciphertext letters are used in alphabetical order, excluding those already used in the key.
The first line of input contains the keyword which you wish to enter. The second line of input contains the string which you have to encrypt.
Plain-text : 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
Encrypted: K R Y P T O S A B C D E F G H I J L M N Q U V W X Z
With KRYPTOS as the keyword, all As becoming Ks, all Bs becoming Rs, and so on. Encrypting the message “knowledge is power” using the keyword “Kryptos”:
Encrypting the message: Knowledge is Power
Encoded message: IlmWjbaEb GQ NmWbp
To decode the message you check the position of the given message in encrypting text with the plain text.
Plain-text : 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
Encrypted: K R Y P T O S A B C D E F G H I J L M N Q U V W X Z
Message: PTYBIATLEP
Deciphered Text: DECIPHERED
Now, how do we generate the deciphered string?
We search for ‘P’ in Encrypted Text and compare its position with plain text letter and generate that letter. So ‘P’ becomes ‘D’, ‘T’ becomes ‘E’, ‘Y’ becomes ‘C’, and so on.