Simple Substitution Cipher

Simple Substitution Cipher differs from Caesar Cipher in that the cipher alphabet is not simply shifted, it uses a look up table (the key book) to find the encrypted alphabet.

The following python program shows how the Simple Substitution Cipher works.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import random
import copy

key = [chr(y + ord('a')) for y in range(26)]
alphalist = copy.deepcopy(key)
random.shuffle(key)

print("key -", " ".join(key))
def simplecipher(input, encrypt = True):
    global key, alphalist
    d = {}
    for i in range(len(alphalist)):
        if encrypt:
            d[alphalist[i]] = key[i]
        else:
            d[key[i]] = alphalist[i]
    output = ""
    for z in input:
        if z == " ":
            output += " "
        else:
            if z.isupper():
                output += d[z.lower()].upper()
            else:
                output += d[z.lower()]
    return output


plain = "This will be encrypted and decrypted"
ciphertext = simplecipher(plain)
decryptedtext = simplecipher(ciphertext, False)
print('ciphertext =', ciphertext)
print("decryptedtext =", decryptedtext)