1. Introduction to Substitution Ciphers

The first well known cipher was a substitution cipher. It was first used around 58 BC by Emperor Julius Caesar. He shifted each letter in his military commands in order to make them seem like gibberish, should they ever be intercepted.

2. Encryption & Decryption Process

Suppose Alice and Bob need to send an encrypted message between the two of them. To do this with a substitution cipher, they must agree on a CIPHERTEXT ALPHABET, which gives the translation of every character into another set one, such that the CIPHERTEXT ALPHABET has the same number of characters as the original set of characters you are encrypting, and such that no two elements of the CIPHERTEXT ALPHABET are the same. Example:

Original Alphabet \ “ABCDEFGHIJKLMNOPQRSTUVWXYZ”

CIPHERTEXT ALPHABET \ “ZYXWVUTSRQPONMLKJIHGFEDCBA”

This is probably the easiest and most simple substitution cipher to crack. Here, each letter is replaced with the one at the opposite end of the alphabet (A->Z, B->Y, etc… Z->A)

Using this cipher, we can encode an example string “END OF THE WORLD” into “VMW LU GSV DLIOW”

It’s quite simple, though you might not be able to decrypt the encrypted string as easily, since there are 26! different substitution ciphers. But using modern computer tools, one can quite easily decode messages sent using substitution ciphers, so this essentially is rendered useless except as an easy-to-understand model and maybe to troll your teachers at school.

3. Python code to encode/decode the above case

Note that A translates into Z, and Z translates into A, etc. in the above example. This means that “VMW LU GSV DLIOW” could be encoded into “END OF THE WORLD”. So we can write a python code that both encodes/decodes strings of text as a single function switch()

This code illustrates the basic algorithm for encoding a string using substitution

def switchsub(string):
    new = '' # initialize new string
    string = string.upper() # only use uppercase
    for i in string: # take the characters one by one
        newi = chr(155-ord(i)) # use ascii symmetry. ord and chr are ascii functions
        new += newi
    return new
mystr = input("Enter your string to encode")
print(switchsub(mystr))

4. Encryption practice

Try to encrypt this message with the Substitution Cipher based on the given ciphertext alphabet!

Original Alphabet: "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

Ciphertext Alphabet: "OMGHAYDENBCFIJKLPQRSTUVWXZ"

Message to encrypt: 'ITS JOVER GUYS'

This is a shifted ciphertext alphabet where OMGHAYDEN is inserted at the front, offsetting the remainder of the characters