###### tags: `finished` :::success # SSN Lab 1 - Classical crypto ::: :::success ## Intro In this assignment, you will look at classical methods of encoding/decoding. Please keep your log detailed, give more than just an account of the questions/answers posed in this assignment. Especially during group work it should be clear from the logs who did what and why. ::: :::info **Preparation:** download WinXP virtual machine with the Codebook. ::: <center> ![](https://i.imgur.com/axo8VEX.png) Figure 1 - Codebook Content </center> :::info ### Task 1 Open the Codebook and look at everything up to and including Vigenere ciphers: choose “Main Contents” and go through the first three chapters of the “Birth of cryptography” up to and including “Mechanising secrecy”. ::: I decided to mark the chapters I read with highlights. ### Birth of cryptography: * Cryptography itself can be divided into two branches, known as ***transposition*** and ***substitution***. * In transposition, the letters of the message are simply rearranged, effectively generating an anagram. * In ***“railfence”*** transposition the message is written with alternating letters on separate upper and lower lines. <center> ![](https://i.imgur.com/nwqdW10.png) Figure 2 - Railfence puzzle </center> * ***Substitution cipher*** is a form of secret writing where each letter in the plaintext (the message before encryption) is substituted for a different letter to produce the ciphertext (the message after encryption), thus acting in a complementary way to the transposition cipher. In transposition each letter retains its identity but changes its position, whereas in substitution each letter changes its identity but retains its position. <center> ![](https://i.imgur.com/F07k8qj.png) Figure 3 - Caesar Shift Puzzle </center> > All Gaul is divided into three parts, one of which the Belgae inhabit, the Aquitani another, those who in their own language are called Celts, in our Gauls, the third. All these differ from each other in language, customs and laws. <center> ![](https://i.imgur.com/LGWRe6g.png) Figure 4 - Pigpen Puzzle ![](https://i.imgur.com/WMSm0iZ.png) Figure 5 - Atbash Puzzle </center> * Arab cryptanalysts managed to find a method of cracking the cipher of one-letter substitution, which consists in finding frequently repeated characters. <center> ![](https://i.imgur.com/W9aQvYf.png) Figure 6 - Frequency Analysis Puzzle </center> * Longer encrypted messages are easier to breake, because they are more likely to follow average frequencies. Shorter messages require more guesswork and imagination, and very short messages may be unbreakable. * ***Kerckhoffs’ Principle***: The security of a cryptosystem must not depend on keeping secret the crypto-algorithm. The security depends only on keeping secret the key. ### The Uncrackable Code: * The great advantage of the Vigenère cipher is that it is invulnerable to the frequency analysis and also the Vigenère cipher has an enormous number of keys. * The Vigenère cipher belongs to a class polyalphabetic because it employs several cipher alphabets per message. <center> ![](https://i.imgur.com/7O1Dhde.png) Figure 7 - Dancing Man Puzzle </center> * Babbage’s successful cryptanalysis of the Vigenère cipher was probably achieved in 1854, soon after his spat with Thwaites, but his discovery went completely unrecognized because he never published it. The discovery came to light only in the twentieth century, when scholars examined Babbage’s extensive notes. <center> ![](https://i.imgur.com/BLVFGCr.png) Figure 8 - Vigenere Cracking Puzzle </center> > Watching a coast as it slips by the ship is like thinking about an enigma. There it is before you--smiling, frowning, inviting, grand, mean, insipid, or savage, and always mute with an air of whispering, 'Come and find out.' This one was almost featureless, as if still in the making, with an aspect of monotonous grimness. The edge of a colossal jungle, so dark-green as to be almost black, fringed with white surf, ran straight, like a ruled line, far, far away along a blue sea whose glitter was blurred by a creeping mist. The sun was fierce, the land seemed to glisten and drip with steam. Here and there grayish-whitish specks showed up, clustered inside the white surf, with a flag flying above them perhaps. Settlements some centuries old, and still no bigger than pin-heads on the untouched expanse of their background. > > Heart of Darkness by Joseph Conrad ### The Mechanization of Secrecy: * Alan Turing identified Enigma’s greatest weakness and exploited it. Thanks to Turing, it became possible to crack the Enigma cipher under even the most difficult circumstances. * The known-plaintext attack (KPA) is an attack model for cryptanalysis where the attacker has access to both the plaintext (called a crib). * Turing proved that the crib placed severe constraints on the setup of the machine used to encrypt the message. In other words, it was possible to home in on the message key, and then the day key, the latter of which could be used to decipher other messages sent on the same day. :::info Encrypt an English text of at least 80 words using the Vigenere cipher and exchange it with one of your fellow students. ::: As a plaintext, I will use an excerpt from Howard Lovecraft's short story "The Dreams in the Witch House", I just like this work. The keyword is **CTHULHU**. > Gilman’s dreams consisted largely in plunges through limitless abysses of inexplicably coloured twilight and bafflingly disordered sound; abysses whose material and gravitational properties, and whose relation to his own entity, he could not even begin to explain. He did not walk or climb, fly or swim, crawl or wriggle; yet always experienced a mode of motion partly voluntary and partly involuntary. Of his own condition he could not well judge, for sight of his arms, legs, and torso seemed always cut off by some odd disarrangement of perspective; but he felt that his physical organisation and faculties were somehow marvellously transmuted and obliquely projected — though not without a certain grotesque relationship to his normal proportions and properties. <center> ![](https://i.imgur.com/8uXP9n9.png) Figure 9 - Vigenere Cthulhu tool </center> The resulting code will look like this: `IBSGLUMFKLUXZWQGZCDAYFEHLRLFABUJWBHIXZNSYIWZOFTTCVELMDHVALZYDVZKGLRASCETIFJJINHBLPKNYBSCRONCGKVLMZNBUAWFXKLVLOLLGWZIFUXCUFMDLMYAVMPTUVXYCLSUPWNLLCCVTACZUUNIYIALLVBLMLUXYAVMPYYNTACZUNQAPMZDHGGACEFBGVVOWKHQMLPPUVGZPHEVYZISUTUBGWPXYVNYTSEZYWNBTVQSSQKZQTTWTTDFZYQTBNAWLSGMHFHHSUXEJPYCGGJYOHGQWLIQTIVBVHAHLVEFPZSOPMHLJHHFIHLESSKGCIWBHVTYSZMBKLVQYJIPWPNTVHJXJIFSXPHAQPSFLNKAPMITLPASAIHAPMLYGUELADHHFMVLDVMGXTYOHFYTFMNBNQYMVJZIOXVXOKCUTYLLUAGFLHEVZRXYMALWVBCYMBNJXMYWANJTABTZJJRZCNHFQKNUYPMCMPIYHHFYHWFSNKXZQPYYUHTYSVQOTYPPSFQNZFJALCGZGFAYFTUXZIFKJBYWFJTHQYNAYFMOIFNBPHAQTABQNAUNLLVTPHRYIVXZKFLLGEHNTVHUAPJEVBKLUICTUNIYIAVLVBVHDHHFIYIALLVBLM` I gave my code to Alisher. :::info Crack the encrypted text of your fellow student using the Vigenere cipher tool. ::: I decided to use Vladimir's code only because he was the only one who reached this point at that time, without mysticism. > Grnp sdie s acofmrsr rlvsfs, > hzmwp G dievslpv, apli ohu osucq, > Sgpp aueq o kfsmye ybx tmfczmw gzjigv gt zzjkzersh cgfy, Hzmwp G biuvsx, ywecww bughwhr, kyoocbfp lvycw glxc o nrhdcyy, Ed zd gidw chp yiyejm lrhdcyy, vlanwhx sh gj ullxzsl ugcl. Eaw dzks pzkwnpj, M xfrhyiwr, nlhttye on dq qblefpc bcii-Gbfj lltd, ybx eghbtfk xzps. Uy, vwmearnejm C iwayxtic tr kuj ab nsw fwpyy Xvusgmwv, Lyb sutz gyasvlec rszfu yxtic hpcoxzh cek kszqh oggb nsw jwzmf. Yryslwq M htqvyu lvy xgvczu;-juzfzs T zeo dmiayl hi mgvczu Tlfe as mgsvd qiltwomp gj dzpfin-kclcga qzp hbv dcme Diyzps-Zfj hbp jecp ybx isrclfx xlgrye ovix llp lluyck buxw Ppymfy-Esaywwwd scfy wgf ygwvxzps. So after that I made code more easyli for decoder. > grnpsdiesacofmrsrrlvsfshzmwpgdievslpvapliohuosucqsgppaueqokfsmyeybxtmfczmwgzjigvgtzzjkzershcgfyhzmwpgbiuvsxywecwwbughwhrkyoocbfplvycwglxconrhdcyyedzdgidwchpyiyejmlrhdcyyvlanwhxshgjullxzslugceawdzkspzkwnpjmxfrhyiwrnlhttyeondqqblefpcbciigbfjlltdybxeghbtfkxzpsuyvwmearnejmciwayxtictrkujabnswfwpyyxvusgmwvlybsutzgyasvlecrszfuyxtichpcoxzhcekkszqhoggbnswjwzmfyryslwqmhtqvyulvyxgvczujuzfzstzeodmiaylhimgvczutlfeasmgsvdqiltwompgjdzpfinkclcgaqzphbvdcmediyzpszfjhbpjecpybxisrclfxxlgryeovixllplluyckbuxwppymfyesaywwwdscfywgfygwvxzps First, we will find the most frequently repeated sequences. As you can see from the screenshot below, the keyword can consist of 2 or 4 letters. <center> ![](https://i.imgur.com/r0xSfyB.png) Figure 10 - Frequently repeated sequences </center> But something is wrong, because the sequence of 3 letters is too small for the code. In this case, we will double-check the frequency of repetitions in another program: <center> ![](https://i.imgur.com/IKv8z3W.png) ![](https://i.imgur.com/PbpRQED.png) Figure 11 - Second frequently repeated sequences </center> Thus, we have a variant of the word of 6 letters and of 12. Before that, I checked 6 letters in the XP program, and they also did not fit, prepositions and conjunctions ("and", "a" and so on) did not fit into the sequence. <center> ![](https://i.imgur.com/nAiDaxp.png) Figure 12 - Spin the drum </center> Now it remains only to move the sequence in the hope of seeing a readable word. I think if Vladimir had written a code word with an error or used just a set of letters, the solution would have taken much longer because even without this, the frequency of repetition of letters did not have pronounced repetitions. That is, the percentage ratio of letters are dispersed on the graph and it is difficult to find the right sequence. Now that we know the key, it won't be difficult to decode the message: <center> ![](https://i.imgur.com/HdwquuU.png) Figure 13 - Decoding </center> > Once upon a midnight dreary, while I pondered, weak and weary, > Over many a quaint and curious volume of forgotten lore > While I nodded, nearly napping, suddenly there came a tapping, > As of some one gently rapping, rapping at my chamber door > Tis some visiter", I muttered, "tapping at my chamber door > Only this and nothing more. > Ah, distinctly I remember it was in the bleak December; > And each separate dying ember wrought its ghost upon the floor. > Eagerly I wished the morrow; - vainly I had sought to borrow > From my books surcease of sorrow - sorrow for the lost > Lenore - For the rare and radiant maiden whom the angels name > Lenore - Nameless here for evermore. > > The Raven by Edward Allan Poe :::info 4. Go through the previous two steps again, this time using a cipher of your own choosing. Do not tell your fellow student what cipher you used! ::: For my code, I'll take the text of a famous lullaby: > Twinkle, twinkle, little star, How I wonder what you are! Up above the world so high, Like a diamond in the sky. > When the blazing sun is gone, When he nothing shines upon, Then you show your little light, Twinkle, twinkle, all the night. I will use the Affine cipher, because of all the tools in Codebook, it seemed to me one of the most interesting among monoalphabetic ciphers, despite the fact that it is quite easy to decrypt it (but I don't really want to mock Alisher, who will solve this code). <center> ![](https://i.imgur.com/pb64xor.png) Figure 14 - The encryption process </center> In this form, I gave the code to Alisher: > XSYHMTW XSYHMTW TYXXTW QXUJROS Y SOHPWJ SRUX GOE UJWEV UBOLW XRW SOJTP QO RYKRTYMW U PYUAOHP YH XRW QMG SRWH XRW BTUNYHK QEH YQ KOHWSRWH RW HOXRYHK QRYHWQ EVOHXRWH GOE QROS GOEJ TYXXTW TYKRXXSYHMTW XSYHMTW UTT XRW HYKRX :::info And this the code I got from him: BokbglzkoztwggwnanhblnbobuubazgzrozowniiBwbokdrlnkbafnvnklbopzgwbkbttnaizfnenbakzobgbzihrbgdgnuunkrogrgwnmnabokbbokhbdgbantbitibohnrmnawzdkrlbzoGwnvjdnkgrdbvgwnlvdgzhdjdnkgrdbvgwbggwnanpbdbgzlnpwnogwnwbokdrlnlbopranorgbgbzihrbgejgbpzkninbgwnaenigpzgwuzdgriejggddgzhfzotrjgqarlzgbokwzdabmnowbzapbdgznkpzgwdhbaingdzifbokjoknawzdhrllbokbeaztdbzinkgwnHbazeenbojoknabeibhfknbgwqibtpzgwbdfjiibokhardderonEjgororGwndnkjhgzmnlvdgzhdbanivzotgwnanbanorHbazeenboDnbdzogwnpraikorkndunabgnqannerrgnadgrdbzigwnlorhramnggnhwbdndbqgnagwnlorhboorodlrfnkazqgdbharddgwnpbmndGwnanzdorgwzotbokgwnanpbdorgwzotGwnanzdgwbgdzhfivizoknormnagwnangwnanzdgwnhbdgzaroqnohnbokgwnerjinmbakenvrokzgBokgwnzhnzdlnigzotzogwnerpibokbggwnonsggbeinvrjdnndrlnrondeirrkdwrgermzonnvndbokvrjanbqabzkbqabzkRwtrkdlvtrkdurzdroeazotlnurzdro ::: So... Let's start sorting through all the tools that are in the Codebook (this is already good, the set of tools here is not so big). Let's start with the Caesar cipher: 1. I think it will be a cipher in English. 2. The Caesar cipher can be sorted manually by changing the alphabet shift, which can be seen in the picture below - just click through all 25 options in the hope that one of them will give a result. <center> ![](https://i.imgur.com/8aTlxIU.png) Figure 15 - Attempt 1 </center> Well, it's not a Caesar cipher. The following encryption algorithm is Affine. It is proposed to use frequency analysis to determine the greatest coincidence with the most common letters of the English alphabet "E", "A", "T", etc. <center> ![](https://i.imgur.com/vOaL904.png) Figure 16 - Frequency analysis </center> This is necessary in order to make sure once again that Alisher encrypted the English text, and now we are going back to manual key search. <center> ![](https://i.imgur.com/OJbIqfh.png) Figure 17 - At first it was even fun ![](https://i.imgur.com/fFvfcMP.png) Figure 18 - A lazy compliment from Alisher, I didn't even have to look at the second option, I hope he will look at my code longer. </center> We take the string and find the source text: > AndatmidnighttherecameanapparitioninhellAhandsomedarkeyedmanwithadaggerlikebeardinatailcoatsteppedontotheverandaandcastaregalglanceoverhisdomainTheyusedtosaythemysticsusedtosaythattherewasatimewhenthehandsomemanworenotatailcoatbutawideleatherbeltwithpistolbuttsstickingoutfromitandhisravenhairwastiedwithscarletsilkandunderhiscommandabrigsailedtheCaribbeanunderablackdeathflagwithaskullandcrossboneButnonoTheseductivemysticsarelyingtherearenoCaribbeanSeasintheworldnodesperatefreebooterstosailthemnocorvettechasesafterthemnocannonsmokedriftsacrossthewavesThereisnothingandtherewasnothingThereisthatsicklylindenovertherethereisthecastironfenceandtheboulevardbeyonditAndtheiceismeltinginthebowlandatthenexttableyouseesomeonesbloodshotbovineeyesandyoureafraidafraidOhgodsmygodspoisonbringmepoison > “ And at midnight there came an apparition in hell. A handsome dark-eyed man with a dagger-like beard, in a tailcoat, stepped onto the veranda and cast a regal glance over his domain. They used to say, the mystics used to say, that there was a time when the handsome man wore not a tailcoat but a wide leather belt with pistol butts sticking out from it, and his raven hair was tied with scarlet silk, and under his command a brig sailed the Caribbean under a black death flag with a skull and crossbones. But no, no! The seductive mystics are lying, there are no Caribbean Seas in the world, no desperate freebooters to sail them, no corvette chases after them, no cannon smoke drifts across the waves. There is nothing, and there was nothing! There is that sickly linden over there, there is the cast-iron fence, and the boulevard beyond it…And the ice is melting in the bowl, and at the next table you see someone’s bloodshot, bovine eyes, and you’re afraid, afraid…Oh, gods, my gods, poison, bring me poison!...” > > Mikhail Bulgakov, The Master and Margarita ## References: 1. [The code book : how to make it, break it, hack it, crack it / Simon Singh (2002)](http://www.it-docs.net/ddata/773.pdf) 2. ["The Dreams in the Witch House" H. P. Lovecraft](https://www.hplovecraft.com/writings/texts/fiction/dwh.aspx) 3. [Vigenere tool](http://www.brianveitch.com/maze-runner/frequency-analysis-vigenere/index.html) 4. ["The Raven" Edward Allan Poe ](https://akyla.net/stihi-na-angliyskom/edward-allan-poe/328-edward-allan-poe/4883-the-raven-voron-edward-allan-poe) 5. [Affine Cipher](https://www.dcode.fr/affine-cipher) 6. [Mikhail Bulgakov: quotes ](https://www.goodreads.com/quotes/165418-and-at-midnight-there-came-an-apparition-in-hell-a)