How to create a cryptogram in python (random substitution cipher)
Cryptograms are enjoyable puzzles created from a saying or phrase encrypted with a substitutional cipher. They can be fun to decipher by hand by looking for common letter combinations, doublets, guesswork, and other flaws in this encryption mechanism.
I wrote a quick python script which will accept an input text and create a random substitutional cipher and encrypt it. It then outputs the cipher alphabet and the encrypted text.
# -*- coding: utf-8 -*- import sys from random import randint from string import maketrans if (len(sys.argv)>1): # Normal alphabet alphabet="abcdefghijklmnopqrstuvwxyz" # Randomly create a new cipherbet cipherbet="" left=alphabet for i in range(0,len(alphabet)): x=randint(0,len(left)-1) cipherbet+=left[x] left=left[:x]+left[x+1:] # Get input text to translate text=sys.argv.lower() trantab = maketrans(alphabet,cipherbet) text=text.translate(trantab) # Replace unused letters in cipherbet with _'s for i in cipherbet: if i not in text: cipherbet=cipherbet.replace(i,"_") # Print cipherbet (solution) and the text (cryptogram) print cipherbet print text
python create_cipher.py “The Science gets done. And you make a neat gun. For the people who are still alive.”
dri zlmixli vidz puxi. bxp oug qbai b xibd vgx. euf dri jiujci nru bfi zdmcc bcmwi.