From 7191a262ea3a83098da7cdf08c24bf45571c3b3d Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Thu, 14 Oct 2021 15:41:50 +0100 Subject: [PATCH] Done challenge 2 --- 2021/2021-challenge2.ipynb | 205 +++++++++++++++++++++++++++++++++++++ 2021/2021-challenge2.md | 62 +++++++++++ 2021/ciphertext.2a.txt | 1 + 2021/ciphertext.2b.txt | 9 ++ 2021/plaintext.2a.txt | 1 + 2021/plaintext.2b.txt | 9 ++ 6 files changed, 287 insertions(+) create mode 100644 2021/2021-challenge2.ipynb create mode 100644 2021/2021-challenge2.md create mode 100644 2021/ciphertext.2a.txt create mode 100644 2021/ciphertext.2b.txt create mode 100644 2021/plaintext.2a.txt create mode 100644 2021/plaintext.2b.txt diff --git a/2021/2021-challenge2.ipynb b/2021/2021-challenge2.ipynb new file mode 100644 index 0000000..ee6f28b --- /dev/null +++ b/2021/2021-challenge2.ipynb @@ -0,0 +1,205 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "e89c0bd1", + "metadata": { + "Collapsed": "false" + }, + "outputs": [], + "source": [ + "from szyfrow.caesar import *\n", + "from szyfrow.affine import *" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "e328ffba", + "metadata": { + "Collapsed": "false" + }, + "outputs": [], + "source": [ + "challenge_number = 2\n", + "plaintext_a_filename = f'plaintext.{challenge_number}a.txt'\n", + "plaintext_b_filename = f'plaintext.{challenge_number}b.txt'\n", + "ciphertext_a_filename = f'ciphertext.{challenge_number}a.txt'\n", + "ciphertext_b_filename = f'ciphertext.{challenge_number}b.txt'" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "17cf9fb6", + "metadata": { + "Collapsed": "false" + }, + "outputs": [], + "source": [ + "ca = open(ciphertext_a_filename).read()\n", + "cb = open(ciphertext_b_filename).read()" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "0fd1afb3", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "((9, 23, True), -2276.8668346831637)" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "affine_break(ca)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "7f378aab", + "metadata": { + "Collapsed": "false" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "9 23 True \n", + "\n", + "Jodie, you were not placed with the Archaeologists as a punishment; everyone takes their turn on that shift because we never know what might turn up in old documents. When I ran the group, we found evidence of a Russian agent placed in the SDECE in the aftermath of the war who was acting as liaison with British and US intelligence. She had remained undiscovered for thirty-five years and leaked a whole load of information across the iron curtain. The team cracked a Fialka cipher from 1956, and that led to an arrest and confession. I admit that the case you are working on doesn’t seem likely to have the same impact, but you never know where it might lead. I took a look at the papers you sent, and I thought this one looked very interesting. The information it contains ties down the year it was written pretty conclusively, and I have to say I don’t like the sound of the phrase “profit from the Schleswig conflict”. I think it is clear who M must be, but I am still unsure about the others mentioned in these letters. I am also very unclear why M might have been involved in what looks like a conspiracy aimed at wartime profiteering. I checked, and there is nothing on file from the period to suggest that this was suspected. Indeed there was nothing on file at all, which, thinking about it is slightly strange. M was a public figure and of definite interest, so there should be something. If you have time it might be worth logging in to the records system to see if anyone else from around then has a suspiciously empty file. I would start with public figures who have the initials CH or FN, as the single letters A, T, E and W don’t give us much to go on. Of course, we do know a little more about W, so that might also be a good place to start. We know we are looking for an engineer with initial W, who knows something about codes and ciphers. Whoever FN is, they are right that they should talk to W. Using Caesar and affine shift ciphers to encode their secret communications is poor, and I would have expected better from an organisation containing M. Perhaps they will have taken the mysterious W’s advice and will have switched to something more secure in their future communications. That will make life a little more interesting for you and the team. Good luck, Harry\n", + "\n" + ] + } + ], + "source": [ + "(m_a, a_a, z_a), score_a = affine_break(ca)\n", + "print(m_a, a_a, z_a, '\\n')\n", + "pa = affine_decipher(ca, m_a, a_a, z_a)\n", + "print(pa)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "dda44618", + "metadata": { + "Collapsed": "false" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "2290" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "open(plaintext_a_filename, 'w').write(pa)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "0b9d3e6a", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "11 9 True \n", + "\n", + "My dearest M,\n", + "Can it really be sixteen years since Caroline first proposed this venture? I cannot believe it and wish with all my heart that she could have seen her vision taking shape.\n", + "Last Wednesday I took the opportunity you so kindly offered to see the progress with the foundation for myself. I was very encouraged by the industry on display, however I left with a concern that we may have miscalculated in our plans to hide the facility in the basement of the building. The ventilation there will, of necessity, be far more restricted than it would be on the upper floors, as I know from my own research. The evidence from your experiments suggests that our device will produce a very large amount of heat in operation. I cannot say for sure, but it seems likely that this will cause problems, both for the device itself and for its operators.\n", + "Looking at the plans, I wondered briefly if we could hide it instead on an upper floor of the lighthouse, but you are likely to have many visitors who are inclined to curiosity, and I suspect the secret would not stay safe for long. We cannot take any risk that might expose us, and even if we keep it behind a locked door, the noise it will generate in operation will propagate more easily across the wharf, leading to questions. In the basement we can at least suppress the noise, reducing that risk, but that leaves the issue of managing the heat.\n", + "I am at a loss. Any changes we want to make to the design of the laboratory and its facilities will need to be made very soon, as once the upper floors are built further heavy construction would attract unwanted attention. Perhaps W will be able to suggest something; he is, after all, an engineer. in any case, I was planning to ask him to join us as head of security. I understand that he has some rather interesting ideas about codes and ciphers.\n", + "\n", + "On a less pressing matter, our first experiments with the prototype are showing some success. If we had been able to run them earlier this year, we may even have been able to profit from the Schleswig conflict, and it may not be too late to do so if we can mobilise our agents. There is much to play for here. Others will be seeking to gain from the conflict, and I see no reason why we cannot do so too.\n", + "Ever yours,\n", + "FN\n", + "\n" + ] + } + ], + "source": [ + "(m_b, a_b, z_b), score_a = affine_break(cb)\n", + "print(m_b, a_b, z_b, '\\n')\n", + "pb = affine_decipher(cb, m_b, a_b, z_b)\n", + "print(pb)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "d7293627", + "metadata": { + "Collapsed": "false" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "2272" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "open(plaintext_b_filename, 'w').write(pb)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "404b70cf", + "metadata": { + "Collapsed": "false" + }, + "outputs": [], + "source": [] + } + ], + "metadata": { + "jupytext": { + "formats": "ipynb,md" + }, + "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.8.8" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/2021/2021-challenge2.md b/2021/2021-challenge2.md new file mode 100644 index 0000000..15a217f --- /dev/null +++ b/2021/2021-challenge2.md @@ -0,0 +1,62 @@ +--- +jupyter: + jupytext: + formats: ipynb,md + text_representation: + extension: .md + format_name: markdown + format_version: '1.3' + jupytext_version: 1.11.1 + kernelspec: + display_name: Python 3 + language: python + name: python3 +--- + +```python Collapsed="false" +from szyfrow.caesar import * +from szyfrow.affine import * +``` + +```python Collapsed="false" +challenge_number = 2 +plaintext_a_filename = f'plaintext.{challenge_number}a.txt' +plaintext_b_filename = f'plaintext.{challenge_number}b.txt' +ciphertext_a_filename = f'ciphertext.{challenge_number}a.txt' +ciphertext_b_filename = f'ciphertext.{challenge_number}b.txt' +``` + +```python Collapsed="false" +ca = open(ciphertext_a_filename).read() +cb = open(ciphertext_b_filename).read() +``` + +```python +affine_break(ca) +``` + +```python Collapsed="false" +(m_a, a_a, z_a), score_a = affine_break(ca) +print(m_a, a_a, z_a, '\n') +pa = affine_decipher(ca, m_a, a_a, z_a) +print(pa) +``` + +```python Collapsed="false" +open(plaintext_a_filename, 'w').write(pa) +``` + +```python +(m_b, a_b, z_b), score_a = affine_break(cb) +print(m_b, a_b, z_b, '\n') +pb = affine_decipher(cb, m_b, a_b, z_b) +print(pb) +``` + +```python Collapsed="false" +open(plaintext_b_filename, 'w').write(pb) +``` + +```python Collapsed="false" + +``` diff --git a/2021/ciphertext.2a.txt b/2021/ciphertext.2a.txt new file mode 100644 index 0000000..1023c79 --- /dev/null +++ b/2021/ciphertext.2a.txt @@ -0,0 +1 @@ +Ibgzp, nbd vpcp sbu kafxpg vzuq uqp Fcxqfpbabhzlul fl f kdszlqjpsu; pmpcnbsp ufrpl uqpzc udcs bs uqfu lqzyu opxfdlp vp spmpc rsbv vqfu jzhqu udcs dk zs bag gbxdjpsul. Vqps Z cfs uqp hcbdk, vp ybdsg pmzgpsxp by f Cdllzfs fhpsu kafxpg zs uqp LGPXP zs uqp fyupcjfuq by uqp vfc vqb vfl fxuzsh fl azfzlbs vzuq Oczuzlq fsg DL zsupaazhpsxp. Lqp qfg cpjfzspg dsgzlxbmpcpg ybc uqzcun-yzmp npfcl fsg apfrpg f vqbap abfg by zsybcjfuzbs fxcbll uqp zcbs xdcufzs. Uqp upfj xcfxrpg f Yzfarf xzkqpc ycbj 1956, fsg uqfu apg ub fs fccplu fsg xbsypllzbs. Z fgjzu uqfu uqp xflp nbd fcp vbcrzsh bs gbpls’u lppj azrpan ub qfmp uqp lfjp zjkfxu, odu nbd spmpc rsbv vqpcp zu jzhqu apfg. Z ubbr f abbr fu uqp kfkpcl nbd lpsu, fsg Z uqbdhqu uqzl bsp abbrpg mpcn zsupcpluzsh. Uqp zsybcjfuzbs zu xbsufzsl uzpl gbvs uqp npfc zu vfl vczuups kcpuun xbsxadlzmpan, fsg Z qfmp ub lfn Z gbs’u azrp uqp lbdsg by uqp kqcflp “kcbyzu ycbj uqp Lxqaplvzh xbsyazxu”. Z uqzsr zu zl xapfc vqb J jdlu op, odu Z fj luzaa dsldcp fobdu uqp buqpcl jpsuzbspg zs uqplp apuupcl. Z fj falb mpcn dsxapfc vqn J jzhqu qfmp opps zsmbampg zs vqfu abbrl azrp f xbslkzcfxn fzjpg fu vfcuzjp kcbyzuppczsh. Z xqpxrpg, fsg uqpcp zl sbuqzsh bs yzap ycbj uqp kpczbg ub ldhhplu uqfu uqzl vfl ldlkpxupg. Zsgppg uqpcp vfl sbuqzsh bs yzap fu faa, vqzxq, uqzsrzsh fobdu zu zl lazhquan lucfshp. J vfl f kdoazx yzhdcp fsg by gpyzszup zsupcplu, lb uqpcp lqbdag op lbjpuqzsh. Zy nbd qfmp uzjp zu jzhqu op vbcuq abhhzsh zs ub uqp cpxbcgl lnlupj ub lpp zy fsnbsp palp ycbj fcbdsg uqps qfl f ldlkzxzbdlan pjkun yzap. Z vbdag lufcu vzuq kdoazx yzhdcpl vqb qfmp uqp zszuzfal XQ bc YS, fl uqp lzshap apuupcl F, U, P fsg V gbs’u hzmp dl jdxq ub hb bs. By xbdclp, vp gb rsbv f azuuap jbcp fobdu V, lb uqfu jzhqu falb op f hbbg kafxp ub lufcu. Vp rsbv vp fcp abbrzsh ybc fs pshzsppc vzuq zszuzfa V, vqb rsbvl lbjpuqzsh fobdu xbgpl fsg xzkqpcl. Vqbpmpc YS zl, uqpn fcp czhqu uqfu uqpn lqbdag ufar ub V. Dlzsh Xfplfc fsg fyyzsp lqzyu xzkqpcl ub psxbgp uqpzc lpxcpu xbjjdszxfuzbsl zl kbbc, fsg Z vbdag qfmp pekpxupg opuupc ycbj fs bchfszlfuzbs xbsufzszsh J. Kpcqfkl uqpn vzaa qfmp ufrps uqp jnlupczbdl V’l fgmzxp fsg vzaa qfmp lvzuxqpg ub lbjpuqzsh jbcp lpxdcp zs uqpzc ydudcp xbjjdszxfuzbsl. Uqfu vzaa jfrp azyp f azuuap jbcp zsupcpluzsh ybc nbd fsg uqp upfj. Hbbg adxr, Qfccn diff --git a/2021/ciphertext.2b.txt b/2021/ciphertext.2b.txt new file mode 100644 index 0000000..130836f --- /dev/null +++ b/2021/ciphertext.2b.txt @@ -0,0 +1,9 @@ +Vx altylju V, +Ptg du yltkkx el jdmullg xltyj jdgpl Ptyrkdgl wdyju cyrcrjla usdj qlgufyl? D ptggru elkdlql du tga bdjs bdus tkk vx sltyu ustu jsl prfka stql jllg sly qdjdrg utzdgh jstcl. +Ktju Blagljatx D urrz usl rccryufgdux xrf jr zdgakx rwwlyla ur jll usl cyrhyljj bdus usl wrfgatudrg wry vxjlkw. D btj qlyx lgprfythla ex usl dgafjuyx rg adjcktx, srblqly D klwu bdus t prgplyg ustu bl vtx stql vdjptkpfktula dg rfy cktgj ur sdal usl wtpdkdux dg usl etjlvlgu rw usl efdkadgh. Usl qlgudktudrg uslyl bdkk, rw glpljjdux, el wty vryl yljuydpula ustg du brfka el rg usl fccly wkrryj, tj D zgrb wyrv vx rbg yljltyps. Usl lqdalgpl wyrv xrfy lmclydvlguj jfhhljuj ustu rfy alqdpl bdkk cyrafpl t qlyx ktyhl tvrfgu rw sltu dg rclytudrg. D ptggru jtx wry jfyl, efu du jllvj kdzlkx ustu usdj bdkk ptfjl cyreklvj, erus wry usl alqdpl dujlkw tga wry duj rclyturyj. +Krrzdgh tu usl cktgj, D brgalyla eydlwkx dw bl prfka sdal du dgjulta rg tg fccly wkrry rw usl kdhsusrfjl, efu xrf tyl kdzlkx ur stql vtgx qdjduryj bsr tyl dgpkdgla ur pfydrjdux, tga D jfjclpu usl jlpylu brfka gru jutx jtwl wry krgh. Bl ptggru utzl tgx ydjz ustu vdhsu lmcrjl fj, tga lqlg dw bl zllc du elsdga t krpzla arry, usl grdjl du bdkk hlglytul dg rclytudrg bdkk cyrcthtul vryl ltjdkx tpyrjj usl bstyw, kltadgh ur nfljudrgj. Dg usl etjlvlgu bl ptg tu kltju jfccyljj usl grdjl, ylafpdgh ustu ydjz, efu ustu kltqlj usl djjfl rw vtgthdgh usl sltu. +D tv tu t krjj. Tgx pstghlj bl btgu ur vtzl ur usl aljdhg rw usl kteryturyx tga duj wtpdkdudlj bdkk glla ur el vtal qlyx jrrg, tj rgpl usl fccly wkrryj tyl efdku wfyusly sltqx prgjuyfpudrg brfka tuuytpu fgbtgula tuulgudrg. Clystcj B bdkk el tekl ur jfhhlju jrvlusdgh; sl dj, twuly tkk, tg lghdglly. dg tgx ptjl, D btj cktggdgh ur tjz sdv ur ordg fj tj slta rw jlpfydux. D fgalyjutga ustu sl stj jrvl ytusly dgulyljudgh daltj terfu pralj tga pdcslyj. + +Rg t kljj cyljjdgh vtuuly, rfy wdyju lmclydvlguj bdus usl cyruruxcl tyl jsrbdgh jrvl jfppljj. Dw bl sta ellg tekl ur yfg uslv ltykdly usdj xlty, bl vtx lqlg stql ellg tekl ur cyrwdu wyrv usl Jpskljbdh prgwkdpu, tga du vtx gru el urr ktul ur ar jr dw bl ptg vredkdjl rfy thlguj. Uslyl dj vfps ur cktx wry slyl. Ruslyj bdkk el jllzdgh ur htdg wyrv usl prgwkdpu, tga D jll gr yltjrg bsx bl ptggru ar jr urr. +Lqly xrfyj, +WG diff --git a/2021/plaintext.2a.txt b/2021/plaintext.2a.txt new file mode 100644 index 0000000..9ab97ca --- /dev/null +++ b/2021/plaintext.2a.txt @@ -0,0 +1 @@ +Jodie, you were not placed with the Archaeologists as a punishment; everyone takes their turn on that shift because we never know what might turn up in old documents. When I ran the group, we found evidence of a Russian agent placed in the SDECE in the aftermath of the war who was acting as liaison with British and US intelligence. She had remained undiscovered for thirty-five years and leaked a whole load of information across the iron curtain. The team cracked a Fialka cipher from 1956, and that led to an arrest and confession. I admit that the case you are working on doesn’t seem likely to have the same impact, but you never know where it might lead. I took a look at the papers you sent, and I thought this one looked very interesting. The information it contains ties down the year it was written pretty conclusively, and I have to say I don’t like the sound of the phrase “profit from the Schleswig conflict”. I think it is clear who M must be, but I am still unsure about the others mentioned in these letters. I am also very unclear why M might have been involved in what looks like a conspiracy aimed at wartime profiteering. I checked, and there is nothing on file from the period to suggest that this was suspected. Indeed there was nothing on file at all, which, thinking about it is slightly strange. M was a public figure and of definite interest, so there should be something. If you have time it might be worth logging in to the records system to see if anyone else from around then has a suspiciously empty file. I would start with public figures who have the initials CH or FN, as the single letters A, T, E and W don’t give us much to go on. Of course, we do know a little more about W, so that might also be a good place to start. We know we are looking for an engineer with initial W, who knows something about codes and ciphers. Whoever FN is, they are right that they should talk to W. Using Caesar and affine shift ciphers to encode their secret communications is poor, and I would have expected better from an organisation containing M. Perhaps they will have taken the mysterious W’s advice and will have switched to something more secure in their future communications. That will make life a little more interesting for you and the team. Good luck, Harry diff --git a/2021/plaintext.2b.txt b/2021/plaintext.2b.txt new file mode 100644 index 0000000..04b753e --- /dev/null +++ b/2021/plaintext.2b.txt @@ -0,0 +1,9 @@ +My dearest M, +Can it really be sixteen years since Caroline first proposed this venture? I cannot believe it and wish with all my heart that she could have seen her vision taking shape. +Last Wednesday I took the opportunity you so kindly offered to see the progress with the foundation for myself. I was very encouraged by the industry on display, however I left with a concern that we may have miscalculated in our plans to hide the facility in the basement of the building. The ventilation there will, of necessity, be far more restricted than it would be on the upper floors, as I know from my own research. The evidence from your experiments suggests that our device will produce a very large amount of heat in operation. I cannot say for sure, but it seems likely that this will cause problems, both for the device itself and for its operators. +Looking at the plans, I wondered briefly if we could hide it instead on an upper floor of the lighthouse, but you are likely to have many visitors who are inclined to curiosity, and I suspect the secret would not stay safe for long. We cannot take any risk that might expose us, and even if we keep it behind a locked door, the noise it will generate in operation will propagate more easily across the wharf, leading to questions. In the basement we can at least suppress the noise, reducing that risk, but that leaves the issue of managing the heat. +I am at a loss. Any changes we want to make to the design of the laboratory and its facilities will need to be made very soon, as once the upper floors are built further heavy construction would attract unwanted attention. Perhaps W will be able to suggest something; he is, after all, an engineer. in any case, I was planning to ask him to join us as head of security. I understand that he has some rather interesting ideas about codes and ciphers. + +On a less pressing matter, our first experiments with the prototype are showing some success. If we had been able to run them earlier this year, we may even have been able to profit from the Schleswig conflict, and it may not be too late to do so if we can mobilise our agents. There is much to play for here. Others will be seeking to gain from the conflict, and I see no reason why we cannot do so too. +Ever yours, +FN -- 2.34.1