IntroductionThe keyword substitution cipher is a monoalphabetic substitution cipher. The cipher alphabet is formed by writing the keyword (with repeating letters removed) first and then following with the unused letters of the alphabet in order. The following example is a cipher alphabet constructed around the word CIPHERS.
Depending on the distribution of letters in the keyword, the end of the cipher alphabet tends to be quite close to the plain alphabet. Choosing a keyword that contain at least one letter from 'R' onwards helps move the latter part of the cipher alphabet around a bit.
This apparent weakness is part of the trade-off made with classical ciphers between security and portability.
Programming The Cipher
The main concern when programming this cipher is the formation of the cipher alphabet. Once this is done, the approach is identical to the one used for the arbitrary letter substitution cipher. Assuming a keyword with no duplicate letters, the cipher alphabet can be formed quite easily with,
cipherAlphabet = keyword
FOR i ← 1 TO 26
IF keyword.Contains(Chr(i + 64)) = False THEN
cipherAlphabet ← cipherAlphabet + Chr(i + 64)
Given this, add the code to encrypt using this cipher alphabet,
plain ← "The secret is out."
upper ← UPPERCASE(plain)
cipherText ← ""
FOR letter ← 0 TO upper.LENGTH - 1
tmpASC ← ASCII CODE OF upper[letter]
IF tmpASC > = 65 AND tmpASC <= 90 THEN
cipherText ← cipherText + cipherAlphabet[tmpASC-65]
cipherText ← cipherText + upper[letter]
Either validate the keyword so that it works with your implementation of the pseudocode or implement the algorithm such that it can deal with duplicate letters and non-letter characters in the keyword.