Python: Cryptograph using maketrans for substitution and Caesar ciphers
I’ve rewritten the functions in my previous two posts (caesar and substitution ciphers) using the maketrans function from the strings module in python (With thanks to wumzi for pointing this out).
maketrans takes an input alphabet and an output alphabet and can then be used on a string. This can be used to greatly simplify the two ciphers I produced previously. Example below:
input_alphabet="abcde" output_alphabet="12345" trantab = maketrans(input_alphabet,output_alphabet) text="translate abcdefg" print text.translate(trantab) # This will output: # tr1nsl1t5 12345fg
This nows means my code can be rewritten in just a fraction of what it was before:
from random import shuffle from string import maketrans alphabet="abcdefghijklmnopqrstuvwxyz" + \ "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"+ \ ":.;,[email protected]#$%&()+=-*/_<> []{}`~^" def translator(text,alphabet,key): trantab = maketrans(alphabet,key) return text.translate(trantab) def caesar_encode(plaintext,s,alphabet): return translator(plaintext,alphabet,alphabet[s:]+alphabet[:s]) def caesar_decode(ciphertext,s,alphabet): return translator(ciphertext,alphabet,alphabet[-s:]+alphabet[:-s]) def substitution_encode(plaintext,alphabet): randarray=range(0,len(alphabet)) shuffle(randarray) key="" for i in range(0,len(alphabet)): key+=alphabet[randarray[i]] return translator(plaintext,alphabet,key),key def substitution_decode(ciphertext,key,alphabet): return translator(ciphertext,key,alphabet) # Example useage plaintext="The wheels on the bus go round and round. round" + \ " and round. round and round. The wheels on the bus go round and"+ \ " round, all through the town!" print print "SUBSTITUTION" ciphertext,key=substitution_encode(plaintext,alphabet) print "Key: ", key print "Plaintext:", plaintext print "Cipertext:", ciphertext print "Decoded :", substitution_decode(ciphertext,key,alphabet) print print "CAESAR SHIFT" ciphertext=caesar_encode(plaintext,5,alphabet) print "Key: ", 5 print "Plaintext:", plaintext print "Cipertext:", ciphertext print "Decoded :", caesar_decode(ciphertext,5,alphabet)
This will output the following
SUBSTITUTION
Key: &fywQ.%!lmx_sRGu:{<(5jqAXvMFgk]SIY[[email protected]/ZnOJ*E>-r},BH16zUb#L?N`e7C ~9t)oW^=;h0
Plaintext: The wheels on the bus go round and round. round and round.round and round. The wheels on the bus go round and round, all through the town!
Cipertext: O!Q)q!QQ_<)GR)(!Q)f5<)%G){G5Rw)&Rw){G5Rw,){G5Rw)&Rw){G5Rw,{G5Rw)&Rw){G5Rw,)O!Q)q!QQ_<)GR)(!Q)f5<)%G){G5Rw)&Rw){G5RwH)&__)(!{G5%!)(!Q)(GqR6 Decoded : The wheels on the bus go round and round. round and round.round and round. The wheels on the bus go round and round, all through the town! CAESAR SHIFT Key: 5 Plaintext: The wheels on the bus go round and round. round and round.round and round. The wheels on the bus go round and round, all through the town! Cipertext: Ymj`2mjjqx`ts`ymj`gzx`lt`wtzsi`fsi`[email protected]`wtzsi`fsi`[email protected]`fsi`[email protected]`Ymj`2mjjqx`ts`ymj`gzx`lt`wtzsi`fsi`wtzsi$`fqq`ymwtzlm`ymj`yt2s& Decoded : The wheels on the bus go round and round. round and round.round and round. The wheels on the bus go round and round, all through the town!
Conclusion
maketrans is awesome 🙂