Substitution Ciphers
Another encryption example is the substitution cipher. With a substitution cipher, each character in an alphabet maps to a cryptabet with different characters in the same position. The simplest example of this is the Atbash or reverse-alphabet cipher.
Alphabet: ABCDEFGHIJKLMNOPQRSTUVWXYZ
Cryptabet: ZYXWVUTSRQPONMLKJIHGFEDCBA
Example project: substitution_cipher_atbash
- Enter substitution_cipher_atbash, then flash it into the micro:bit.
- Open the terminal, check that local echo is checked, and make sure your keyboard is set to CAPS LOCK.
- Try ABCD, the result should be ZYXW.
- Now, try ZYXW, the result should be ABCD.
How It Works – substitution_cipher_atbash
Your code has to use set result to (atbash(plaintext)) to get the ciphertext—no key required.
The atbash function has one parameter, text. The built-in key is the cryptabet, a reverse alphabet in this case. Like the other cipher functions, this one also declares an empty string named result to store the result.
Inside the loop that generates the ciphertext, it starts the same way, by finding the index of the letter in the alphabet with set index to ((alpha) find index of (letter)). What’s different is set result to (join (result) (char from(crypta) at (index))). This grabs the character with the same index in the cryptabet, and adds it to the result.
Example: Let’s say that letter is E. It’s index in the alphabet is 4. The character at crypta[4] is V, so V would be added to the result string.
Your Turn – Custom Cryptabet
Here is an alphabet that also includes digits, a space and some characters that would be useful for making a dictionary.
Alphabet: abcdefghijklmnopqrstuvwxyz 123456789:,{}’
- Make a cryptabet by scrambling all 41 characters.
Your Cryptabet: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
- After making your custom cryptabet, substitute the alphabet and cryptabet into the Atbash function.
- Rename it scrambled_alphabet.
- Update the function call accordingly.
- Name your project scrambled_alphabet_cipher.
- Test to make sure that you can encrypt and decrypt this string: {‘start’ : 3, ‘after’ : ‘Liftoff! ‘}.