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"+ \
":.;,?!@#$%&()+=-*/_<> []{}`~^"
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[4iK+3P8pcV$2da@DT/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`wtzsi@`wtzsi`fsi`wtzsi@wtzsi`fsi`wtzsi@`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


Whaoh looks great !
Just add a caesar cracker those who wants to spy conversations crypted with caesar and it will be fine
Have a good Christmas.
Anyone crack this code?
020 102 202 171
225 071 805 062
403 100 501 100
309 100 505 121
225 200 700 261
228 140 119 160
110 130 004 012
410 032 810 270
014 111 618 091
005 181 810 002
205 121 201 051
002 131 114 070
Well a quick google shows that this code is worth money to decode:
“I have been challenged by a person on face-book to solve a code for £100.”
Source:http://uk.answers.yahoo.com/question/index?qid=20100401144917AA93yXO
Unfortunately the first task you need to do is identify which kinda of cipher has been used, and also to work out if the spacing is there to help you or put you off, the code may be read horizontally, vertically or in a spiral, thereby increasing the complexity of the decoding. There is not enough text to use frequency and repetition analysis.
Well thank you, but i am still none the wiser…..
£100 for anyone cracks it.
All i want is the credit!!