Making a program to encipher by substitution

 

We have just used two of the most important basic routines in python:

1. repetition:

for x in range(y):
    do something

2. conditional testing. We used above

if (message[j] >='a' and message[j]<='z'):

which means “if message[j] is greater than or equal to ‘a’ and if message[j] is less than or equal to ‘z’ “

Here are some other examples:

(a)   if x is greater than y    if ( x > y):
(b)   if x equals y               if (x==y):
(c)   if x is less than y        if (x<y):

x and y stand for numerical variables. If they stand for letters then they must be enclosed in inverted commas, as in the program.

You might like to make a few trial programs of your own with these routines to get the hang of it. For example this will print out 0 to 9:

for k in range(10):
    (print k,end="")

The little program below will print the letters of the alphabet. The computer recognizes characters as a numbered list where 'A' is at 65, 'B' at 66, ‘C’ at 67 .. and Z at 90. The line below instructs: "for the variable ‘m’ starting at 65, ending at 90 and moving forward by 1 print out the relevant character".

for m in range(65,91,1):
    (print chr(m),end="")

Similarly the lower case letters are numbered from 97 for 'a' to 122 for 'z'

Here's a little conditional program to try. If 'x' is less than 5, it will print 'less', otherwise it will print 'more'.

for x in range (10):
    if(x<5):
print ("less ",end="")
    else:
print ("more ",end="")

An enciphering program

Enciphering is done with a key which often is an easy-to-remenber word like HYPNOTIZABLE.   The rest of the alphabet is then added on, in order: HYPNOTIZABLECDFGJKMQRSUVWX.

This string, that I will call ‘key’, represents the cipher letters. It is then set against a plain alphabet to represent the plain letters, like this:

key:      HYPNOTIZABLECDFGJKMQRSUVWX
alphabet: abcdefghijklmnopqrstuvwxyz

So plain 'a' will be enciphered to 'H', plain 'b' to 'Y' and so on.

Following this method for the first 13 letters of ‘plain’

i m u s t g o d o w n t o

we get cipher letters as below:
ACRMQ IFNFU DQF

To mimic this process, the computer must

-start with an empty string called cipher
-take the first plain letter 'i'
-go through the alphabet, letter by letter, until it finds 'i'
-note the position of 'i' and get the cipher letter in the same position, which is 'A'
-add 'A' to the string 'cipher'
-repeat this process for all the other letters in the message.

The string 'cipher' will then hold the encrypted message.

Python provides a command to go through ‘alphabet’ and find the position of a letter. To find the position of ‘i’:

position=alphabet.index(‘i’)

Using this command we write the following programming lines to encipher ‘plain’:

#...declare ‘cipher’ as an empty string…
cipher = ‘’
key = 'HYPNOTIZABLECDFGJKMQRSUVWX'
alphabet='abcdefghijklmnopqrstuvwxyz'

#..for each plain letter in a string ‘length’ long..
for j in range(length):
#....find position of plain letter in alphabet
position=key.index(plain[j])
#...add corresponding key letter to ‘cipher’
cipher = cipher + key[position]

It's usual to print out ciphertext in 5-letter blocks. This is done by adding a few more lines of program code to put a space in the string after every 5 letters:

#...declare the new string ‘cipher’...
cipher = ''
#.. declare the variable ‘n’, to be used as a counter
n = 0
#..for each plain letter up to ‘length’..
for j in range(length):
#...find position of plain letter in alphabet
     position=key.index(plain[j])
#...add corresponding key letter to ‘cipher’
     cipher = cipher + key[position]
#...increment counter. If counter = 5 add a space
# and reset counter to zero
    n = n + 1
    if (n==5):
        cipher = cipher + ‘ ‘
        n=0

Here’s a new, and simpler, way of displaying the string ‘cipher’ in the Shell, and you will notice it is much faster than printing the letters one by one:

print (cipher)

When you run the complete enciphering program (Appendix 2) you will get the ciphertext:

ACRMQ IFNFU DQFQZ OMOHM HIHAD TFKQZ OPHEE FTQZO KRDDA DIQAN OAMHU AENPH EEHDN HPEOH KPHEE QZHQC HWDFQ YONOD AON


There we are, our first program is complete. You now have a simple but effective enciphering tool.

PREVIOUS          NEXT