--- /dev/null
+{
+ "cells": [
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "import os,sys,inspect, collections\n",
+ "currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))\n",
+ "parentdir = os.path.dirname(currentdir)\n",
+ "sys.path.insert(0,parentdir) \n",
+ "\n",
+ "import matplotlib.pyplot as plt\n",
+ "%matplotlib inline\n",
+ "\n",
+ "from cipherbreak import *\n",
+ "\n",
+ "ca = open('6a.ciphertext').read()\n",
+ "cb = open('6b.ciphertext').read()"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 2,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": [
+ "def tpack(text, width=100):\n",
+ " lines = [text[0]]\n",
+ " for word in text[1:]:\n",
+ " if len(lines[-1]) + 1 + len(word) <= width:\n",
+ " lines[-1] += (' ' + word)\n",
+ " else:\n",
+ " lines += [word]\n",
+ " return lines"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 4,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "JDPWD JGLOF YBDQA LKYFD PPFTY OFKQY ICSHD QMIDK KYOES RQBYO FOKRJ DHYRB YPYKO YXTLS QRBYI LCDIQ RDRFL KRBFK HQBYJ DWBDT YEYYK HFOKD MMYOE SRRBY PYBDQ EYYKK LPDKQ LJOYJ DKODK ORBYP YFQKL RBFKA JSCBR LALLK BYPPL LJBDO EYYKP DKQDC HYODK OQBYI YZRKL JYQQD AYESR QLJYR BFKAR YIIQJ YQBYF QLHDK OUYQB LSIOJ LTYLK FRBFK HQBYJ DWBDT YMIDK KYORB FQJYD KUBFI YFBDT YRBYO LCSJY KRGLO FYUDK RYOZP LJILK OLKFO LKRHK LUBLU QBYHK YUESR RBYZF ZRBCB DMRYP UDQBF OOYKF KDCDT FRWFK DQRLK YIFKR YIFKQ LJYPY JDFKQ FKRBY YMBYQ SQPLL JDRRB YEPFR FQBJS QYSJF RUDQY KCPWM RYOSQ FKADK LRBYP MLIWD IMBDE YRFCC FMBYP FKTYK RYOFK RBYKF KYRYY KRBCY KRSPW FDQHY ORBYJ SQYSJ RLRYQ RFRDK ORBYO LCSJY KRFQD SRBYK RFCQL FASYQ QRBYF JMYPF DICFM BYPQC BLLIP YDIIW BDOJL TYOLK MYPBD MQFQB LSIOK RBDTY EYYKQ SPMPF QYODZ RYPRB YOFQC LTYPW DRELO PSJES RFRIY DTYQJ YULKO YPFKA UBDRY IQYRB YWOYT YILMY OLKYL OOZYD RSPYU DQDZP FYXYP SKKFK APLSK ORBPY YQFOY QLZRB YIFKR YICLK QFQRF KALZD QFKAI YPLUL ZEIDC HDKOU BFRYR FIYQR BDRRB YJSQY SJRBF KHUYP YDOOY OFKRB YPLJD KMYPF LORBY WPYDO FKQYN SYKCY UUEEU UUEUU UEUUU EUUUE UUUEU UUEUU UEUUU EUUUE UUUEU UUEUU UEUUU EUUUE UUUEU UUEUU UEUUU EUUUE EUUUE UUEUE UEUEU EUEUE UEUEU EUEUE UEUEU EUEUE UEUEU EUEUE UEUEU EUEUE UEUEU EUEUE UEUEU EUEUE UEUUE UUUEU EEEUE EEUEE EUEEE UEEEU EEEUE EEUEE EUEEE UEEEU EEEUE EEUEE EUEEE UEEEU EEEUE EEUEE EUEEE UEUUU EUUUE UUUEU UUEUU UEUUU EUUUE UUUEU UUEUU UEUUU EUUUE UUUEU UUEUU UEUUU EUUUE UUUEU UUEUU UEUUU EUUUE UUUUU UUUUU UUUUU UUUUU UUUUU UUUUU UUUUU UUUUU UUUUU UUUUU UUUUU UUUUU UUUUU UUUUU UUUUU UUEUU UEUUU UUUUU UUUUU UUUUU UUUUU UUUUU UUUUU UUUUU UUUUU UUUUU UUUUU UUUUU UUUUU UUUUU UUUUU UUUUU UEUUU EUUUU EUUUE EEUUU UUEEE UUUEU UUEUU UEUUU EUUUU EEEUU EUUUE UEEEU UUEEE EUUEE EUUUE EEUUE EEUEU UUEUU UUEUE UUEUU EUUUE UUUUU EUEUU EEUUE UUEUE UUUUE UUUEE UEEUE UUEUU EUUUU UEUUE UUUEU UUUEU UUEUU UEUUE UUUEU EUUUE UEUUU UUEUU UEUEU EUEUE UUUEU UUEUU UEUEU EUEUU EUUEU UUUUE UUUEU UEUUU UEUUE UUUEU UUEEE EEUEE EEUUE UUUUU EEEEE UEUEU EUEEE EEUUU EUUUE UUUEU EEEEU UEEEU UUEEE EUUUE UUUUE UUUEU UUEUU EUUUE UEUEU UUEUU UUUEU UUEUE UEUEU EUUUE UUUEU UUEUU UEUEU UUUUE UUUUU EUEUU UUEUU UUEUU EUUUE UUUEU UUEUE UUEUU UEUUU UEUUU EUEUU EEUEU UUEUU UEUUU EUUUE UEUUU UUEUU UUUEU UEUUU EUUUU EUUUE UUUEU UEUUU EUEUU UEUUU EEEUE UUUEU EUUUE UEUUU EUUEE EUUEU UUEUE UUUUU EEEEU UEUUU EUEEE UUEEE UEUUU EUUUU UUUUU UUUUU UUUUU UUUUU UUUUU UUUUU UUUUU UUUUU UUUUU UUUUU UUUUU UUUUU UUUUU UUUUU UUUUU EUUUE UUUUU UUUUU UUUUU UUUUU UUUUU UUUUU UUUUU UUUUU UUUUU UUUUU UUUUU UUUUU UUUUU UUUUU UUUUU UUEUU UEUEU UUEUU UEUUU EUUUE UUUEU UUEUU UEUUU EUUUE UUUEU UUEUU UEUUU EUUUE UUUEU UUEUU UEUUU EUUUE UEUUU EUEUE UEUEU EUEUE UEUEU EUEUE UEUEU EUEUE UEUEU EUEUE UEUEU EUEUE UEUEU EUEUE UEUEU EUEUE UEUEU UUEUU EEEUE EEUEE EUEEE UEEEU EEEUE EEUEE EUEEE UEEEU EEEUE EEUEE EUEEE UEEEU EEEUE EEUEE EUEEE UUEUU UEUUE UUUEU UUEUU UEUUU EUUUE UUUEU UUEUU UEUUU EUUUE UUUEU UUEUU UEUUU EUUUE UUUEU UUEUU UEUUE UUFBD TYSQY OEZLP RBYEI DCHRF IYQDK OUZLP RBYUB FRYLK YQFRR LLHJY DUBFI YRLZF ASPYL SRRBD RRBYZ PFYXY FQQRY ADKLA PDMBW CLKCY DIFKA RBYHY WULPO ZLPRB YCFMB YPFKC BDMRY PZFTY UFRBL SRGLO FYFDJ KLRQS PYUBY PYRLA LKYVR RBYPY DPYLK IWRUL MLQQF EFIFR FYQDQ ZDPDQ FCDKQ YYESR FBDTY KLFOY DUBYP YLKYL ZRBYJ FQDKO DQZDP DQFCD KRYII KLLKY YIQYH KLUQY FRBYP QLFAS YQQFU FIIBY DOZLP LIWJM FDQYY WLSRB YPY\n"
+ ]
+ }
+ ],
+ "source": [
+ "print(ca)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 5,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": [
+ "ca_text = '''\n",
+ "JDPWD JGLOF YBDQA LKYFD PPFTY OFKQY ICSHD QMIDK KYOES RQBYO FOKRJ DHYRB YPYKO YXTLS QRBYI \n",
+ "LCDIQ RDRFL KRBFK HQBYJ DWBDT YEYYK HFOKD MMYOE SRRBY PYBDQ EYYKK LPDKQ LJOYJ DKODK ORBYP \n",
+ "YFQKL RBFKA JSCBR LALLK BYPPL LJBDO EYYKP DKQDC HYODK OQBYI YZRKL JYQQD AYESR QLJYR BFKAR \n",
+ "YIIQJ YQBYF QLHDK OUYQB LSIOJ LTYLK FRBFK HQBYJ DWBDT YMIDK KYORB FQJYD KUBFI YFBDT YRBYO \n",
+ "LCSJY KRGLO FYUDK RYOZP LJILK OLKFO LKRHK LUBLU QBYHK YUESR RBYZF ZRBCB DMRYP UDQBF OOYKF \n",
+ "KDCDT FRWFK DQRLK YIFKR YIFKQ LJYPY JDFKQ FKRBY YMBYQ SQPLL JDRRB YEPFR FQBJS QYSJF RUDQY\n",
+ "KCPWM RYOSQ FKADK LRBYP MLIWD IMBDE YRFCC FMBYP FKTYK RYOFK RBYKF KYRYY KRBCY KRSPW FDQHY \n",
+ "ORBYJ SQYSJ RLRYQ RFRDK ORBYO LCSJY KRFQD SRBYK RFCQL FASYQ QRBYF JMYPF DICFM BYPQC BLLIP \n",
+ "YDIIW BDOJL TYOLK MYPBD MQFQB LSIOK RBDTY EYYKQ SPMPF QYODZ RYPRB YOFQC LTYPW DRELO PSJES \n",
+ "RFRIY DTYQJ YULKO YPFKA UBDRY IQYRB YWOYT YILMY OLKYL OOZYD RSPYU DQDZP FYXYP SKKFK APLSK \n",
+ "ORBPY YQFOY QLZRB YIFKR YICLK QFQRF KALZD QFKAI YPLUL ZEIDC HDKOU BFRYR FIYQR BDRRB YJSQY \n",
+ "SJRBF KHUYP YDOOY OFKRB YPLJD KMYPF LORBY WPYDO FKQYN SYKCY\n",
+ " FBD TYSQY OEZLP RBYEI DCHRF IYQDK OUZLP RBYUB FRYLK YQFRR LLHJY DUBFI YRLZF ASPYL SRRBD \n",
+ "RRBYZ PFYXY FQQRY ADKLA PDMBW CLKCY DIFKA RBYHY WULPO ZLPRB YCFMB YPFKC BDMRY PZFTY UFRBL \n",
+ "SRGLO FYFDJ KLRQS PYUBY PYRLA LKYVR RBYPY DPYLK IWRUL MLQQF EFIFR FYQDQ ZDPDQ FCDKQ YYESR \n",
+ "FBDTY KLFOY DUBYP YLKYL ZRBYJ FQDKO DQZDP DQFCD KRYII KLLKY YIQYH KLUQY FRBYP QLFAS YQQFU \n",
+ "FIIBY DOZLP LIWJM FDQYY WLSRB YPY\n",
+ "'''\n",
+ "sca_text = sanitise(ca_text)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 6,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "(24, -1733.3180566179738)"
+ ]
+ },
+ "execution_count": 6,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "key_a, score = caesar_break(sca_text)\n",
+ "key_a, score"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 7,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "lfryflinqhadfscnmahfrrhvaqhmsakeujfsokfmmaqgutsdaqhqmtlfjatdaramqazvnustdaknefkstfthnmtdhmjsdalfydfvagaamjhqmfooaqguttdaradfsgaammnrfmsnlqalfmqfmqtdarahsmntdhmcluedtncnnmdarrnnldfqgaamrfmsfejaqfmqsdakabtmnlassfcagutsnlatdhmctakkslasdahsnjfmqwasdnukqlnvanmhtdhmjsdalfydfvaokfmmaqtdhslafmwdhkahdfvatdaqneulamtinqhawfmtaqbrnlknmqnmhqnmtjmnwdnwsdajmawguttdabhbtdedfotarwfsdhqqamhmfefvhtyhmfstnmakhmtakhmsnlaralfhmshmtdaaodasusrnnlfttdagrhthsdlusaulhtwfsameryotaqushmcfmntdaronkyfkodfgatheehodarhmvamtaqhmtdamhmataamtdeamturyhfsjaqtdalusaultntasthtfmqtdaqneulamthsfutdamthesnhcuasstdahloarhfkehodarsednnkrafkkydfqlnvaqnmoardfoshsdnukqmtdfvagaamsurorhsaqfbtartdaqhsenvaryftgnqrulguthtkafvaslawnmqarhmcwdftaksatdayqavaknoaqnmanqqbafturawfsfbrhazarummhmcrnumqtdraashqasnbtdakhmtakenmshsthmcnbfshmckarnwnbgkfejfmqwdhtathkastdfttdalusaultdhmjwarafqqaqhmtdarnlfmoarhnqtdayrafqhmsapuameahdfvausaqgbnrtdagkfejthkasfmqwbnrtdawdhtanmashttnnjlafwdhkatnbhcuranuttdfttdabrhazahsstacfmncrfodyenmeafkhmctdajaywnrqbnrtdaehodarhmedfotarbhvawhtdnutinqhahflmntsurawdaratncnmaxttdarafranmkytwnonsshghkhthasfsbfrfshefmsaaguthdfvamnhqafwdaranmanbtdalhsfmqfsbfrfshefmtakkmnnmaaksajmnwsahtdarsnhcuasshwhkkdafqbnrnkylohfsaaynutdara\n"
+ ]
+ }
+ ],
+ "source": [
+ "print(caesar_decipher(sca_text, key_a))"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 8,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "((17, 21, True), -1723.2877779777489)"
+ ]
+ },
+ "execution_count": 8,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "(m_a, a_a, o_a), score = affine_break(sca_text)\n",
+ "(m_a, a_a, o_a), score"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 9,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "gyotygparsneylhadnsyooscnrsdlnjbfmylxjyddnrvfilenrsrdigymnienondrnqcaflienjabyjliyisadiesdmlengyteycnvnndmsrdyxxnrvfiienoneylvnnddaoydlagrngydrydrienonsldaiesdhgfbeiahaadenooaageyrvnndoydlybmnrydrlenjnkidagnllyhnvfilagniesdhinjjlgnlenslamydrznleafjrgacnadsiesdmlengyteycnxjyddnrieslgnydzesjnseycnienrabfgndiparsnzydinrkoagjadradsradimdazeazlenmdnzvfiienkskiebeyxinozylesrrndsdybycsitsdyliadnjsdinjsdlagnongysdlsdiennxenlfloaagyiienvosislegflnfgsizylndbotxinrflsdhydaienoxajtyjxeyvnisbbsxenosdcndinrsdiendsdninndiebndifotsylmnriengflnfgiainlisiydrienrabfgndislyfiendisblashfnlliensgxnosyjbsxenolbeaajonyjjteyrgacnradxnoeyxlsleafjrdieycnvnndlfoxoslnrykinoienrslbacnotyivarofgvfisijnycnlgnzadrnosdhzeyinjlnientrncnjaxnradnarrknyifonzylykosnqnofddsdhoafdrieonnlsrnlakienjsdinjbadlslisdhakylsdhjnoazakvjybmydrzesinisjnlieyiiengflnfgiesdmznonyrrnrsdienoagydxnosarientonyrsdlnufndbnseycnflnrvkaoienvjybmisjnlydrzkaoienzesinadnlsiiaamgnyzesjniakshfonafiieyiienkosnqnsllinhydahoyxetbadbnyjsdhienmntzaorkaoienbsxenosdbeyxinokscnzsieafiparsnsygdailfonzenoniahadnwiienonyonadjtizaxallsvsjsisnlylkyoylsbydlnnvfiseycndasrnyzenonadnakiengslydrylkyoylsbydinjjdaadnnjlnmdazlnsienolashfnllszsjjenyrkaoajtgxsylnntafienon\n"
+ ]
+ }
+ ],
+ "source": [
+ "print(affine_decipher(sca_text, m_a, a_a, o_a))"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 10,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "'etoainhsrdlumwycfgpbvkxjqz'"
+ ]
+ },
+ "execution_count": 10,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "ltrs = [p[0] for p in english_counts.most_common()]\n",
+ "cat(ltrs)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 11,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "'yrkfdlbqpoisjcumzaetwhxgvn'"
+ ]
+ },
+ "execution_count": 11,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "ctls = [p[0] for p in collections.Counter(sanitise(ca_text)).most_common()]\n",
+ "cat(ctls)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 12,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "{'A': 'g',\n",
+ " 'B': 'h',\n",
+ " 'C': 'w',\n",
+ " 'D': 'i',\n",
+ " 'E': 'p',\n",
+ " 'F': 'a',\n",
+ " 'G': 'j',\n",
+ " 'H': 'k',\n",
+ " 'I': 'l',\n",
+ " 'J': 'm',\n",
+ " 'K': 'o',\n",
+ " 'L': 'n',\n",
+ " 'M': 'c',\n",
+ " 'N': 'z',\n",
+ " 'O': 'd',\n",
+ " 'P': 'r',\n",
+ " 'Q': 's',\n",
+ " 'R': 't',\n",
+ " 'S': 'u',\n",
+ " 'T': 'b',\n",
+ " 'U': 'y',\n",
+ " 'V': 'q',\n",
+ " 'W': 'v',\n",
+ " 'X': 'x',\n",
+ " 'Y': 'e',\n",
+ " 'Z': 'f'}"
+ ]
+ },
+ "execution_count": 12,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "trans = {pr[1].upper(): pr[0] for pr in zip(ltrs, ctls)}\n",
+ "trans"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 13,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "mirvi mjnda ehisg noeai rrabe daose lwuki sclio oedpu tshed adotm iketh ereod exbnu sthel nwils titan othao kshem ivhib epeeo kadoi ccedp utthe rehis peeoo nrios nmdem iodio dther eason thaog muwht ngnno herrn nmhid peeor iosiw kedio dshel efton messi geput snmet haogt ellsm eshea snkio dyesh nuldm nbeno athao kshem ivhib eclio oedth asmei oyhal eahib ethed nwume otjnd aeyio tedfr nmlno dnoad notko nyhny sheko eyput thefa fthwh icter yisha ddeoa oiwib atvao istno elaot elaos nmere miaos aothe eches usrnn mitth eprat ashmu seuma tyise owrvc tedus aogio nther cnlvi lchip etaww acher aobeo tedao theoa oetee othwe oturv aiske dthem useum tntes tatio dthed nwume otasi utheo tawsn agues sthea mcera ilwac hersw hnnlr eillv hidmn bedno cerhi csash nuldo thibe peeos urcra sedif terth edasw nberv itpnd rumpu tatle ibesm eynod eraog yhite lseth evdeb elnce dnoen ddfei turey isifr aexer uooao grnuo dthre esade snfth elaot elwno sasta ognfi saogl ernyn fpliw kiody hatet alest hitth emuse umtha okyer eidde daoth ernmi ocera ndthe vreid aosez ueowe yyppy yypyy ypyyy pyyyp yyypy yypyy ypyyy pyyyp yyypy yypyy ypyyy pyyyp yyypy yypyy ypyyy pyyyp pyyyp yypyp ypypy pypyp ypypy pypyp ypypy pypyp ypypy pypyp ypypy pypyp ypypy pypyp ypypy pypyp ypyyp yyypy pppyp ppypp pyppp ypppy pppyp ppypp pyppp ypppy pppyp ppypp pyppp ypppy pppyp ppypp pyppp ypyyy pyyyp yyypy yypyy ypyyy pyyyp yyypy yypyy ypyyy pyyyp yyypy yypyy ypyyy pyyyp yyypy yypyy ypyyy pyyyp yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yypyy ypyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy ypyyy pyyyy pyyyp ppyyy yyppp yyypy yypyy ypyyy pyyyy pppyy pyyyp ypppy yyppp pyypp pyyyp ppyyp ppypy yypyy yypyp yypyy pyyyp yyyyy pypyy ppyyp yypyp yyyyp yyypp yppyp yypyy pyyyy ypyyp yyypy yyypy yypyy ypyyp yyypy pyyyp ypyyy yypyy ypypy pypyp yyypy yypyy ypypy pypyy pyypy yyyyp yyypy ypyyy ypyyp yyypy yyppp ppypp ppyyp yyyyy ppppp ypypy pyppp ppyyy pyyyp yyypy ppppy ypppy yyppp pyyyp yyyyp yyypy yypyy pyyyp ypypy yypyy yyypy yypyp ypypy pyyyp yyypy yypyy ypypy yyyyp yyyyy pypyy yypyy yypyy pyyyp yyypy yypyp yypyy ypyyy ypyyy pypyy ppypy yypyy ypyyy pyyyp ypyyy yypyy yyypy ypyyy pyyyy pyyyp yyypy ypyyy pypyy ypyyy pppyp yyypy pyyyp ypyyy pyypp pyypy yypyp yyyyy ppppy ypyyy pyppp yyppp ypyyy pyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy pyyyp yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yyyyy yypyy ypypy yypyy ypyyy pyyyp yyypy yypyy ypyyy pyyyp yyypy yypyy ypyyy pyyyp yyypy yypyy ypyyy pyyyp ypyyy pypyp ypypy pypyp ypypy pypyp ypypy pypyp ypypy pypyp ypypy pypyp ypypy pypyp ypypy pypyp ypypy yypyy pppyp ppypp pyppp ypppy pppyp ppypp pyppp ypppy pppyp ppypp pyppp ypppy pppyp ppypp pyppp yypyy ypyyp yyypy yypyy ypyyy pyyyp yyypy yypyy ypyyy pyyyp yyypy yypyy ypyyy pyyyp yyypy yypyy ypyyp yyahi beuse dpfnr thepl iwkta lesio dyfnr theyh ateno esatt nnkme iyhal etnfa guren utthi tthef raexe asste giong richv wnowe ilaog theke vynrd fnrth ewach eraow hicte rfabe yathn utjnd aeaim ontsu reyhe retng noeqt there ireno lvtyn cnssa palat aesis firis awios eeput ahibe onade iyher enoen fthem asiod isfir isawi otell onnoe elsek onyse ather snagu essay allhe idfnr nlvmc aisee vnuth ere\n"
+ ]
+ }
+ ],
+ "source": [
+ "tt = ''.maketrans(trans)\n",
+ "print(ca.translate(tt))"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 14,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "maryam jodie has gone i arrived in selcuk as planned but she didnt make the rendezvous the local\n",
+ "station think she may have been kidnapped but there has been no ransom demand and there is nothing\n",
+ "much to go on her room had been ransacked and she left no message but something tells me she is ok\n",
+ "and we should move on i think she may have planned this meanwhile i have the document jodie wanted\n",
+ "from london i dont know how she knew but the fifth chapter was hidden in a cavity in a stone lintel\n",
+ "in some remains in the ephesus room at the british museum it was encrypted using another poly\n",
+ "alphabetic cipher invented in the nineteenth century i asked the museum to test it and the document\n",
+ "is authentic so i guess the imperial cipher school really had moved on perhaps i shouldnt have been\n",
+ "surprised after the discovery at bodrum but it leaves me wondering what else they developed one odd\n",
+ "feature was a frieze running round three sides of the lintel consisting of a single row of\n",
+ "blackandwhite tiles that the museum think were added in the roman period they read in sequence\n",
+ "wwbbwwwbwwwbwwwbwwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb www bbw\n",
+ "wwbw wbwbwbwbwbwbwbwbwbw bwbwbwbwbwbwbwbwbwbw bwbwbwbwbwbwbwbwbwbw bwbwbwbwbwbwbwbwbwwb www\n",
+ "bwbbbwbbbwbbbwbbbwb bbwbbbwbbbwbbbwbbbwb bbwbbbwbbbwbbbwbbbwb bbwbbbwbbbwbbbwbbbwb wwwb wwwb wwwb\n",
+ "wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb www\n",
+ "www www www www www www www www www www www www www www www www www www www www www www www www wwbw\n",
+ "wwbw www www www www www www www www www www www www www www www www www www www www www www www www\n",
+ "www wwwb wwwb www wb wwwb bbw wwww bbb wwwb wwwb wwwb www bw www bbb wwbw wwbw bbb www bbbb\n",
+ "wwbbbwwwbbbwwbbbwb www bwwwwbwbwwbwwbwwwbww www bwbwwbbwwbwwbwbwwwwb www bbw bbw\n",
+ "bwwbwwbwwwwwbwwbwwwb www wb www bwwwbwwbwwwbwbwwwbwb www wwbwwwbwbwbwbwbwwwb www bwwwbwbwbwbwwbwwbww\n",
+ "www bwwwbwwbwwwwbwwbwwwb www bbb bbw bbbb wwbw wwww bbb bbw bwbwbwbbbbbwwwbwwwbw wwbw bbbb ww bbb\n",
+ "www bbbb wwwb www wb www bwwwbwwbwwwbwbwbwwwb www wwbwwwbwbwbwbwbwwwb wwwb wwwb www bwb www wwbw\n",
+ "wwww bwbwwwwbwwwwbwwbwwwb www bwwwbwbwwbwwwbwwwwb www bwbwwbbwbwwwbwwwb wwwb www bwb www wwbw www wb\n",
+ "wwbw wwbw www bwwwbwwwbwwbwwwbwb wwwb www bbb wbwwwbwbwwwbwbwwwbww bbb wwbw wwbw bww www bbbb wwbw\n",
+ "wwbwbbbwwbbbwbwwwbww www www www www www www www www www www www www www www www www www www www www\n",
+ "www www www www www wwbw wwbw www www www www www www www www www www www www www www www www www\n",
+ "www www www www www www www www wbwwwbwbwwwbwwwbwwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb\n",
+ "wwwb wwwb wwwb wwwb wwwb wwwb www bwb www bwbwbwbwbwbwbwbwbwb wbwbwbwbwbwbwbwbwbwb\n",
+ "wbwbwbwbwbwbwbwbwbwb wbwbwbwbwbwbwbwbwbwb www bwwbbbwbbbwbbbwbbbwb bbw bbb wbbbwbbbwbbbwbbbwb bbw\n",
+ "bbb wbbbwbbbwbbbwbbbwb bbwbbbwbbbwwbwwwbwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb\n",
+ "wwwb wwwb wwwb www bwwwbwwwbwwwbwwbwwi have used b for the black tiles and w for the white ones it\n",
+ "took me a while to figure out that the frieze is steganography concealing the keyword for the cipher\n",
+ "in chapter five without jodie i am not sure where to go next there are only two possibilities as far\n",
+ "as i can see but i have no idea where one of them is and as far as i can tell no one else knows\n",
+ "either so i guess i will head for olympia see you there\n"
+ ]
+ }
+ ],
+ "source": [
+ "trans = {\n",
+ " 'A': 'g',\n",
+ " 'B': 'h',\n",
+ " 'C': 'c',\n",
+ " 'D': 'a',\n",
+ " 'E': 'b',\n",
+ " 'F': 'i',\n",
+ " 'G': 'j',\n",
+ " 'H': 'k',\n",
+ " 'I': 'l',\n",
+ " 'J': 'm',\n",
+ " 'K': 'n',\n",
+ " 'L': 'o',\n",
+ " 'M': 'p',\n",
+ " 'N': 'q',\n",
+ " 'O': 'd',\n",
+ " 'P': 'r',\n",
+ " 'Q': 's',\n",
+ " 'R': 't',\n",
+ " 'S': 'u',\n",
+ " 'T': 'v',\n",
+ " 'U': 'w',\n",
+ " 'V': 'x',\n",
+ " 'W': 'y',\n",
+ " 'X': 'z',\n",
+ " 'Y': 'e',\n",
+ " 'Z': 'f'}\n",
+ "\n",
+ "tt = ''.maketrans(trans)\n",
+ "print('\\n'.join(tpack(segment(sanitise(ca.translate(tt))))))"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 15,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "{'A': 'g',\n",
+ " 'B': 'h',\n",
+ " 'C': 'c',\n",
+ " 'D': 'a',\n",
+ " 'E': 'b',\n",
+ " 'F': 'i',\n",
+ " 'G': 'j',\n",
+ " 'H': 'k',\n",
+ " 'I': 'l',\n",
+ " 'J': 'm',\n",
+ " 'K': 'n',\n",
+ " 'L': 'o',\n",
+ " 'M': 'p',\n",
+ " 'N': 'q',\n",
+ " 'O': 'd',\n",
+ " 'P': 'r',\n",
+ " 'Q': 's',\n",
+ " 'R': 't',\n",
+ " 'S': 'u',\n",
+ " 'T': 'v',\n",
+ " 'U': 'w',\n",
+ " 'V': 'x',\n",
+ " 'W': 'y',\n",
+ " 'X': 'z',\n",
+ " 'Y': 'e',\n",
+ " 'Z': 'f'}"
+ ]
+ },
+ "execution_count": 15,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "trans"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 16,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "{'a': 'D',\n",
+ " 'b': 'E',\n",
+ " 'c': 'C',\n",
+ " 'd': 'O',\n",
+ " 'e': 'Y',\n",
+ " 'f': 'Z',\n",
+ " 'g': 'A',\n",
+ " 'h': 'B',\n",
+ " 'i': 'F',\n",
+ " 'j': 'G',\n",
+ " 'k': 'H',\n",
+ " 'l': 'I',\n",
+ " 'm': 'J',\n",
+ " 'n': 'K',\n",
+ " 'o': 'L',\n",
+ " 'p': 'M',\n",
+ " 'q': 'N',\n",
+ " 'r': 'P',\n",
+ " 's': 'Q',\n",
+ " 't': 'R',\n",
+ " 'u': 'S',\n",
+ " 'v': 'T',\n",
+ " 'w': 'U',\n",
+ " 'x': 'V',\n",
+ " 'y': 'W',\n",
+ " 'z': 'X'}"
+ ]
+ },
+ "execution_count": 16,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "{v: k for k, v in trans.items()}"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 17,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "maryam jodie has gone i arrived in selcuk as planned but she didnt make the rendezvous the local\n",
+ "station think she may have been kidnapped but there has been no ransom demand and there is nothing\n",
+ "much to go on her room had been ransacked and she left no message but something tells me she is ok\n",
+ "and we should move on i think she may have planned this meanwhile i have the document jodie wanted\n",
+ "from london i dont know how she knew but the fifth chapter was hidden in a cavity in a stone lintel\n",
+ "in some remains in the ephesus room at the british museum it was encrypted using another poly\n",
+ "alphabetic cipher invented in the nineteenth century i asked the museum to test it and the document\n",
+ "is authentic so i guess the imperial cipher school really had moved on perhaps i shouldnt have been\n",
+ "surprised after the discovery at bodrum but it leaves me wondering what else they developed one odd\n",
+ "feature was a frieze running round three sides of the lintel consisting of a single row of\n",
+ "blackandwhite tiles that the museum think were added in the roman period they read in sequence\n",
+ "wwbbwwwbwwwbwwwbwwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb www bbw\n",
+ "wwbw wbwbwbwbwbwbwbwbwbw bwbwbwbwbwbwbwbwbwbw bwbwbwbwbwbwbwbwbwbw bwbwbwbwbwbwbwbwbwwb www\n",
+ "bwbbbwbbbwbbbwbbbwb bbwbbbwbbbwbbbwbbbwb bbwbbbwbbbwbbbwbbbwb bbwbbbwbbbwbbbwbbbwb wwwb wwwb wwwb\n",
+ "wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb www\n",
+ "www www www www www www www www www www www www www www www www www www www www www www www www wwbw\n",
+ "wwbw www www www www www www www www www www www www www www www www www www www www www www www www\n",
+ "www wwwb wwwb www wb wwwb bbw wwww bbb wwwb wwwb wwwb www bw www bbb wwbw wwbw bbb www bbbb\n",
+ "wwbbbwwwbbbwwbbbwb www bwwwwbwbwwbwwbwwwbww www bwbwwbbwwbwwbwbwwwwb www bbw bbw\n",
+ "bwwbwwbwwwwwbwwbwwwb www wb www bwwwbwwbwwwbwbwwwbwb www wwbwwwbwbwbwbwbwwwb www bwwwbwbwbwbwwbwwbww\n",
+ "www bwwwbwwbwwwwbwwbwwwb www bbb bbw bbbb wwbw wwww bbb bbw bwbwbwbbbbbwwwbwwwbw wwbw bbbb ww bbb\n",
+ "www bbbb wwwb www wb www bwwwbwwbwwwbwbwbwwwb www wwbwwwbwbwbwbwbwwwb wwwb wwwb www bwb www wwbw\n",
+ "wwww bwbwwwwbwwwwbwwbwwwb www bwwwbwbwwbwwwbwwwwb www bwbwwbbwbwwwbwwwb wwwb www bwb www wwbw www wb\n",
+ "wwbw wwbw www bwwwbwwwbwwbwwwbwb wwwb www bbb wbwwwbwbwwwbwbwwwbww bbb wwbw wwbw bww www bbbb wwbw\n",
+ "wwbwbbbwwbbbwbwwwbww www www www www www www www www www www www www www www www www www www www www\n",
+ "www www www www www wwbw wwbw www www www www www www www www www www www www www www www www www\n",
+ "www www www www www www www www wbwwwbwbwwwbwwwbwwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb\n",
+ "wwwb wwwb wwwb wwwb wwwb wwwb www bwb www bwbwbwbwbwbwbwbwbwb wbwbwbwbwbwbwbwbwbwb\n",
+ "wbwbwbwbwbwbwbwbwbwb wbwbwbwbwbwbwbwbwbwb www bwwbbbwbbbwbbbwbbbwb bbw bbb wbbbwbbbwbbbwbbbwb bbw\n",
+ "bbb wbbbwbbbwbbbwbbbwb bbwbbbwbbbwwbwwwbwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb\n",
+ "wwwb wwwb wwwb www bwwwbwwwbwwwbwwbwwi have used b for the black tiles and w for the white ones it\n",
+ "took me a while to figure out that the frieze is steganography concealing the keyword for the cipher\n",
+ "in chapter five without jodie i am not sure where to go next there are only two possibilities as far\n",
+ "as i can see but i have no idea where one of them is and as far as i can tell no one else knows\n",
+ "either so i guess i will head for olympia see you there\n"
+ ]
+ }
+ ],
+ "source": [
+ "print('\\n'.join(tpack(segment(keyword_decipher(sanitise(ca), 'decoy', KeywordWrapAlphabet.from_last)))))"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 18,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "3362"
+ ]
+ },
+ "execution_count": 18,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "open('5a.plaintext', 'w').write('\\n'.join(tpack(segment(sanitise(ca.translate(tt))))))"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 19,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": [
+ "bkeyt = sanitise('''\n",
+ "wwbbwwwbwwwbwwwbwwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb www bbw\n",
+ "wwbw wbwbwbwbwbwbwbwbwbw bwbwbwbwbwbwbwbwbwbw bwbwbwbwbwbwbwbwbwbw bwbwbwbwbwbwbwbwbwwb www\n",
+ "bwbbbwbbbwbbbwbbbwb bbwbbbwbbbwbbbwbbbwb bbwbbbwbbbwbbbwbbbwb bbwbbbwbbbwbbbwbbbwb wwwb wwwb wwwb\n",
+ "wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb www\n",
+ "www www www www www www www www www www www www www www www www www www www www www www www www wwbw\n",
+ "wwbw www www www www www www www www www www www www www www www www www www www www www www www www\n",
+ "www wwwb wwwb www wb wwwb bbw wwww bbb wwwb wwwb wwwb www bw www bbb wwbw wwbw bbb www bbbb\n",
+ "wwbbbwwwbbbwwbbbwb www bwwwwbwbwwbwwbwwwbww www bwbwwbbwwbwwbwbwwwwb www bbw bbw\n",
+ "bwwbwwbwwwwwbwwbwwwb www wb www bwwwbwwbwwwbwbwwwbwb www wwbwwwbwbwbwbwbwwwb www bwwwbwbwbwbwwbwwbww\n",
+ "www bwwwbwwbwwwwbwwbwwwb www bbb bbw bbbb wwbw wwww bbb bbw bwbwbwbbbbbwwwbwwwbw wwbw bbbb ww bbb\n",
+ "www bbbb wwwb www wb www bwwwbwwbwwwbwbwbwwwb www wwbwwwbwbwbwbwbwwwb wwwb wwwb www bwb www wwbw\n",
+ "wwww bwbwwwwbwwwwbwwbwwwb www bwwwbwbwwbwwwbwwwwb www bwbwwbbwbwwwbwwwb wwwb www bwb www wwbw www wb\n",
+ "wwbw wwbw www bwwwbwwwbwwbwwwbwb wwwb www bbb wbwwwbwbwwwbwbwwwbww bbb wwbw wwbw bww www bbbb wwbw\n",
+ "wwbwbbbwwbbbwbwwwbww www www www www www www www www www www www www www www www www www www www www\n",
+ "www www www www www wwbw wwbw www www www www www www www www www www www www www www www www www\n",
+ "www www www www www www www www wbwwwbwbwwwbwwwbwwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb\n",
+ "wwwb wwwb wwwb wwwb wwwb wwwb www bwb www bwbwbwbwbwbwbwbwbwb wbwbwbwbwbwbwbwbwbwb\n",
+ "wbwbwbwbwbwbwbwbwbwb wbwbwbwbwbwbwbwbwbwb www bwwbbbwbbbwbbbwbbbwb bbw bbb wbbbwbbbwbbbwbbbwb bbw\n",
+ "bbb wbbbwbbbwbbbwbbbwb bbwbbbwbbbwwbwwwbwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb wwwb\n",
+ "wwwb wwwb wwwb www bwwwbwwwbwwwbwwbww''')"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 20,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "1577"
+ ]
+ },
+ "execution_count": 20,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "len(bkeyt)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 21,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "1577"
+ ]
+ },
+ "execution_count": 21,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "19 * 83"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 22,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "[' ** * * * * * * * * * * * * * * * * * * ** ',\n",
+ " ' * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ',\n",
+ " ' * *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** * ',\n",
+ " ' * * * * * * * * * * * * * * * * * * * * * ',\n",
+ " ' * * ',\n",
+ " ' * * ',\n",
+ " ' * * *** *** * * * * *** * * *** **** *** *** *** * ',\n",
+ " ' * * * * * * * * ** * * * * ** ** * * * * * * * * ',\n",
+ " ' * * * * * * * * * * * * * * * * * * * * * * * * * ',\n",
+ " ' * ***** **** * ***** * * * ***** * * * **** *** **** * * * ',\n",
+ " ' * * * * * * * * * * * * * * * * * * * * * * * ',\n",
+ " ' * * * * * * * * * ** * * * * * * * * * * * * ',\n",
+ " ' * * * * * *** * * * * * * *** * * * **** * * *** *** * ',\n",
+ " ' * * ',\n",
+ " ' * * ',\n",
+ " ' * * * * * * * * * * * * * * * * * * * * * * ',\n",
+ " ' * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ',\n",
+ " ' * *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** * ',\n",
+ " ' * * * * * * * * * * * * * * * * * * * * * ']"
+ ]
+ },
+ "execution_count": 22,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "tt = ''.maketrans({'w': ' ', 'b': '*'})\n",
+ "chunks(bkeyt.translate(tt), 83)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 25,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": [
+ "scb = sanitise(cb)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 27,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "('fwruffnquevmp', -1750.4576000271347)"
+ ]
+ },
+ "execution_count": 27,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "kb, score = vigenere_frequency_break(cb)\n",
+ "kb, score"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 28,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "'nieyqmhcenwlqvchdporosweiohtsshreoihpsvrvusdcostmjezkdeytdhrtasupknropbklngeutgnssthjhhnowgbdwvucncdvofhbdltfpvobiuicirayodpwkpoutibtgcenuhpwachchnbrjydhsfvhqszeuvdcpswatvzmxhsiigkobvcagqcdikfstrvkfggbdkvcvstuvufienwiniasbcecmvdwemkwgtnsyjridowjamhuhnrvxssgewikqxpxhashvwvteulpavioctweavlhtikkkximoikdloikrwswfiojrynulrsgtoktknqcporlruoigivdpeddhrntdmrnucnrioiuetpdnftaipjrewjqtercxzvydhiswrtrneonknnfsyrifcdhedoweptjrechehcptdcehirohbcpniloorntrrrtohgeoandszehilxywfqkrgnphglvcojfbetcietpqmfetkalnahiwixdppersmrihrctseahkhtifbokasoozrjgvvkehjsufarunbrtiswzdmriwcrovhdsdeihdteiiustmjupvkhdiisibjloyrdpyxkusdlwfpsehvevdoiudppehzgvcewnhifrvkfggbdkbidptartnotloihpseehrdipeotmlwbcousicucctsktsrlritcanhpdmrehuinioiuuwirsxtanvjshfwyrkacfihfctdipytegvprdowhfldhycrqhfctswdbnagevitctsufsrofoccprycjgnifeikrthnowgcedfedjwiisdtihszeecfvlwjqgehoeicssaqflthagmncpvvifnreneoiioiulytddhuhkrtzsdpevphodehkctsvfhltophksiepsgcohnrnuseajettlhdwhiepjwcenbtbuecihpijncohouidiqjikpovimvsrfridohvchdporhrmhlpnikpacvjiswwgszeyxrcpragclkvxbporlruoigoviferifridicrskiporsyypwgprszenephnicczvttucvqesuiivqleesvfrhcfgchuswrocjslotcnxtiiswkoieatruninitradpaortylcouoahdpehgukcvlrmrafrntaovetrxvyinpenvniquglltisxgy'"
+ ]
+ },
+ "execution_count": 28,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "vigenere_decipher(sanitise(cb), kb)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 29,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "('arcanaimperii', -1506.8637359274674)"
+ ]
+ },
+ "execution_count": 29,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "kb, score = beaufort_frequency_break(cb)\n",
+ "kb, score"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 30,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "'inhisjournaldatedtheidesofoctoberintheyearoftheconsulshipsofcaeceliustulliuscapitopomponianusplotiusfirmusandgaiuscorneliusgallicanusagricolawrotethemysteryofthebattleatcamulodonumisatlastsolvedcalgacusmaybeabarbariannowbuthewasaromancitizenthenwhobetrayedusallforloveofabarbarianithastakenallmyskillsasaleaderofmentokeephimalivethelegionnairesspendtheireveningsdesigningnewandcruelwaystoexecutehiminrevengefortheshamehebroughtuponusbuthislifeispreciousitistheonlycardlefttoplayinoursearchforsalvationandthereturnofthestolenaquilaeifwecanalsorecoverthecodexthenperhapsitslosscanbeconcealedandourliveswillbesparedreleasingtheromantraitorcalgacusmusthavestuckintheproudagricolasthroatbuthemadeapactwiththeremainingcaledoniiandtravellednorthtoexchangetheprisonerfortheaquilaeandthecodexbutthecunningcaledoniantribesmansetanothertrapandpresentedagricolawithaforgerycunninglyassembledwithpagesfromthebooksstolenwhenthetriberansackedmonsgraupiusfortoolongthesonsofromehadunderestimatedthepeopleinbritanniaandwhiletheaquilaofthelegionhadbeenrestoredbytheexchangetheirhonourwasnotagricolafacedareturntoromehumiliationandalmostcertaindeaththesixthchapterofmytaleofwoeisguardedbylightningbullandoak'"
+ ]
+ },
+ "execution_count": 30,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "beaufort_decipher(sanitise(cb), kb)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 31,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "'a'"
+ ]
+ },
+ "execution_count": 31,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "unpos(25 - pos('z'))"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 32,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "'zyxwvutsrqponmlkjihgfedcba'"
+ ]
+ },
+ "execution_count": 32,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "cat(unpos(25 - pos(l)) for l in string.ascii_lowercase)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 33,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "'hveheifhbiicuzbbcfgvvnzafwnkqnndivxopvpdionrsyrmjvjlkaehbrfsmvmtvkqrrftcyspatroqqnbndcyiqretamkaszhcaqidtaxmpfrvenkfimmihgrxnvgqtrmcpzsqzpygininbbstddlcomipnnqgqargdgmrkbijtxnubxpuzjzbizesjbvqmutztdzotjzktjvraioamqznxiwnstbedizjnbjurebqqhldegrzvnynjbsdzprnparcknlqxnmryaisrqjxquzevdcijkzsbmmkczinszckaxrmkvnnzzfwlmksajvrzcqdrkqsdfgvyobqfemifqqrjcoilkydqodhdevxbauvrqdmumxaorieubzrdxvrlcowvodkrsqgzzsizvmdvddrnigrzayrlmednqfhqcblgnvrrntkusntzwdqpoddtvypazkhaqgqneyixiiukmcsfngyktqeftzpdmqtupjzjrkdxsunenxybyvqmqtdmfsdcmjkntbmmplvvvmzwvmzzzzcfymmtfumostdtbnzfkydvmddgrccdbjcnapbmmsrmjvtvztbcmmubemtzmdathxksrckiivczbkqzjvxbbyvqwjzzsinsowitztdzotjzenbyrumpsgbbvxopvgqwrcmfivmjtrjsponmsshdcmdrcmxombkjsopkydzblmhevxbkrcdllmuhranozrmdtidpmfedcbfvwkezzfvgrzxizrwkddefedcmrhtqizqzeqdcmtfcmuagskuoxceehvdbmkvqyiqreszfaqrdnxnmkrmwqgqqkekkieuozbrqmkrnvoizbwizihkukawixdzvbgmevxbtprrablnkvqgdbygzobrrqfzdcmsfnsprfncrxrpvespbsdhsrbvvjrbsbcynefqmilghcpeaqkbygwexspbramjbpmwdvgiatzcveonbzdzbbcfgvcojxcvhvyqusraxdireceehxdkuovylzkilefgvyobqfegiaaqdeeonbfidlyxfgvrhxprefmqgqhiuyiwlivipmasrtbdkfcznxbqcreoocieswonydyhwdtzrsqlmmmunvhwjkbmosmheqovbykgmphjsyprvxkvqwclksryojnnfdqpfgziqoyjpchoeszhetlptcrmllzw'"
+ ]
+ },
+ "execution_count": 33,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "rscb = cat(unpos(25 - pos(l)) for l in scb)\n",
+ "rscb"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 34,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "('zixzmzrnkvirr', -1506.8637359274674)"
+ ]
+ },
+ "execution_count": 34,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "kb, score = vigenere_frequency_break(rscb)\n",
+ "kb, score"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 35,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "'inhisjournaldatedtheidesofoctoberintheyearoftheconsulshipsofcaeceliustulliuscapitopomponianusplotiusfirmusandgaiuscorneliusgallicanusagricolawrotethemysteryofthebattleatcamulodonumisatlastsolvedcalgacusmaybeabarbariannowbuthewasaromancitizenthenwhobetrayedusallforloveofabarbarianithastakenallmyskillsasaleaderofmentokeephimalivethelegionnairesspendtheireveningsdesigningnewandcruelwaystoexecutehiminrevengefortheshamehebroughtuponusbuthislifeispreciousitistheonlycardlefttoplayinoursearchforsalvationandthereturnofthestolenaquilaeifwecanalsorecoverthecodexthenperhapsitslosscanbeconcealedandourliveswillbesparedreleasingtheromantraitorcalgacusmusthavestuckintheproudagricolasthroatbuthemadeapactwiththeremainingcaledoniiandtravellednorthtoexchangetheprisonerfortheaquilaeandthecodexbutthecunningcaledoniantribesmansetanothertrapandpresentedagricolawithaforgerycunninglyassembledwithpagesfromthebooksstolenwhenthetriberansackedmonsgraupiusfortoolongthesonsofromehadunderestimatedthepeopleinbritanniaandwhiletheaquilaofthelegionhadbeenrestoredbytheexchangetheirhonourwasnotagricolafacedareturntoromehumiliationandalmostcertaindeaththesixthchapterofmytaleofwoeisguardedbylightningbullandoak'"
+ ]
+ },
+ "execution_count": 35,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "vigenere_decipher(rscb, kb)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 36,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "'arcanaimperii'"
+ ]
+ },
+ "execution_count": 36,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "cat(unpos(25 - pos(l)) for l in kb)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 37,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "in his journal dated the ides of october in the year of the consulships of caec eli us tullius\n",
+ "capito pompon ian us plot i us firm us and gaius cornelius galli can us agricola wrote the mystery\n",
+ "of the battle at camu lo donum is at last solved cal gac us maybe a barbarian now but he was a roman\n",
+ "citizen then who betrayed us all for love of a barbarian it has taken all my skills as a leader of\n",
+ "men to keep him alive the legionnaires spend their evenings designing new and cruel ways to execute\n",
+ "him in revenge for the shame he brought upon us but his life is precious it is the only card left to\n",
+ "play in our search for salvation and the return of the stolen aquilae if we can also recover the\n",
+ "codex then perhaps its loss can be concealed and our lives will be spared releasing the roman\n",
+ "traitor cal gac us must have stuck in the proud agricola s throat but he made a pact with the\n",
+ "remaining caledon ii and travelled north to exchange the prisoner for the aquilae and the codex but\n",
+ "the cunning caledonian tribesman set another trap and presented agricola with a forgery cunningly\n",
+ "assembled with pages from the books stolen when the tribe ransacked mons grau pius for too long the\n",
+ "sons of rome had underestimated the people in britannia and while the aquila of the legion had been\n",
+ "restored by the exchange their honour was not agricola faced a return to rome humiliation and almost\n",
+ "certain death the sixth chapter of my tale of woe is guarded by lightning bull and oak\n"
+ ]
+ }
+ ],
+ "source": [
+ "print('\\n'.join(tpack(segment(vigenere_decipher(rscb, kb)))))"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 38,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "1465"
+ ]
+ },
+ "execution_count": 38,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "open('5b.plaintext', 'w').write('\\n'.join(tpack(segment(vigenere_decipher(rscb, kb)))))"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 39,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": [
+ "def beaufort_encipher(message, keyword):\n",
+ " \"\"\"Beaufort encipher\n",
+ "\n",
+ " >>> beaufort_encipher('inhisjournaldatedtheidesofoctober', 'arcanaimperii')\n",
+ " 'sevsvrusyrrxfayyxuteemazudmpjmmwr'\n",
+ " \"\"\"\n",
+ " shifts = [pos(l) for l in sanitise(keyword)]\n",
+ " pairs = zip(message, cycle(shifts))\n",
+ " return cat([unpos(k - pos(l)) for l, k in pairs])"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 40,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "'sevsvrusyrrxfayyxuteemazudmpjmmwr'"
+ ]
+ },
+ "execution_count": 40,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "beaufort_encipher('inhisjournaldatedtheidesofoctober', 'arcanaimperii')"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 41,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": [
+ "beaufort_decipher = beaufort_encipher"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 42,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "'inhisjournaldatedtheidesofoctober'"
+ ]
+ },
+ "execution_count": 42,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "beaufort_decipher('sevsvrusyrrxfayyxuteemazudmpjmmwr', 'arcanaimperii')"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 43,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": [
+ "# def beaufort_sub_break(message, fitness=Pletters):\n",
+ "# best_shift = 0\n",
+ "# best_fit = float('-inf')\n",
+ "# for key in range(26):\n",
+ "# plaintext = [unpos(key - pos(l)) for l in message]\n",
+ "# fit = fitness(plaintext)\n",
+ "# logger.debug('Beaufort sub break attempt using key {0} gives fit of {1} '\n",
+ "# 'and decrypt starting: {2}'.format(key, fit,\n",
+ "# plaintext[:50]))\n",
+ "# if fit > best_fit:\n",
+ "# best_fit = fit\n",
+ "# best_key = key\n",
+ "# logger.info('Beaufort sub break best fit: key {0} gives fit of {1} and '\n",
+ "# 'decrypt starting: {2}'.format(best_key, best_fit, \n",
+ "# cat([unpos(best_key - pos(l)) for l in message[:50]])))\n",
+ "# return best_key, best_fit"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 44,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": [
+ "# def beaufort_frequency_break(message, max_key_length=20, fitness=Pletters):\n",
+ "# \"\"\"Breaks a Beaufort cipher with frequency analysis\n",
+ "\n",
+ "# >>> beaufort_frequency_break(beaufort_encipher(sanitise(\"It is time to \" \\\n",
+ "# \"run. She is ready and so am I. I stole Daniel's pocketbook this \" \\\n",
+ "# \"afternoon when he left his jacket hanging on the easel in the \" \\\n",
+ "# \"attic. I jump every time I hear a footstep on the stairs, \" \\\n",
+ "# \"certain that the theft has been discovered and that I will \" \\\n",
+ "# \"be caught. The SS officer visits less often now \" \\\n",
+ "# \"that he is sure\"), 'florence')) # doctest: +ELLIPSIS\n",
+ "# ('florence', -307.5473096791...)\n",
+ "# \"\"\"\n",
+ "# def worker(message, key_length, fitness):\n",
+ "# splits = every_nth(message, key_length)\n",
+ "# key = cat([unpos(beaufort_sub_break(s)[0]) for s in splits])\n",
+ "# plaintext = beaufort_decipher(message, key)\n",
+ "# fit = fitness(plaintext)\n",
+ "# return key, fit\n",
+ "# sanitised_message = sanitise(message)\n",
+ "# results = starmap(worker, [(sanitised_message, i, fitness)\n",
+ "# for i in range(1, max_key_length+1)])\n",
+ "# return max(results, key=lambda k: k[1])"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 45,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "('arcanaimperii', -1506.8637359274674)"
+ ]
+ },
+ "execution_count": 45,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "beaufort_frequency_break(cb)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": []
+ }
+ ],
+ "metadata": {
+ "kernelspec": {
+ "display_name": "Python 3",
+ "language": "python",
+ "name": "python3"
+ },
+ "language_info": {
+ "codemirror_mode": {
+ "name": "ipython",
+ "version": 3
+ },
+ "file_extension": ".py",
+ "mimetype": "text/x-python",
+ "name": "python",
+ "nbconvert_exporter": "python",
+ "pygments_lexer": "ipython3",
+ "version": "3.6.1"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 2
+}