Appendix 1: Entering the message to encipher:

message= '''I must go down to the seas again, for the call of the
running tide is a wild call, and a clear call, that
may not be denied.'''
length=len(message)
message=message.lower()
plain=''
for j in range(length):
if(message[j]>='a' and message[j]<='z'):
plain=plain+message[j]
length=len(plain)
print("plain = ",end=" ")
for j in range(length):
print(plain[j],end="")
print()
print("length=",length,end=" ")


Appendix 2: Encipher the message

message= '''I must go down to the seas again, for the call of the
running tide is a wild call, and a clear call, that
may not be denied.'''
length=len(message)
message=message.lower()
plain=''
for j in range(length):
if(message[j]>='a' and message[j]<='z'):
plain=plain+message[j]
length=len(plain)
print("plain = ",end=" ")
for j in range(length):
print(plain[j],end="")
print()
print("length=",length,end=" ")
print()
cipher = ''
key = 'HYPNOTIZABLECDFGJKMQRSUVWX'
alphabet='abcdefghijklmnopqrstuvwxyz'
n = 0
for j in range(length):
position=alphabet.index(plain[j])
cipher = cipher + key[position]
n = n + 1
if (n==5):
cipher = cipher + ' '
n=0
print ("cipher = ",cipher)

 

Appendix 3: Decipher the message

cipher= 'ACRMQ IFNFU DQFQZ OMOHM HIHAD TFKQZ OPHEE FTQZO KRDDA DIQAN \
OAMHU AENPH EEHDN HPEOH KPHEE QZHQC HWDFQ YONOD AON'
length=len(cipher)
print ("cipher = ", cipher)
print
key = 'HYPNOTIZABLECDFGJKMQRSUVWX'
alphabet = 'abcdefghijklmnopqrstuvwxyz'
#..remove spaces from ciphertext..
cipher_no_spaces=''
for j in range(length):
if(cipher[j]>='A' and cipher[j]<='Z'):
cipher_no_spaces=cipher_no_spaces+cipher[j]
length = len(cipher_no_spaces)
plain=''
#..replace each cipher letter with matching plain letter
for j in range(length):
position = key.index(cipher_no_spaces[j])
plain = plain + alphabet[position]
print(plain)



Appendix 4: Use of functions

def remove_spaces(message):
plain=''
message=message.lower()
length=len(message)
for j in range(length):
if(message[j]>='a' and message[j]<='z'):
plain = plain + message[j]
length = len(plain)
return(plain)

def get_key():
got_key=0
while got_key==0:
key = input("Enter the key & press Return ");
key=key.upper()
got_key=1
for j in range(len(key)):
if(key[j]<'A' or key[j]>'Z'):
got_key=0
return(key)

def extend_key(key):
length_key=len(key)
for j in range(26):
present=0
for k in range(length_key):
if(alphabet[j].upper()==key[k]):
present=1
if(present==0):
key=key+alphabet[j].upper()
return (key)

def encipher(plain,key):
n = 0
length=len(plain)
cipher=''
for j in range(length):
position=alphabet.index(plain[j])
cipher = cipher + key[position]
n = n + 1
if (n==5):
cipher = cipher + ' '
n=0
return(cipher)

#..MAIN PART OF PROGRAM...
alphabet = 'abcdefghijklmnopqrstuvwxyz'
message= "I must go down to the sea again"
plain=remove_spaces(message)
key=get_key()
key=extend_key(key)
cipher=encipher(plain,key)
print ('message = ',message)
print ('plain = ',plain)
print ('key = ', key)
print ('cipher=',cipher)

PREVIOUS