Skip to content

Instantly share code, notes, and snippets.

@messa
Created Apr 7, 2017
Embed
What would you like to do?
#!/usr/bin/env python3
ciphertext = '''
ymjwj fwj ybt ydujx tk jshwduynts: tsj ymfy bnqq
uwjajsy dtzw xnxyjw kwtr wjfinsl dtzw infwd fsi tsj ymfy bnqq
uwjajsy dtzw ltajwsrjsy. ymnx nx f ajwd nrutwyfsy qjxxts yt
wjrjgjwjxujhnfqqd ktw fyyfhpx zxnsl kwjvzjshd fsfqdxnx bmnhm
wjvznwj qtsljw ufxxflj tk yjcy ns twijw yt fhmnjaj gjyyjw wjxzqyx.
'''
alphabet = 'abcdefghijklmnopqrstuvwxyz'
def get_letter_counts(text):
counts = {c: 0 for c in alphabet}
for letter in text.lower():
if letter in alphabet:
counts[letter] += 1
return counts
assert get_letter_counts('x')['a'] == 0
assert get_letter_counts('aa')['a'] == 2
def get_letter_frequencies(text):
counts = get_letter_counts(text)
total = max(counts.values())
return {letter: count/total for letter, count in counts.items()}
english_frequencies = [0.64297,0.11746,0.21902,0.33483,1.00000,0.17541,
0.15864,0.47977,0.54842,0.01205,0.06078,0.31688,0.18942,
0.53133,0.59101,0.15187,0.00748,0.47134,0.49811,0.71296,
0.21713,0.07700,0.18580,0.01181,0.15541,0.00583]
assert len(english_frequencies) == len(alphabet)
english_frequencies = {letter: freq for letter, freq in zip(alphabet, english_frequencies)}
def rotate_text(text, offset):
return ''.join(rotate_letter(letter, offset) for letter in text)
def rotate_letter(letter, offset):
letter = letter.lower()
if letter in alphabet:
n = ord(letter) + offset
if n > ord('z'):
n -= ord('z') - ord('a') + 1
return chr(n)
else:
return letter
assert rotate_letter('a', 0) == 'a'
assert rotate_letter('a', 1) == 'b'
assert rotate_letter('z', 0) == 'z'
assert rotate_letter('z', 1) == 'a'
assert rotate_letter('z', 2) == 'b'
best_offset = 0
best_offset_distance = None
for offset in range(25):
print('-' * 80)
print('Offset:', offset)
rotated_text = rotate_text(ciphertext, offset)
print('Text:', rotated_text)
rotated_freqs = get_letter_frequencies(rotated_text)
#print(rotated_freqs)
print('Frequency distance - sum:', sum(abs(english_frequencies[letter] - rotated_freqs[letter]) for letter in alphabet))
print('Frequency distance - quadratic:', sum((english_frequencies[letter] - rotated_freqs[letter])**2 for letter in alphabet))
distance = sum((english_frequencies[letter] - rotated_freqs[letter])**2 for letter in alphabet)
if best_offset_distance is None or distance < best_offset_distance:
best_offset = offset
best_offset_distance = distance
plaintext = rotate_text(ciphertext, best_offset)
print()
print('Best plaintext:', plaintext)
--------------------------------------------------------------------------------
Offset: 0
Text:
ymjwj fwj ybt ydujx tk jshwduynts: tsj ymfy bnqq
uwjajsy dtzw xnxyjw kwtr wjfinsl dtzw infwd fsi tsj ymfy bnqq
uwjajsy dtzw ltajwsrjsy. ymnx nx f ajwd nrutwyfsy qjxxts yt
wjrjgjwjxujhnfqqd ktw fyyfhpx zxnsl kwjvzjshd fsfqdxnx bmnhm
wjvznwj qtsljw ufxxflj tk yjcy ns twijw yt fhmnjaj gjyyjw wjxzqyx.
Frequency distance - sum: 7.521892857142856
Frequency distance - quadratic: 4.1179784738551035
--------------------------------------------------------------------------------
Offset: 1
Text:
znkxk gxk zcu zevky ul ktixevzout: utk zngz corr
vxkbktz euax yoyzkx lxus xkgjotm euax jogxe gtj utk zngz corr
vxkbktz euax mubkxtsktz. znoy oy g bkxe osvuxzgtz rkyyut zu
xkskhkxkyvkiogrre lux gzzgiqy ayotm lxkwaktie gtgreyoy cnoin
xkwaoxk rutmkx vgyygmk ul zkdz ot uxjkx zu ginokbk hkzzkx xkyarzy.
Frequency distance - sum: 7.825549999999999
Frequency distance - quadratic: 3.7698087595693877
--------------------------------------------------------------------------------
Offset: 2
Text:
aolyl hyl adv afwlz vm lujyfwapvu: vul aoha dpss
wylclua fvby zpzaly myvt ylhkpun fvby kphyf huk vul aoha dpss
wylclua fvby nvclyutlua. aopz pz h clyf ptwvyahua slzzvu av
yltlilylzwljphssf mvy haahjrz bzpun mylxblujf huhsfzpz dopjo
ylxbpyl svunly whzzhnl vm alea pu vykly av hjoplcl ilaaly ylzbsaz.
Frequency distance - sum: 7.269072857142857
Frequency distance - quadratic: 3.594043045283673
--------------------------------------------------------------------------------
Offset: 3
Text:
bpmzm izm bew bgxma wn mvkzgxbqwv: wvm bpib eqtt
xzmdmvb gwcz aqabmz nzwu zmilqvo gwcz lqizg ivl wvm bpib eqtt
xzmdmvb gwcz owdmzvumvb. bpqa qa i dmzg quxwzbivb tmaawv bw
zmumjmzmaxmkqittg nwz ibbiksa caqvo nzmycmvkg ivitgaqa epqkp
zmycqzm twvomz xiaaiom wn bmfb qv wzlmz bw ikpqmdm jmbbmz zmactba.
Frequency distance - sum: 8.430735714285714
Frequency distance - quadratic: 4.222971045283674
--------------------------------------------------------------------------------
Offset: 4
Text:
cqnan jan cfx chynb xo nwlahycrxw: xwn cqjc fruu
yanenwc hxda brbcna oaxv anjmrwp hxda mrjah jwm xwn cqjc fruu
yanenwc hxda pxenawvnwc. cqrb rb j enah rvyxacjwc unbbxw cx
anvnknanbynlrjuuh oxa jccjltb dbrwp oanzdnwlh jwjuhbrb fqrlq
anzdran uxwpna yjbbjpn xo cngc rw xamna cx jlqrnen knccna anbducb.
Frequency distance - sum: 6.820435714285715
Frequency distance - quadratic: 3.207523045283674
--------------------------------------------------------------------------------
Offset: 5
Text:
drobo kbo dgy dizoc yp oxmbizdsyx: yxo drkd gsvv
zbofoxd iyeb cscdob pbyw boknsxq iyeb nskbi kxn yxo drkd gsvv
zbofoxd iyeb qyfobxwoxd. drsc sc k fobi swzybdkxd voccyx dy
bowoloboczomskvvi pyb kddkmuc ecsxq pboaeoxmi kxkvicsc grsmr
boaesbo vyxqob zkcckqo yp dohd sx ybnob dy kmrsofo loddob bocevdc.
Frequency distance - sum: 7.568612857142858
Frequency distance - quadratic: 3.4733264738551024
--------------------------------------------------------------------------------
Offset: 6
Text:
espcp lcp ehz ejapd zq pyncjaetzy: zyp esle htww
acpgpye jzfc dtdepc qczx cplotyr jzfc otlcj lyo zyp esle htww
acpgpye jzfc rzgpcyxpye. estd td l gpcj txazcelye wpddzy ez
cpxpmpcpdapntlwwj qzc leelnvd fdtyr qcpbfpynj lylwjdtd hstns
cpbftcp wzyrpc alddlrp zq epie ty zcopc ez lnstpgp mpeepc cpdfwed.
Frequency distance - sum: 7.005664285714287
Frequency distance - quadratic: 2.9026967595693884
--------------------------------------------------------------------------------
Offset: 7
Text:
ftqdq mdq fia fkbqe ar qzodkbfuaz: azq ftmf iuxx
bdqhqzf kagd euefqd rday dqmpuzs kagd pumdk mzp azq ftmf iuxx
bdqhqzf kagd sahqdzyqzf. ftue ue m hqdk uybadfmzf xqeeaz fa
dqyqnqdqebqoumxxk rad mffmowe geuzs rdqcgqzok mzmxkeue ituot
dqcgudq xazsqd bmeemsq ar fqjf uz adpqd fa motuqhq nqffqd dqegxfe.
Frequency distance - sum: 7.862478571428572
Frequency distance - quadratic: 3.577721330997959
--------------------------------------------------------------------------------
Offset: 8
Text:
gurer ner gjb glcrf bs rapelcgvba: bar gung jvyy
cerirag lbhe fvfgre sebz ernqvat lbhe qvnel naq bar gung jvyy
cerirag lbhe tbireazrag. guvf vf n irel vzcbegnag yrffba gb
erzrorerfcrpvnyyl sbe nggnpxf hfvat serdhrapl nanylfvf juvpu
erdhver ybatre cnffntr bs grkg va beqre gb npuvrir orggre erfhygf.
Frequency distance - sum: 5.984130000000002
Frequency distance - quadratic: 2.2154259024265306
--------------------------------------------------------------------------------
Offset: 9
Text:
hvsfs ofs hkc hmdsg ct sbqfmdhwcb: cbs hvoh kwzz
dfsjsbh mcif gwghsf tfca fsorwbu mcif rwofm obr cbs hvoh kwzz
dfsjsbh mcif ucjsfbasbh. hvwg wg o jsfm wadcfhobh zsggcb hc
fsaspsfsgdsqwozzm tcf ohhoqyg igwbu tfseisbqm obozmgwg kvwqv
fseiwfs zcbusf doggous ct hslh wb cfrsf hc oqvwsjs pshhsf fsgizhg.
Frequency distance - sum: 7.499189999999999
Frequency distance - quadratic: 3.326054473855102
--------------------------------------------------------------------------------
Offset: 10
Text:
iwtgt pgt ild ineth du tcrgneixdc: dct iwpi lxaa
egtktci ndjg hxhitg ugdb gtpsxcv ndjg sxpgn pcs dct iwpi lxaa
egtktci ndjg vdktgcbtci. iwxh xh p ktgn xbedgipci athhdc id
gtbtqtgthetrxpaan udg piiprzh jhxcv ugtfjtcrn pcpanhxh lwxrw
gtfjxgt adcvtg ephhpvt du itmi xc dgstg id prwxtkt qtiitg gthjaih.
Frequency distance - sum: 6.042607142857142
Frequency distance - quadratic: 2.4125927595693875
--------------------------------------------------------------------------------
Offset: 11
Text:
jxuhu qhu jme jofui ev udshofjyed: edu jxqj mybb
fhuludj oekh iyijuh vhec huqtydw oekh tyqho qdt edu jxqj mybb
fhuludj oekh weluhdcudj. jxyi yi q luho ycfehjqdj buiied je
hucuruhuifusyqbbo veh qjjqsai kiydw vhugkudso qdqboiyi mxysx
hugkyhu bedwuh fqiiqwu ev junj yd ehtuh je qsxyulu rujjuh huikbji.
Frequency distance - sum: 7.203041428571428
Frequency distance - quadratic: 3.2265190452836734
--------------------------------------------------------------------------------
Offset: 12
Text:
kyviv riv knf kpgvj fw vetipgkzfe: fev kyrk nzcc
givmvek pfli jzjkvi wifd ivruzex pfli uzrip reu fev kyrk nzcc
givmvek pfli xfmviedvek. kyzj zj r mvip zdgfikrek cvjjfe kf
ivdvsvivjgvtzrccp wfi rkkrtbj ljzex wivhlvetp rercpjzj nyzty
ivhlziv cfexvi grjjrxv fw kvok ze fiuvi kf rtyzvmv svkkvi ivjlckj.
Frequency distance - sum: 7.577392857142856
Frequency distance - quadratic: 3.7596739024265315
--------------------------------------------------------------------------------
Offset: 13
Text:
lzwjw sjw log lqhwk gx wfujqhlagf: gfw lzsl oadd
hjwnwfl qgmj kaklwj xjge jwsvafy qgmj vasjq sfv gfw lzsl oadd
hjwnwfl qgmj ygnwjfewfl. lzak ak s nwjq aehgjlsfl dwkkgf lg
jwewtwjwkhwuasddq xgj sllsuck mkafy xjwimwfuq sfsdqkak ozauz
jwimajw dgfywj hskksyw gx lwpl af gjvwj lg suzawnw twllwj jwkmdlk.
Frequency distance - sum: 8.041958571428571
Frequency distance - quadratic: 4.087395045283674
--------------------------------------------------------------------------------
Offset: 14
Text:
maxkx tkx mph mrixl hy xgvkrimbhg: hgx matm pbee
ikxoxgm rhnk lblmxk ykhf kxtwbgz rhnk wbtkr tgw hgx matm pbee
ikxoxgm rhnk zhoxkgfxgm. mabl bl t oxkr bfihkmtgm exllhg mh
kxfxuxkxlixvbteer yhk tmmtvdl nlbgz ykxjnxgvr tgterlbl pabva
kxjnbkx ehgzxk itlltzx hy mxqm bg hkwxk mh tvabxox uxmmxk kxlneml.
Frequency distance - sum: 7.398490000000001
Frequency distance - quadratic: 3.6256921881408157
--------------------------------------------------------------------------------
Offset: 15
Text:
nbyly uly nqi nsjym iz yhwlsjncih: ihy nbun qcff
jlypyhn siol mcmnyl zlig lyuxcha siol xculs uhx ihy nbun qcff
jlypyhn siol aipylhgyhn. nbcm cm u pyls cgjilnuhn fymmih ni
lygyvylymjywcuffs zil unnuwem omcha zlykoyhws uhufsmcm qbcwb
lykocly fihayl jummuay iz nyrn ch ilxyl ni uwbcypy vynnyl lymofnm.
Frequency distance - sum: 6.594730000000001
Frequency distance - quadratic: 3.4117150452836746
--------------------------------------------------------------------------------
Offset: 16
Text:
oczmz vmz orj otkzn ja zixmtkodji: jiz ocvo rdgg
kmzqzio tjpm ndnozm amjh mzvydib tjpm ydvmt viy jiz ocvo rdgg
kmzqzio tjpm bjqzmihzio. ocdn dn v qzmt dhkjmovio gznnji oj
mzhzwzmznkzxdvggt ajm voovxfn pndib amzlpzixt vivgtndn rcdxc
mzlpdmz gjibzm kvnnvbz ja ozso di jmyzm oj vxcdzqz wzoozm mznpgon.
Frequency distance - sum: 7.350655714285715
Frequency distance - quadratic: 3.876038473855102
--------------------------------------------------------------------------------
Offset: 17
Text:
pdana wna psk pulao kb ajynulpekj: kja pdwp sehh
lnarajp ukqn oeopan bnki nawzejc ukqn zewnu wjz kja pdwp sehh
lnarajp ukqn ckranjiajp. pdeo eo w ranu eilknpwjp haookj pk
naiaxanaolayewhhu bkn wppwygo qoejc bnamqajyu wjwhuoeo sdeyd
namqena hkjcan lwoowca kb patp ej knzan pk wydeara xappan naoqhpo.
Frequency distance - sum: 6.301461428571429
Frequency distance - quadratic: 2.388451045283674
--------------------------------------------------------------------------------
Offset: 18
Text:
qebob xob qtl qvmbp lc bkzovmqflk: lkb qexq tfii
mobsbkq vlro pfpqbo colj obxafkd vlro afxov xka lkb qexq tfii
mobsbkq vlro dlsbokjbkq. qefp fp x sbov fjmloqxkq ibpplk ql
objbybobpmbzfxiiv clo xqqxzhp rpfkd cobnrbkzv xkxivpfp tefze
obnrfob ilkdbo mxppxdb lc qbuq fk loabo ql xzefbsb ybqqbo obpriqp.
Frequency distance - sum: 8.495872857142858
Frequency distance - quadratic: 4.056658473855103
--------------------------------------------------------------------------------
Offset: 19
Text:
rfcpc ypc rum rwncq md clapwnrgml: mlc rfyr ugjj
npctclr wmsp qgqrcp dpmk pcybgle wmsp bgypw ylb mlc rfyr ugjj
npctclr wmsp emtcplkclr. rfgq gq y tcpw gknmprylr jcqqml rm
pckczcpcqncagyjjw dmp yrryaiq sqgle dpcosclaw ylyjwqgq ufgaf
pcosgpc jmlecp nyqqyec md rcvr gl mpbcp rm yafgctc zcrrcp pcqsjrq.
Frequency distance - sum: 8.05287
Frequency distance - quadratic: 3.928586473855103
--------------------------------------------------------------------------------
Offset: 20
Text:
sgdqd zqd svn sxodr ne dmbqxoshnm: nmd sgzs vhkk
oqdudms xntq rhrsdq eqnl qdzchmf xntq chzqx zmc nmd sgzs vhkk
oqdudms xntq fnudqmldms. sghr hr z udqx hlonqszms kdrrnm sn
qdldadqdrodbhzkkx enq zsszbjr trhmf eqdptdmbx zmzkxrhr vghbg
qdpthqd knmfdq ozrrzfd ne sdws hm nqcdq sn zbghdud adssdq qdrtksr.
Frequency distance - sum: 6.754481428571427
Frequency distance - quadratic: 3.3539139024265308
--------------------------------------------------------------------------------
Offset: 21
Text:
there are two types of encryption: one that will
prevent your sister from reading your diary and one that will
prevent your government. this is a very important lesson to
remeberespecially for attacks using frequency analysis which
require longer passage of text in order to achieve better results.
Frequency distance - sum: 1.951481428571429
Frequency distance - quadratic: 0.30071675956938776
--------------------------------------------------------------------------------
Offset: 22
Text:
uifsf bsf uxp uzqft pg fodszqujpo: pof uibu xjmm
qsfwfou zpvs tjtufs gspn sfbejoh zpvs ejbsz boe pof uibu xjmm
qsfwfou zpvs hpwfsonfou. uijt jt b wfsz jnqpsubou mfttpo up
sfnfcfsftqfdjbmmz gps buubdlt vtjoh gsfrvfodz bobmztjt xijdi
sfrvjsf mpohfs qbttbhf pg ufyu jo psefs up bdijfwf cfuufs sftvmut.
Frequency distance - sum: 7.745590000000001
Frequency distance - quadratic: 3.567283616712244
--------------------------------------------------------------------------------
Offset: 23
Text:
vjgtg ctg vyq vargu qh gpetarvkqp: qpg vjcv yknn
rtgxgpv aqwt ukuvgt htqo tgcfkpi aqwt fkcta cpf qpg vjcv yknn
rtgxgpv aqwt iqxgtpogpv. vjku ku c xgta korqtvcpv nguuqp vq
tgogdgtgurgekcnna hqt cvvcemu wukpi htgswgpea cpcnauku yjkej
tgswktg nqpigt rcuucig qh vgzv kp qtfgt vq cejkgxg dgvvgt tguwnvu.
Frequency distance - sum: 7.889132857142859
Frequency distance - quadratic: 3.642691045283673
--------------------------------------------------------------------------------
Offset: 24
Text:
wkhuh duh wzr wbshv ri hqfubswlrq: rqh wkdw zloo
suhyhqw brxu vlvwhu iurp uhdglqj brxu gldub dqg rqh wkdw zloo
suhyhqw brxu jryhuqphqw. wklv lv d yhub lpsruwdqw ohvvrq wr
uhphehuhvshfldoob iru dwwdfnv xvlqj iuhtxhqfb dqdobvlv zklfk
uhtxluh orqjhu sdvvdjh ri whaw lq rughu wr dfklhyh ehwwhu uhvxowv.
Frequency distance - sum: 7.6764100000000015
Frequency distance - quadratic: 3.701904759569388
Best plaintext:
there are two types of encryption: one that will
prevent your sister from reading your diary and one that will
prevent your government. this is a very important lesson to
remeberespecially for attacks using frequency analysis which
require longer passage of text in order to achieve better results.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment