From: Neil Smith Date: Fri, 10 Nov 2017 12:51:38 +0000 (+0000) Subject: Reimplementing Beaufort ciphers X-Git-Url: https://git.njae.me.uk/?p=cipher-tools.git;a=commitdiff_plain;h=f1a99f2d70045b6fd19ded876c9a7584099b0e18 Reimplementing Beaufort ciphers --- diff --git a/2017/2017-challenge5.ipynb b/2017/2017-challenge5.ipynb index aaee5b6..3160653 100644 --- a/2017/2017-challenge5.ipynb +++ b/2017/2017-challenge5.ipynb @@ -506,7 +506,7 @@ }, { "cell_type": "code", - "execution_count": 34, + "execution_count": 3, "metadata": {}, "outputs": [], "source": [ @@ -535,7 +535,7 @@ }, { "cell_type": "code", - "execution_count": 73, + "execution_count": 4, "metadata": {}, "outputs": [ { @@ -544,7 +544,7 @@ "1577" ] }, - "execution_count": 73, + "execution_count": 4, "metadata": {}, "output_type": "execute_result" } @@ -555,7 +555,7 @@ }, { "cell_type": "code", - "execution_count": 77, + "execution_count": 5, "metadata": {}, "outputs": [ { @@ -564,7 +564,7 @@ "26" ] }, - "execution_count": 77, + "execution_count": 5, "metadata": {}, "output_type": "execute_result" } @@ -575,7 +575,7 @@ }, { "cell_type": "code", - "execution_count": 35, + "execution_count": 6, "metadata": {}, "outputs": [ { @@ -598,8 +598,8 @@ " ('u', 38),\n", " ('l', 38),\n", " ('k', 35),\n", - " ('c', 34),\n", " ('b', 34),\n", + " ('c', 34),\n", " ('t', 33),\n", " ('z', 33),\n", " ('q', 32),\n", @@ -609,7 +609,7 @@ " ('d', 22)]" ] }, - "execution_count": 35, + "execution_count": 6, "metadata": {}, "output_type": "execute_result" } @@ -620,7 +620,7 @@ }, { "cell_type": "code", - "execution_count": 81, + "execution_count": 7, "metadata": {}, "outputs": [], "source": [ @@ -629,7 +629,7 @@ }, { "cell_type": "code", - "execution_count": 83, + "execution_count": 8, "metadata": {}, "outputs": [ { @@ -638,7 +638,7 @@ "[('i', 'n'), ('n', 'o'), ('o', 'o'), ('o', 'a'), ('a', 'd')]" ] }, - "execution_count": 83, + "execution_count": 8, "metadata": {}, "output_type": "execute_result" } @@ -649,7 +649,7 @@ }, { "cell_type": "code", - "execution_count": 85, + "execution_count": 9, "metadata": {}, "outputs": [ { @@ -658,7 +658,7 @@ "('fwruffnquevmp', -1750.4576000271347)" ] }, - "execution_count": 85, + "execution_count": 9, "metadata": {}, "output_type": "execute_result" } @@ -670,7 +670,7 @@ }, { "cell_type": "code", - "execution_count": 87, + "execution_count": 10, "metadata": {}, "outputs": [ { @@ -679,7 +679,7 @@ "'nieyqmhcenwlqvchdporosweiohtsshreoihpsvrvusdcostmjezkdeytdhrtasupknropbklngeutgnssthjhhnowgbdwvucncdvofhbdltfpvobiuicirayodpwkpoutibtgcenuhpwachchnbrjydhsfvhqszeuvdcpswatvzmxhsiigkobvcagqcdikfstrvkfggbdkvcvstuvufienwiniasbcecmvdwemkwgtnsyjridowjamhuhnrvxssgewikqxpxhashvwvteulpavioctweavlhtikkkximoikdloikrwswfiojrynulrsgtoktknqcporlruoigivdpeddhrntdmrnucnrioiuetpdnftaipjrewjqtercxzvydhiswrtrneonknnfsyrifcdhedoweptjrechehcptdcehirohbcpniloorntrrrtohgeoandszehilxywfqkrgnphglvcojfbetcietpqmfetkalnahiwixdppersmrihrctseahkhtifbokasoozrjgvvkehjsufarunbrtiswzdmriwcrovhdsdeihdteiiustmjupvkhdiisibjloyrdpyxkusdlwfpsehvevdoiudppehzgvcewnhifrvkfggbdkbidptartnotloihpseehrdipeotmlwbcousicucctsktsrlritcanhpdmrehuinioiuuwirsxtanvjshfwyrkacfihfctdipytegvprdowhfldhycrqhfctswdbnagevitctsufsrofoccprycjgnifeikrthnowgcedfedjwiisdtihszeecfvlwjqgehoeicssaqflthagmncpvvifnreneoiioiulytddhuhkrtzsdpevphodehkctsvfhltophksiepsgcohnrnuseajettlhdwhiepjwcenbtbuecihpijncohouidiqjikpovimvsrfridohvchdporhrmhlpnikpacvjiswwgszeyxrcpragclkvxbporlruoigoviferifridicrskiporsyypwgprszenephnicczvttucvqesuiivqleesvfrhcfgchuswrocjslotcnxtiiswkoieatruninitradpaortylcouoahdpehgukcvlrmrafrntaovetrxvyinpenvniquglltisxgy'" ] }, - "execution_count": 87, + "execution_count": 10, "metadata": {}, "output_type": "execute_result" } @@ -690,7 +690,7 @@ }, { "cell_type": "code", - "execution_count": 88, + "execution_count": 11, "metadata": {}, "outputs": [ { @@ -699,7 +699,7 @@ "('vejgvvnkgwfol', -1750.4576000271347)" ] }, - "execution_count": 88, + "execution_count": 11, "metadata": {}, "output_type": "execute_result" } @@ -711,7 +711,7 @@ }, { "cell_type": "code", - "execution_count": 89, + "execution_count": 12, "metadata": {}, "outputs": [ { @@ -720,7 +720,7 @@ "'nieyqmhcenwlqvchdporosweiohtsshreoihpsvrvusdcostmjezkdeytdhrtasupknropbklngeutgnssthjhhnowgbdwvucncdvofhbdltfpvobiuicirayodpwkpoutibtgcenuhpwachchnbrjydhsfvhqszeuvdcpswatvzmxhsiigkobvcagqcdikfstrvkfggbdkvcvstuvufienwiniasbcecmvdwemkwgtnsyjridowjamhuhnrvxssgewikqxpxhashvwvteulpavioctweavlhtikkkximoikdloikrwswfiojrynulrsgtoktknqcporlruoigivdpeddhrntdmrnucnrioiuetpdnftaipjrewjqtercxzvydhiswrtrneonknnfsyrifcdhedoweptjrechehcptdcehirohbcpniloorntrrrtohgeoandszehilxywfqkrgnphglvcojfbetcietpqmfetkalnahiwixdppersmrihrctseahkhtifbokasoozrjgvvkehjsufarunbrtiswzdmriwcrovhdsdeihdteiiustmjupvkhdiisibjloyrdpyxkusdlwfpsehvevdoiudppehzgvcewnhifrvkfggbdkbidptartnotloihpseehrdipeotmlwbcousicucctsktsrlritcanhpdmrehuinioiuuwirsxtanvjshfwyrkacfihfctdipytegvprdowhfldhycrqhfctswdbnagevitctsufsrofoccprycjgnifeikrthnowgcedfedjwiisdtihszeecfvlwjqgehoeicssaqflthagmncpvvifnreneoiioiulytddhuhkrtzsdpevphodehkctsvfhltophksiepsgcohnrnuseajettlhdwhiepjwcenbtbuecihpijncohouidiqjikpovimvsrfridohvchdporhrmhlpnikpacvjiswwgszeyxrcpragclkvxbporlruoigoviferifridicrskiporsyypwgprszenephnicczvttucvqesuiivqleesvfrhcfgchuswrocjslotcnxtiiswkoieatruninitradpaortylcouoahdpehgukcvlrmrafrntaovetrxvyinpenvniquglltisxgy'" ] }, - "execution_count": 89, + "execution_count": 12, "metadata": {}, "output_type": "execute_result" } @@ -731,7 +731,7 @@ }, { "cell_type": "code", - "execution_count": 94, + "execution_count": 13, "metadata": {}, "outputs": [ { @@ -740,7 +740,7 @@ "'a'" ] }, - "execution_count": 94, + "execution_count": 13, "metadata": {}, "output_type": "execute_result" } @@ -751,7 +751,7 @@ }, { "cell_type": "code", - "execution_count": 95, + "execution_count": 14, "metadata": {}, "outputs": [ { @@ -760,7 +760,7 @@ "'zyxwvutsrqponmlkjihgfedcba'" ] }, - "execution_count": 95, + "execution_count": 14, "metadata": {}, "output_type": "execute_result" } @@ -771,7 +771,7 @@ }, { "cell_type": "code", - "execution_count": 97, + "execution_count": 15, "metadata": {}, "outputs": [ { @@ -780,7 +780,7 @@ "'hveheifhbiicuzbbcfgvvnzafwnkqnndivxopvpdionrsyrmjvjlkaehbrfsmvmtvkqrrftcyspatroqqnbndcyiqretamkaszhcaqidtaxmpfrvenkfimmihgrxnvgqtrmcpzsqzpygininbbstddlcomipnnqgqargdgmrkbijtxnubxpuzjzbizesjbvqmutztdzotjzktjvraioamqznxiwnstbedizjnbjurebqqhldegrzvnynjbsdzprnparcknlqxnmryaisrqjxquzevdcijkzsbmmkczinszckaxrmkvnnzzfwlmksajvrzcqdrkqsdfgvyobqfemifqqrjcoilkydqodhdevxbauvrqdmumxaorieubzrdxvrlcowvodkrsqgzzsizvmdvddrnigrzayrlmednqfhqcblgnvrrntkusntzwdqpoddtvypazkhaqgqneyixiiukmcsfngyktqeftzpdmqtupjzjrkdxsunenxybyvqmqtdmfsdcmjkntbmmplvvvmzwvmzzzzcfymmtfumostdtbnzfkydvmddgrccdbjcnapbmmsrmjvtvztbcmmubemtzmdathxksrckiivczbkqzjvxbbyvqwjzzsinsowitztdzotjzenbyrumpsgbbvxopvgqwrcmfivmjtrjsponmsshdcmdrcmxombkjsopkydzblmhevxbkrcdllmuhranozrmdtidpmfedcbfvwkezzfvgrzxizrwkddefedcmrhtqizqzeqdcmtfcmuagskuoxceehvdbmkvqyiqreszfaqrdnxnmkrmwqgqqkekkieuozbrqmkrnvoizbwizihkukawixdzvbgmevxbtprrablnkvqgdbygzobrrqfzdcmsfnsprfncrxrpvespbsdhsrbvvjrbsbcynefqmilghcpeaqkbygwexspbramjbpmwdvgiatzcveonbzdzbbcfgvcojxcvhvyqusraxdireceehxdkuovylzkilefgvyobqfegiaaqdeeonbfidlyxfgvrhxprefmqgqhiuyiwlivipmasrtbdkfcznxbqcreoocieswonydyhwdtzrsqlmmmunvhwjkbmosmheqovbykgmphjsyprvxkvqwclksryojnnfdqpfgziqoyjpchoeszhetlptcrmllzw'" ] }, - "execution_count": 97, + "execution_count": 15, "metadata": {}, "output_type": "execute_result" } @@ -792,7 +792,7 @@ }, { "cell_type": "code", - "execution_count": 98, + "execution_count": 16, "metadata": {}, "outputs": [ { @@ -801,7 +801,7 @@ "('zixzmzrnkvirr', -1506.8637359274674)" ] }, - "execution_count": 98, + "execution_count": 16, "metadata": {}, "output_type": "execute_result" } @@ -813,7 +813,7 @@ }, { "cell_type": "code", - "execution_count": 99, + "execution_count": 17, "metadata": {}, "outputs": [ { @@ -822,7 +822,7 @@ "'inhisjournaldatedtheidesofoctoberintheyearoftheconsulshipsofcaeceliustulliuscapitopomponianusplotiusfirmusandgaiuscorneliusgallicanusagricolawrotethemysteryofthebattleatcamulodonumisatlastsolvedcalgacusmaybeabarbariannowbuthewasaromancitizenthenwhobetrayedusallforloveofabarbarianithastakenallmyskillsasaleaderofmentokeephimalivethelegionnairesspendtheireveningsdesigningnewandcruelwaystoexecutehiminrevengefortheshamehebroughtuponusbuthislifeispreciousitistheonlycardlefttoplayinoursearchforsalvationandthereturnofthestolenaquilaeifwecanalsorecoverthecodexthenperhapsitslosscanbeconcealedandourliveswillbesparedreleasingtheromantraitorcalgacusmusthavestuckintheproudagricolasthroatbuthemadeapactwiththeremainingcaledoniiandtravellednorthtoexchangetheprisonerfortheaquilaeandthecodexbutthecunningcaledoniantribesmansetanothertrapandpresentedagricolawithaforgerycunninglyassembledwithpagesfromthebooksstolenwhenthetriberansackedmonsgraupiusfortoolongthesonsofromehadunderestimatedthepeopleinbritanniaandwhiletheaquilaofthelegionhadbeenrestoredbytheexchangetheirhonourwasnotagricolafacedareturntoromehumiliationandalmostcertaindeaththesixthchapterofmytaleofwoeisguardedbylightningbullandoak'" ] }, - "execution_count": 99, + "execution_count": 17, "metadata": {}, "output_type": "execute_result" } @@ -831,6 +831,26 @@ "vigenere_decipher(rscb, kb)" ] }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'arcanaimperii'" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "cat(unpos(25 - pos(l)) for l in kb)" + ] + }, { "cell_type": "code", "execution_count": 100, @@ -881,6 +901,79 @@ "source": [ "open('5b.plaintext', 'w').write('\\n'.join(tpack(segment(vigenere_decipher(rscb, kb)))))" ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [], + "source": [ + "def b2_encipher(message, keyword):\n", + " \"\"\"Vigenere encipher\n", + "\n", + " >>> vigenere_encipher('hello', 'abc')\n", + " 'hfnlp'\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": 22, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'sevsvrusyrrxfayyxuteemazudmpjmmwr'" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "b2_encipher('inhisjournaldatedtheidesofoctober', 'arcanaimperii')" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [], + "source": [ + "b2_decipher = b2_encipher" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'inhisjournaldatedtheidesofoctober'" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "b2_decipher('sevsvrusyrrxfayyxuteemazudmpjmmwr', 'arcanaimperii')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": {