Skip to content

Instantly share code, notes, and snippets.

@nyeecola
Created April 1, 2017 12:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nyeecola/3a9a7e0586001db4abda630fc756957a to your computer and use it in GitHub Desktop.
Save nyeecola/3a9a7e0586001db4abda630fc756957a to your computer and use it in GitHub Desktop.
caesar 2
text = u'Pmoi erc sxliv wsgmep qihme wmxi Jegifsso lew pirkxl viuymviqirxw alir mx gsqiw xs avmxmrk sr xli aepp, tvszmhmrk wxexyw, qiwwe kmrk erh gsqqirxmrk. Yrhivwxerhmrk lsa qerc glevegxivw csy ger ywi, irefpiw csy xs qsvi ijjigxmzipc ywi Jegifsso ew e fywmriww sv geqtem kr xssp. Tvmzexi qiwwekmrk mw sri sj xli qemr aecw xlex tistpi mrxivegx sr Jegifsso. Xlmw xcti sj hmvigx qiwwekmrk ger fi imxliv er mrw xerx qiwweki (glex), sv e vikypev iqemp-xcti qiwweki. Jsv fsxl mrwxerx erh vikypev qiwwekmrk, xlivi mw e 20,000 glevegxiv pmqmx. E Jegi fsso wxexyw qec lezi glevegxiv pmqmxw, fyx gsrwmhivmrk xlex mx mw ex 63,206 glevegxivw, yrpiww csy evi avmxmrk Aev erh Tiegi, csy wlsyph fi jmri. Jegifsso lew vemwih xlmw ryqfiv 12 xmqiw xs eggsqqshexi ywivw wxexyw erh jiihfego. Jegifsso aepp tswxw lezi e 5000 glevegxiv pmqmx, fyx xvyrgexmsr fikmrw ex 477 glevegxivw. Xlmw irefpiw wsqisri xs avmxi e xlsyklxjyp viwtsrwi sv gviexi wsqixlmrk wmqmpev xs e fps k. Er evie almgl mw vevipc ywih mr Jegifsso mw xli Rsxiw wigxmsr. Mx mw e avmxmrk evie almgl qerc fpskkivw jmrh ywijyp. Xli viewsr mw f igeywi Jegifsso Rsxiw hsiw rsx lezi e glevegxiv pmqmx, ew sj cix. Ywivw evi hmvigxih xs xlmw evie mj xlic lezi zivc psrk wxexyw sv gsqqi rx xs qeoi. Xlmw ger fi ywih xs wsqisri\'w ehzerxeki mj xlic lezi e psrkiv tswx xs qeoi erh amwl xs wlevi mx amxl xlimv jvmirhw xlvsykl xekkmrk. Ex Jegifsso, ywivw ytpseh erh tswx ria tlsxsw izivc hec. Alir ytpsehmrk tmgxyviw, ywivw qec riih xs avmxi e hiwgvmtxmsr, almgl lew e 5000 glevegxiv pmqmx. Srgi ytpsehih, jia jvmirhw ampp gsqqirx efsyx xli tswx, almgl wlsyph kmzi xliq e glevegxiv pmqmx sj 8000 gl evegxivw.'
vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']
attempts = []
guessed = False
# there are 26 possible ceasar's cyphers "variations"
for i in range(0, 26):
# shift letters `i` times to the right
dec_text = ''
for c in text:
if ord(c) >= ord('a') and ord(c) <= ord('z'):
dec_text += chr((((ord(c) + i) - ord('a')) % 26) + ord('a'))
elif ord(c) >= ord('A') and ord(c) <= ord('Z'):
dec_text += chr((((ord(c) + i) - ord('A')) % 26) + ord('A'))
else:
dec_text += c
# count groups of 3 or more consonants to estimate the likelihood of the text being right
consecutive_consonants = 0
consonant_factor = 0
for c in dec_text:
if ((c in vowels) == True) or ((ord(c) < ord('a')) and (ord(c) > ord('Z'))) or (ord(c) > ord('z')) or (ord(c) < ord('A')):
consecutive_consonants = 0
continue
consecutive_consonants += 1
if consecutive_consonants >= 3:
consonant_factor += 1
consecutive_consonants = 0
# structure the data obtained
result = {
'dec_text': dec_text,
'consonant_factor': consonant_factor
}
# try to guess if the text was decrypted successfully by divinding the consonant_factor
# by the consonant factor of a previous attempt, if one is 0.4 times the other or less
# it means that there are very few groups of consonants and that the decryption was probably
# successful.
if attempts:
a = float(result['consonant_factor'])
b = float(attempts[0]['consonant_factor'])
if a / b < 0.4:
guess = result['dec_text']
guessed = True
break
if b / a < 0.4:
guess = attempts[0]['dec_text']
guessed = True
break
# if no guess could be made so far, store the results and move on
# it is important to notice that here the attempts are stored in order of most likely to
# be right to less likely to be right
index = 0
for attempt in attempts:
if attempt['consonant_factor'] > result['consonant_factor']:
break
index += 1
attempts.insert(index, result)
# output
print
# made a guess? print it
if guessed == True:
print guess
print
# otherwise, print the 5 best attempts
else:
for i in range(0, 5):
print attempts[i]['dec_text'], attempts[i]['consonant_factor']
print
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment