--- /dev/null
+{
+ "cells": [
+ {
+ "cell_type": "code",
+ "execution_count": 1,
+ "id": "5c19999b",
+ "metadata": {
+ "Collapsed": "false"
+ },
+ "outputs": [],
+ "source": [
+ "from szyfrow.caesar import *\n",
+ "from szyfrow.affine import *\n",
+ "from szyfrow.keyword_cipher import *\n",
+ "from szyfrow.support.text_prettify import *"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 2,
+ "id": "d9dd1b5e",
+ "metadata": {
+ "Collapsed": "false"
+ },
+ "outputs": [],
+ "source": [
+ "challenge_number = 4\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": 6,
+ "id": "0f1f792a",
+ "metadata": {
+ "Collapsed": "false"
+ },
+ "outputs": [],
+ "source": [
+ "ca = open(ciphertext_a_filename).read()\n",
+ "cb = open(ciphertext_b_filename).read()\n",
+ "\n",
+ "sca = sanitise(ca)\n",
+ "scb = sanitise(cb)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 7,
+ "id": "c3cccd86-5e40-4040-8f91-9ca53ac6e909",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "archive KeywordWrapAlphabet.from_last \n",
+ "\n",
+ "harry iamba ckisp entmo stoft helas ttwow eeksi nthev aultt rying tounp ackso meoft hemys terys urrou nding theli ghtho useco nspir acycr ewand ithin kimad esome progr esswh ileiw asawa ythet raine estac kledt helet terfr omabc andth eydid agrea tjobt heyar eshap ingup welli think wewil lbegl adoft heirh elpas thisc asepr ogres sesth elibr arian atthe vault wasve ryhel pfula ndwhe nitol dherw hatiw asloo kingf orshe point edmet oanar chive ofvic toria npape rscon cerni ngsci entis tseng ineer sands ocial refor merst hatha dreco rdede arlyb ackgr oundi nvest igati onsin topeo pleof inter estis aypeo plebu tidid notic ethat there wasah eavyb iasto inves tigat ingwo menba ckthe niwas surpr iseda tfirs tihad assum edtha tthey would haveb eenla rgely ignor edbut isupp oseth esere marka blewo menma dethe irpre sence known andpe rhaps thees tabli shmen tfelt threa tened inmos tcase sther earen ostan dards ecuri tyfil esfor these women butwh ereth erewe rethe ywere atbes tthin there allyi ntere sting mater ialwa sinth ehidd enarc hivew herei found repor tsand paper sabou tlove lacen ighti ngale andab cases tabli shmen tfigu resth emsel vesth eysee mtoha vebee nigno redby these crets ervic eandc learl ynotc onsid ereda secur ityth reatb utour bossf orebe arsdi dcarr youti nvest igati onsin tothe iract iviti esand there arecl earsi gnsth atthe ymayh avewo rkedt ogeth erobv iousl ythef actth atthe ywere makin gsuch animp acton socie tywas enoug htoma kesom epeop lener vousa ftera llitw asati meofg reatc hange andco ncern sabou trevo lutio nwere veryr ealon eofth enote sonfi lesug gests thatt hegro wingc ampai gnfor women ssuff ragea ndsoc ialac tivis mmigh thave beent hesta rtofa ninsu rrect ionan ditna medse veral ofour prota gonis tsbut itdid ntsay howth eymig hthav einte ndedt otrig gerth eupri singw hatwe haves eenso faris thatw hatev erthe light house consp iracy wereu ptoth eywen ttoex traor dinar yleng thsto keepi tsecr etand thefa cttha twear eonly nowun cover ingit sugge ststh atitw asmuc hsubt lerth anapl anned upris ingia mstil lstru gglin gwith theid eatha tpeop lewho werep artof thebi rthof bossc ouldh avebe enact ingse cretl yagai nstus butth atisc ertai nlyim plied bysom eofth efile sweha vecra ckeda ndthe attac heddo cumen thasa ddedt omywo rries itwas writt enint heaft ermat hofth ehind enbur gdisa sterb ysome oneit hinky oukne wwell sheha dafan tasti creco rdwor kingd eepun derco veran drepo rting backt owest ernin telli gence agenc iesbu tthet hingt hatca ughtm yeyew astha there sheus edthe coden ameau gusta idont knowi fyoua lread yspot tedit butth elett ering aroun dthee dgeof theli ghtho usest ampre adsad aaddd aaada aadad andsu bstit uting theaa nddfo rdota nddas hspel lsout herco denam eitho ughta tfirs tthat imigh tbere ading toomu chint oitth elett erinh erfil eseem sneve rtoha vebee nbrok ensom eonew roted ouble encry ptedo nthee nvelo peand igues sthat wasen ought odiss uadet heage ncies froms pendi ngmuc htime onitw ithhi ndsig htiti spret tyobv ioust hatac tuall yitha sjust beene ncryp tedon ceand thene ncode dasmo rseco deusi ngthe samec onven tiona equal sdotd equal sdash oncet hatha dbeen untan gledi found itpre ttyea sytob reaka ndiam veryw orrie dabou twhat ifoun dithi nkyou needt otake alook jodie\n",
+ "\n"
+ ]
+ }
+ ],
+ "source": [
+ "(word_a, wrap_a), score_a = keyword_break(sca)\n",
+ "print(word_a, wrap_a, '\\n')\n",
+ "pa = keyword_decipher(ca, word_a, wrap_a)\n",
+ "print(pa)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 8,
+ "id": "c7f2e770-8062-4d18-b90a-cb2e410ef88c",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "harry i am back i spent most of the last two weeks in the vault trying to unpack some of the mystery\n",
+ "surrounding the lighthouse conspiracy crew and i think i made some progress while i was away the\n",
+ "trainees tackled the letter from abc and they did a great job they are shaping up well i think we\n",
+ "will be glad of their help as this case progresses the librarian at the vault was very helpful and\n",
+ "when it old her what i was looking for she pointed me to an archive of victorian papers concerning\n",
+ "scientists engineers and social reformers that had recorded early background investigations into\n",
+ "people of interest i say people but i did notice that there was a heavy bias to investigating women\n",
+ "back then i was surprised at first i had assumed that they would have been largely ignored but i\n",
+ "suppose these remarkable women made their presence known and perhaps the establishment felt\n",
+ "threatened in most cases there are no standard security files for these women but where there were\n",
+ "they were at best thin the really interesting material was in the hidden archive where i found\n",
+ "reports and papers about lovelace nightingale and abc as establishment figures themselves they seem\n",
+ "to have been ignored by the secret service and clearly not considered a security threat but our boss\n",
+ "forebears did carryout investigations into their activities and there are clear signs that they may\n",
+ "have worked together obviously the fact that they were making such an impact on society was enough\n",
+ "to make some people nervous after all it was a time of great change and concerns about revolution\n",
+ "were very real one of the notes on file suggests that the growing campaign for womens suffrage and\n",
+ "social activism might have been the start of an insurrection and it named several of our\n",
+ "protagonists but it didnt say how they might have intended to trigger the uprising what we have seen\n",
+ "so far is that whatever the lighthouse conspiracy were up to they went to extraordinary lengths to\n",
+ "keep it secret and the fact that we are only now uncovering it suggests that it was much subtler\n",
+ "than a planned uprising i am still struggling with the idea that people who were part of the birth\n",
+ "of boss could have been acting secretly against us but that is certainly implied by some of the\n",
+ "files we have cracked and the attached document has added to my worries it was written in the\n",
+ "aftermath of the hindenburg disaster by someone i think you knew well she had a fantastic record\n",
+ "working deep undercover and reporting back to western intelligence agencies but the thing that\n",
+ "caught my eye was that here she used the codename augusta i dont know if you already spotted it but\n",
+ "the lettering around the edge of the lighthouse stamp reads ada add da a ada a a dad and\n",
+ "substituting the a and d for dot and dash spells out her codename i thought at first that i might be\n",
+ "reading too much into it the letter in her file seems never to have been broken someone wrote double\n",
+ "encrypted on the envelope and i guess that was enough to dissuade the agencies from spending much\n",
+ "time on it with hindsight it is pretty obvious that actually it has just been encrypted once and\n",
+ "then encoded as morsecode using the same convention a equals dotd equals dash once that had been\n",
+ "untangled i found it pretty easy to break and i am very worried about what i found i think you need\n",
+ "to take a look jodie\n"
+ ]
+ }
+ ],
+ "source": [
+ "pa = keyword_decipher(sca, word_a, wrap_a)\n",
+ "print(prettify(pa))"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 9,
+ "id": "6cecddd9",
+ "metadata": {
+ "Collapsed": "false"
+ },
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "3341"
+ ]
+ },
+ "execution_count": 9,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "open(plaintext_a_filename, 'w').write(prettify(pa))"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 11,
+ "id": "77e0eb5c-226d-4197-a687-4a5d725c6af0",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "morse_chars = 'a b c d e f g h i j k l m n o p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9'.split()\n",
+ "# morse_chars"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 12,
+ "id": "0c7912fb-f552-4933-9730-3784197fc729",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "morse_codes = '.- / -... / -.-. / -.. / . / ..-. / --. / .... / .. / .--- / -.- / .-.. / -- / -. / --- / .--. / --.- / .-. / ... / - / ..- / ...- / .-- / -..- / -.-- / --.. / ----- / .---- / ..--- / ...-- / ....- / ..... / -.... / --... / ---.. / ----.'.split(' / ')"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 14,
+ "id": "ae62967b-0c5d-4e14-adf2-8cad03ed6970",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "['.-', '-...', '-.-.']"
+ ]
+ },
+ "execution_count": 14,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "char_to_morse = {l: c for l, c in zip(morse_chars, morse_codes)}\n",
+ "morse_to_char = {c: l for l, c in zip(morse_chars, morse_codes)}\n",
+ "[char_to_morse[l] for l in 'abc']"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 25,
+ "id": "d5c24bcb-fda8-40f0-89ba-166380046fc9",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "'otjsjseifzotyijsfeajzmoafzkfacjtntjtegsaneyeceggaftwqifzoytygcaweayhczxbweptjqayhotjsjsajjsefetgaifzotyiftgvjsajzkfxtggtzyotwwlekycznefehlqzyezmjsebafjtegjzjseczymwtcjtsaneleeyalwejzhftbmeehtyjewwtieycejzjseoegjefyawwtegntajsetfaieyjsaffqayhagmafagtcayjewwsefexatygtiyzfayjzmzkfjfkebkfbzgeyzyejseweggjseftgvfexatygstiszmzyezflzjsgthegacdktftyijsegecfejjzajzxtcoeabzygeafwqayhoeotwwyeehjzacjmagjtmoeafejzbfeneyjjstgjsewzggzmjsestyheylkfibfegeyjehamalkwzkgzbbzfjkytjqayhtkgehtjjzxavegkfejsajjseawwtegkyhefgjzzhosajjseyartgcteyjtgjgoefeozfvtyizytsanejftehyzjjzbfznthesaffqotjsjzzxkcstymzfxajtzyajjstggjaiegtycetjtgcfkctawjzzkfxtggtzyjsajoexatyjatyjselawaycezmkycefjatyjqtjtgyzjajawwcweafoszxtisjotyjseykcweafafxgfacelkjzkfepbefteycezmjsexzyjsgayhqeafgweahtyikbjzjseifeajoafgkiiegjgjsajjsefaceotwwsanetjgzoyxzxeyjkxayhtjotwwjaveczygthefalweemmzfjlqawwzmkgtmoeszbejzhtfecjtjjzzkfahnayjaieakikgja'"
+ ]
+ },
+ "execution_count": 25,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "morse_table = ''.maketrans('AD', '.-')\n",
+ "cb_dd = cb.translate(morse_table)\n",
+ "cb_words = cb_dd.split(' / ')\n",
+ "cb_letters = cat(cat(morse_to_char[l] for l in w.split()) for w in cb_words)\n",
+ "cb_letters"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 27,
+ "id": "86085f32-1564-4f01-8d58-2786625a743f",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "alchemist KeywordWrapAlphabet.from_last \n",
+ "\n",
+ "with the growing threat of war our activities have necessarily grown in scale and complexity and\n",
+ "with that there is a growing risk that our mission will be uncovered by one of the parties to the\n",
+ "conflict i have be enable to drip feed intelligence to the western allies via their agent harry and\n",
+ "as far as i can tell he remains ignorant of our true purpose nonetheless the risk remains high of\n",
+ "one or both sides acquiring the secret to atomic weapons early and we will need to act fast if we\n",
+ "are to prevent this the loss of the hindenburg presented a fabulous opportunity and i used it to\n",
+ "make sure that the allies understood what the nazi scientists were working on i have tried not to\n",
+ "provide harry with too much information at this stage since it is crucial to our mission that we\n",
+ "maintain the balance of uncertainty it is not at all clear who might win the nuclear arms race but\n",
+ "our experience of the months and years leading up to the great war suggests that the race will have\n",
+ "its own momentum and it will take considerable effort by all of us if we hope to direct it to our\n",
+ "advantage augusta\n"
+ ]
+ }
+ ],
+ "source": [
+ "(word_b, wrap_b), score_a = keyword_break(cb_letters, fitness=Ptrigrams)\n",
+ "print(word_b, wrap_b, '\\n')\n",
+ "pb = keyword_decipher(cb_letters, word_b, wrap_b)\n",
+ "print(prettify(pb))"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 28,
+ "id": "d12a663c",
+ "metadata": {
+ "Collapsed": "false"
+ },
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "1097"
+ ]
+ },
+ "execution_count": 28,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "open(plaintext_b_filename, 'w').write(prettify(pb))"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "504ec2e2",
+ "metadata": {
+ "Collapsed": "false"
+ },
+ "outputs": [],
+ "source": []
+ }
+ ],
+ "metadata": {
+ "jupytext": {
+ "formats": "ipynb,md"
+ },
+ "kernelspec": {
+ "display_name": "Python 3 (ipykernel)",
+ "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
+}
--- /dev/null
+---
+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 (ipykernel)
+ language: python
+ name: python3
+---
+
+```python Collapsed="false"
+from szyfrow.caesar import *
+from szyfrow.affine import *
+from szyfrow.keyword_cipher import *
+from szyfrow.support.text_prettify import *
+```
+
+```python Collapsed="false"
+challenge_number = 4
+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()
+
+sca = sanitise(ca)
+scb = sanitise(cb)
+```
+
+```python
+(word_a, wrap_a), score_a = keyword_break(sca)
+print(word_a, wrap_a, '\n')
+pa = keyword_decipher(ca, word_a, wrap_a)
+print(pa)
+```
+
+```python
+pa = keyword_decipher(sca, word_a, wrap_a)
+print(prettify(pa))
+```
+
+```python Collapsed="false"
+open(plaintext_a_filename, 'w').write(prettify(pa))
+```
+
+```python
+morse_chars = 'a b c d e f g h i j k l m n o p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9'.split()
+# morse_chars
+```
+
+```python
+morse_codes = '.- / -... / -.-. / -.. / . / ..-. / --. / .... / .. / .--- / -.- / .-.. / -- / -. / --- / .--. / --.- / .-. / ... / - / ..- / ...- / .-- / -..- / -.-- / --.. / ----- / .---- / ..--- / ...-- / ....- / ..... / -.... / --... / ---.. / ----.'.split(' / ')
+```
+
+```python
+char_to_morse = {l: c for l, c in zip(morse_chars, morse_codes)}
+morse_to_char = {c: l for l, c in zip(morse_chars, morse_codes)}
+[char_to_morse[l] for l in 'abc']
+```
+
+```python
+morse_table = ''.maketrans('AD', '.-')
+cb_dd = cb.translate(morse_table)
+cb_words = cb_dd.split(' / ')
+cb_letters = cat(cat(morse_to_char[l] for l in w.split()) for w in cb_words)
+cb_letters
+```
+
+```python
+(word_b, wrap_b), score_a = keyword_break(cb_letters, fitness=Ptrigrams)
+print(word_b, wrap_b, '\n')
+pb = keyword_decipher(cb_letters, word_b, wrap_b)
+print(prettify(pb))
+```
+
+```python Collapsed="false"
+open(plaintext_b_filename, 'w').write(prettify(pb))
+```
+
+```python Collapsed="false"
+
+```
--- /dev/null
+harry i am back i spent most of the last two weeks in the vault trying to unpack some of the mystery
+surrounding the lighthouse conspiracy crew and i think i made some progress while i was away the
+trainees tackled the letter from abc and they did a great job they are shaping up well i think we
+will be glad of their help as this case progresses the librarian at the vault was very helpful and
+when it old her what i was looking for she pointed me to an archive of victorian papers concerning
+scientists engineers and social reformers that had recorded early background investigations into
+people of interest i say people but i did notice that there was a heavy bias to investigating women
+back then i was surprised at first i had assumed that they would have been largely ignored but i
+suppose these remarkable women made their presence known and perhaps the establishment felt
+threatened in most cases there are no standard security files for these women but where there were
+they were at best thin the really interesting material was in the hidden archive where i found
+reports and papers about lovelace nightingale and abc as establishment figures themselves they seem
+to have been ignored by the secret service and clearly not considered a security threat but our boss
+forebears did carryout investigations into their activities and there are clear signs that they may
+have worked together obviously the fact that they were making such an impact on society was enough
+to make some people nervous after all it was a time of great change and concerns about revolution
+were very real one of the notes on file suggests that the growing campaign for womens suffrage and
+social activism might have been the start of an insurrection and it named several of our
+protagonists but it didnt say how they might have intended to trigger the uprising what we have seen
+so far is that whatever the lighthouse conspiracy were up to they went to extraordinary lengths to
+keep it secret and the fact that we are only now uncovering it suggests that it was much subtler
+than a planned uprising i am still struggling with the idea that people who were part of the birth
+of boss could have been acting secretly against us but that is certainly implied by some of the
+files we have cracked and the attached document has added to my worries it was written in the
+aftermath of the hindenburg disaster by someone i think you knew well she had a fantastic record
+working deep undercover and reporting back to western intelligence agencies but the thing that
+caught my eye was that here she used the codename augusta i dont know if you already spotted it but
+the lettering around the edge of the lighthouse stamp reads ada add da a ada a a dad and
+substituting the a and d for dot and dash spells out her codename i thought at first that i might be
+reading too much into it the letter in her file seems never to have been broken someone wrote double
+encrypted on the envelope and i guess that was enough to dissuade the agencies from spending much
+time on it with hindsight it is pretty obvious that actually it has just been encrypted once and
+then encoded as morsecode using the same convention a equals dotd equals dash once that had been
+untangled i found it pretty easy to break and i am very worried about what i found i think you need
+to take a look jodie
\ No newline at end of file