How To Make A Substitution Cipher In Python


Monoalphanumeric cryptograms are not cryptographically secure. In fact you can solve them with a note pad and a pencil in most cases. This is for fun. A use case for something like this is obfuscating code in any language if for some reason you want to make it harder to read. But other than that, I’d suggest using a modern crypto library to encrypt your messages.

This first bit of code will generate a key.

 

#!/usr/bin/python
import ascii
import random
import string
alphabet = str(string.printable)
key = str(''.join(random.sample(alphabet,len(alphabet))))

print(key)

 

The second bit of code uses this key to encrypt a string.

 

#!/usr/bin/python
import random
import string
import ascii
import argparse

argParser = argparse.ArgumentParser()
argParser.add_argument(“-m”, “–message”, help=”Message you want to encrypt”)
argParser.add_argument(“-k”, “–key”, help=”Key to encrypt message with”)

args = argParser.parse_args()
message = args.message
key = args.key

alphabet = str(string.ascii_uppercase)

alphalist = list(alphabet)
alphakey = list(key)

upmessage=message.upper()

def encryptmessage(upmessage):
sec = []
for i in upmessage:
if i not in alphabet:
sec.append(i)
secret=”.join(sec)
else:
pos = alphalist.index(i)
s=alphakey[pos]
sec.append(s)
secret=”.join(sec)
return secret

getsecret = encryptmessage(upmessage)
print(getsecret)

 

This next bit of code will decrypt a message. Both encrypt and decrypt will use the same key.

 

#!/usr/bin/python
import random
import string
import ascii
import argparse

argParser = argparse.ArgumentParser()
argParser.add_argument(“-m”, “–message”, help=”Message you want to encrypt”)
argParser.add_argument(“-k”, “–key”, help=”Key to encrypt message with”)

args = argParser.parse_args()
message = args.message
key = args.key

alphabet = str(string.ascii_uppercase)

alphalist = list(alphabet)
alphakey = list(key)

upmessage=message.upper()

def decryptmessage(upmessage):
plaintext = []
for i in upmessage:
if i not in alphabet:
plaintext.append(i)
smessage=”.join(plaintext)
else:
pos1 = alphakey.index(i)
a=alphalist[pos1]
plaintext.append(a)
smessage=”.join(plaintext)
return smessage

newmessage = decryptmessage(message)

print(newmessage)

 

Putting it all together:

 

#!/usr/bin/python
import random
import string
import ascii
import argparse

argParser = argparse.ArgumentParser()
argParser.add_argument(“-s”, “–string”, help=”Enter the string you want to make a cryptogram from in quotes.”)

args = argParser.parse_args()
mess = args.string

alphabet = str(string.ascii_uppercase)
key=str(”.join(random.sample(alphabet,len(alphabet))))

alphalist = list(alphabet)
alphakey = list(key)

#print(alphalist)
#print(alphakey)

message=mess.upper()

def encryptmessage(message):
sec = []
for i in message:
if i not in alphabet:
sec.append(i)
secret=”.join(sec)
else:
pos = alphakey.index(i)
s=alphalist[pos]
sec.append(s)
secret=”.join(sec)
return secret
getsecret = encryptmessage(message)
print(getsecret)

def decryptmessage(getsecret):
plaintext = []
for i in getsecret:
if i not in alphabet:
plaintext.append(i)
asecret=”.join(plaintext)
else:
pos1 = alphalist.index(i)
a=alphakey[pos1]
plaintext.append(a)
asecret=”.join(plaintext)
return asecret
agetseceret = decryptmessage(getsecret)
print(agetseceret)
print(key)

 

Here’s an example of on of the cryptograms this type of program creates. They are like those crypto quips you see in the newspapers.

 

NOCGN MY NPH DTOS VMNP JDD XTCO PHJON JYS DHJY YTN TY XTCO TVY CYSHOGNJYSMYA; MY JDD XTCO VJXG GCEBMN NT PMB, JYS PH VMDD BJZH XTCO IJNPG GNOJMAPN.

 

Running the program will output the plaintext, the ciphertext, and the key.

Plaintext and key will look something like this in the output.

 

TRUST IN THE LORD WITH ALL YOUR HEART AND LEAN NOT ON YOUR OWN UNDERSTANDING; IN ALL YOUR WAYS SUBMIT TO HIM, AND HE WILL MAKE YOUR PATHS STRAIGHT. — Proverbs 3:5-6

KEY: GMULBCSEPAJQITRHZXDOFWVYNK

Pegicorn Technologies


Leave a Reply

Your email address will not be published. Required fields are marked *