The Ultimate Hack: Using a Raspberry Pi 5 Cluster to Break the Enigma Code

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey robot makers hope you're having a good day so far so do you want to learn how to crack the Enigma code from World War II using some python and a cluster of raspberry pies however many pies you have available on your network then this is the show for you so uh let's dive straight in my name is Kevin come with me as we build robots and bring them to life with code and have a whole lot of fun along the way okay let's get over to our keynote and um have a not going it right at the very beginning there so let's go back to there there we go so yes this is is all about how to crack the Enigma code so we're going to have some fun with some distributed um Raspberry Pi python uh but before we do that we're just going to have a quick word from our sponsor and then we'll get right straight to it I promise this video is sponsored by PCB way your ultimate destination for all things PCB manufacturing and assembly whether you're a hobbyist a startup or a season professional PCB way has got you covered PCB way offers an impressive range of services they provide high quality customdesign printed circuit boards for for any application you can imagine from single layer to multi-layer flexible and even rigid Flex pcbs they have the expertise to bring your designs to life PCB way ensures fast turnaround times and affordable prices without compromising on quality with their state-of-the-art facilities and advanced manufacturing techniques they can handle small prototype orders up to large scale production runs with equal precision and efficiency PCB way offers additional value added services such as PCB assembly component sourcing and even functional testing you can trust them to deliver the fully ass assembled and tested boards ready for integration into your projects one of the best parts of pcbaa is a userfriendly online platform it allows you to easily upload your designs get instant quotes and track the progress of your orders in real time plus their dedicated customer support team are ready to assist you with any questions or concerns so whether you're working on an Innovative Internet of Things device a robotics project or anything in between PCB way is your go-to partner for Reliable and affordable PCB manufacturing and assembly head over to PCB way.com today and turn your ideas into into reality with PCB way your trusted PCB manufacturing and assembly partner okay thanks again to PCB way for sponsoring today's episode right let's get straight to it shall we so yes we're going to be talking about encryption today decryption and how to crack the enima code with a raspby pi a bunch of raspby Pies in fact so very very quick history of encryption honestly we won't go over too much of this um already um so one of the earliest methods of encryption was the Caesar Cipher this was allegedly used by Julia Julia Caesar I'm not sure that it actually was but this is the one that's been attributed to to him to sort of send secret messages and it simply involves Shifting the alphabet uh by a number of different places and then using that that letter a bit like on this ring that you can see here so in this case if we have the message hello we could then um encrypt it as d a hhk uh so the H is being replaced by the D and so on now this is actually very easy to crack one you know that this is all it is because you've only got 26 combinations to go through so um it's not very very secure but um if users had a cursery glanded it you wouldn't really know what was being said there so it is something that was used very very early on probably when people weren't particularly literate as well so that was way back when um in the Middle Ages we had this um visionaire Cipher to uh go on to Google to to figure out how to pronounce that it's a French name so the uh visionaire Cipher was a lot more complicated than the Caesar Cipher if effectively you have a number of Caesar ciphers all lined up and you have a keyword and you simply repeat that keyword over and over and you use that same number of letters shifted uh for each one of the letters that you want to encrypt so for example in this one if our keyword was frog uh we would use the uh we would re rotate our Alphabet by five characters and then by whatever R is number of characters and then my o and so on and then we would simply use each one of those um to replace the characters um in our actual message so obviously the person receiving this has to know what the keyword is but once they have that they can they can decrypt your messages now transferring messages between people is always one of the challenges because if you if you imagine you're this um you're this King in this castle and you want to send a message to one of your um concierges who might be in a tower hundreds of miles away and you don't want anybody to intercept this message message how would you tell him what that that message is how could you deliver that message and it's one of these kind of um you remember that uh that mind puzzle that they you tend to learn in school about the the chicken it's the hen the seed and the fox and you've got to try and get them across a river it's the same kind of thing what we need to do is figure out a way of getting a message from one place to another without it being intercepted in between and it's it's it's difficult to do uh for a number of different reasons so one challenge that that was overcome by I think in this King and consort thing was the king um put a padlock on a chest and the message was inside the chest and the chest was very secure it made of uh the strongest material so they they were pretty sure that that wouldn't be able to be opened up that was sent across to the the conce the the the person at the other um the console the other um in the tower hundreds miles away he then attached his own padlock and sent it back uh then the king received it unlocked his padlock it was pad locked it went back with the um the I keep getting that name mixed up um with the uh the person who's receiving the message they have their padlock on and when they received it they could unlock it and get the message but that requires a message going backwards and forwards several times and it's not exactly ideal if you want quite a quick message uh being encoded and decoded so this Vision a one is quite a nice one because you just need to have that keyword and maybe you could get that one through a trusted Source or maybe they they could visit the castle get the message and then from there on they could have that to to decode it with that only works if people don't know that one so in World War II there was this uh Enigma machine that was uh created by the the German Army and this was a very very complex uh sequence of encryptions a bit like the visionaire but with many times over complexity we'll actually have a look at how it exactly does that and it uses electrical Pathways to encrypt the message uh and it there are astronomical numbers of possibilities of what the message could be encrypted so it makes it very difficult to manually decrypt this and they would change this every single day now what they did have so where we said before about having that frog or the the sort of keyword um each one of the officers who would have one of the machines to send or decrypt so encrypting or decrypted could be done on the same device but they would have to have a little book so they would have like a little imagine this is the uh the code book and each day would be um a different code sequence that they would have to type in so they it w as much typ it in as you would set the machine up uh with some rotor positions um the initial position that these rotors are in and the plugboard connections as well and we'll have a look at exactly what all those are in in a second so they would have that information and the books that they had so they had some of these in say the submarine and um they were made of ink that would uh dissolve if it came in contact with seawater so it wouldn't be of any use all you have to do if you are being captured is just make sure this gets submerged into water and then the enemy can't capture and have the all the codes there because one they just need one book to be able to decode everything now what's really cool about this is this this guy here Marian rusi he figured out without even seeing an enig machine how it worked which is that's quite mind-blowing that he actually managed to do this with his team so um he instead relied on intercepted messages some mathematical prowess and um the Ingenuity of is a crypto crypto analytic techniques so he figured this out without even ever seeing an Enigma machine now these Enigma machines were publicly available you could buy one the problem was having the codes and trying to decrypt it because just having the machine wasn't enough you would have to know what the rotor settings were which rotors were being used which reflector was being used we'll have a look at all these parts are in a second and also what the plug board combination is so even having a machine isn't good enough that doesn't tell you how to decrypt the messages and the number of permutations you could go through to try and crack a message like I said is astronomical so this guy figured it out first and then building on that work Alan shuring at Bletchley Park he devised a machine that could decrypt this um in within a day so that they could actually and the idea would be they would receive an Intercept in the morning using the latest days um encryption U settings they could run it through this bomb as they called it we'll get on to why we think it's called the bomb in a minute or two and um that would crunch through all the permutations and when it found uh match to a piece of text so they would have a piece of text that they would call a crib and a crib was basically like a short word like Hitler or Hy or submarine or British something that they could use to search uh to see if they've got a match because without that you could go right past the actual correct combination without knowing it so you need this little crib this little piece of short text to do a match within the entire possible messages that come through there so why is it called a bomb um so that guy um Marian rusi we looked at previously he made his own um Enigma machine by hand uh to try and figure out how he could possibly defeat the device and he was trying to come up with what he could call it so he called it a bomb um so some people say that it's um it stands for something but there there isn't actually a correct um reason why it's called call that but he simply just called it a bomb maybe because he thought it looked like one it looked like a very strange um device on his desk there so that name stuck and you can actually go and visit Bley Park in the UK just outside Milton keing we did this uh twice uh once last year and think in 2017 and you can actually see the room where Alan churing came up with all this you can see the bomb reconstructed and it sort of wors away and you can see a lot of their Enigma machines as well there's quite a lot of different varieties of enigma machines but that significantly meant that we couldn't uh we had victory in World War II because we were able to crack those German codes and actually understand without them knowing that we knew what they knew so this is the number of permutations that you have to go through to crack an enigma so the rotors we're going to have a look at what these rotors are in a minute but they're essentially a disc which has letters of the alphabet on inside there are some electrical connectors but within the rotor um you can you can have lots of different wiring so a letter A coming in might go to to a letter Zed for example uh coming out on those different um rotors there was lots of different types at least five different types of rotor and you could have them in different orders and you could have three uh per machine and then later I think they they expanded it to four rotors per machine so the number of possible rotor orderings there 60 the rotor positions so which positions are they going to be in so you could actually turn the rotors and set them so there's 17 1 half thousand different positions that those three rotors could be in and then there is these plugboard connectors so you can you can then manually up to 10 different um handwind connections to again just make a different letter go to another letter pair so that ends up with this very long number um you can see there it's just it's astronomical that's how many message encryption possibilities there are with Enigma so to try and do that every single day go through that that's not possible by a team of people um so they would have to come up with a method that could that could crack this that didn't involve people manually trying to work this out um like a crossword puzzle so let's have a look at how this actually works we're going to look inside the Enigma machine so we start out with the base this is the bottom of the machine it's sort of a little steel base there with a lots of different parts on it uh we've got a a battery pack so this is before the days of um electrical appliances being regular in in offices and households you just had to have a battery probably like a 12vt battery uh we have a rock so every time you hit a key on the keyboard um that is a bit like um hitting shift on a typewriter it would the entire Carriage would would tilt and that would make this little rocker mechanism turn one of the rotors one um one rotation round to the next letter so each time you press a key these rotors are turning and it's a bit like that uh visionaire Cipher with the uh the keyw changing each time you press a new key and that's why it's even more complicated because um if you if you pressed a a a you wouldn't get z z z out the other side you'd get some other letters as well so very very um complex mechanism there so that's the battery box and the rocker we've then got the rotors themselves there in this case we got three rotors and that larger thing on the side is the reflector so the power would come through the battery when you press a key you would you um make a circuit and that would go through each one of the rotors in turn to the reflector and then back again through the rotors one more time and then to the plug board at the front so next up we've got this switch assembly this is when you hit a key this is actually what makes the electrial connection uh and provide some power to the bulb above it as well got the keyboard looks a bit like an oldfashioned typewriter and weirdly they don't have a space bar on here so there is no space it's just 26 letters of the alphabet so instead of using space they often use the letter X to separate words cuz it's not very common in the alphabet is in names but not in the alphabet then we've got the lightboard so when you press a a key it makes all the electrical connections and a light will light up to tell you what the letter is that that's been encoded to so you would have somebody pressing the keys and somebody else next to them writing down what those um encrypted letters were so it didn't have to be one pair tend to work in pairs and we got the the lid there as well and then the plugboard at the very front you can see that is the um where you put these 10 cables in addition to um if you wanted to so you could when you put the plug in it would kind of short out um the the combination that was already in place you didn't have to use the plugboard but you could use up to 10 different combinations of that as well and then we've got this nice oak case that the whole thing goes inside as well so I found this CAD model on grabcad I was absolutely Blown Away with the the detail of it so I was very impressed that they' made this available for free so this is where I got this from from grabcad okay so the rotors themselves you can see that this is a reflector D I think we're going to be using reflector b in a lot of our codee in a second now within each one of these rotors there's also a slip ring and the sliit ring enabled you to further scrumble just by rotating and it would just change the position of the um the the letters inside and how they would scrumble so they had different positions that that could be in I think there's like five different positions that that slip ring could be in itself um you can see there there's these little electrical connectors so there's one for each letter of the alphabet and they would touch to make the electrical connector so you could take these um rotors out you could change the order that they're in and there's five possible rotors that you can put in there 1 two 3 four five but you could have them in like 4 3 2 or 1 five two whatever the different order you wanted to and there's a few different reflectors as well again just to scrumble the order even further okay so this is how the encoding works so you press the W key on the keyboard that um gets power from the battery uh and then the plugboard will then convert that W to a u U rotor one takes that U changes it to an X rotor 2 takes that X changes it to a g the next one takes it from a g to an i the reflector changes a i to a u that U then becomes an s on the rift rotor three and so on back to the plugboard and that gives you this letter G so w in this particular configuration gives you a g now once you've pressed that key it will change the order of all the the rotors because of that rocker mechanism uh and that again scrambles it so each time I think once rotor one's gone around 26 times it then rotor two changes and so on so um you get a very different combination and again the plugboard can further um add some complexity to that just by those 10 different cables that you can put on the front there so to be able to to defeat this we need to be able to test every single one of those permutations out with a piece of CRI text that's what they managed to do in World War II so if you like what I do and you want to make me more if you want me to make more of these types of videos please give this video a like drop me a comment let me know if you've got a cluster of raspberry pies um that's something I'm interested to know if other people have these uh and if you haven't already subscribed to the Channel please consider subscribing it costs nothing and it means a lot to me it helps me grow the channel and I do go live every single Sunday at 7:00 um GMT so if you want to catch me and say hello then you can do that on the live stream we have a Q&A at the end and a bit of a hangout after the main show okay let's get to some code ch so over here I have a visual studio code I'm just going to go sort of full screen with this and we're going to do some um some live coding now to try and encrypt a message so the first thing we're going to do is I've installed on this computer if I go down here if I do pip install Pi Dash uh Enigma uh that will install the Enigma Library so I've got this installed on here already so I'm going to say from um enigma machine and then I'm going to import Enigma machine then what I'm going to do is I'm going to set up that machine with the different settings that we need so machine is going to equal uh Enigma machine do from Key sheet and then the key sheet that's that little book that they would have in their submarines or wherever that would tell them what the settings of the day are so for today we're going to have um the rotor the rotors are going to be um let's do this so this is going to be i i v i i so going to use Rotator rotor 2 rotor 5 and rotor three in that sequence next we're going to have the reflector oops just type that wrong there sorry next we're going to have the reflector that's going to be um reflector B like I said there's a few different types of reflector there and then the ring settings these are these slip Rings we can also provide what the slip ring settings are in this case I'm just going to set them all to be one um just so that it's nice and simple for us and then the plugboard we can even provide what the plugboard connectors are so we have 10 connectors so we have to say what pair of letters these um these match up so a goes to v b goes to s c goes to g d goes to l f goes to u h goes to zed I goes to n k goes to m o goes to W and R goes to X now it doesn't have to have every single letter being plugged in uh if you don't plug something in it has a default setting on there so that's our machine setup uh we're now going to um just we want to basically rotate those rotors round uh and this's little window on top of the Enigma and you can see what's they're set to so what we need to do is just set that display so if we say machine do set display and we set this to qjf so we're just going to rotate them around so we can see QJ and F in that that order on the little window and then we've got some plain text that we want to encrypt so our plain text is going to be chelam so why do you think we would choose cheltam as our plain text let me know in the comments below and then our Cipher text going to take that plain text and we're going to process it so we say machine dot process text and we put in our plain text like so and then let's just print out what our Cipher text is like so right let's run this and see what we get oops uh it says I've got an error there let me just uh bring this up so we can see what the message is so it's the key sheet I've probably missed out a comma just after that rotor there there we go let's try that again uh so it doesn't know that type Ah that's because I put the commer in the wrong space there let's try that again okay so the encrypted text is this FK FP qz y v o n so there we go that's that's our encrypted message we can send that to somebody and they will be able to decrypt that if they had the exact same rotor reflector ring and plugboard settings so I know that's the correct answer that we should get there so we can move on to the next piece of code so we're going to try and decrypt this now so we're going to start off with that same block of text that we just created up there so from Enigma machine import Enigma the machine is going to be set the same from that key sheet so we're going to use the same rotors uh reflectors ring and plugboard settings and now and also set the display to be that qjf as well and this time we're going to say plain text equals let's just grab that um text that we just encrypted down there and we're going to say Cipher text equals machine. process text so it's the same function that we're using to encrypt or decrypt and that's one of the quirks of the Enigma machine we'll talk about that in a second and then we're just going to print out what the plain text is that we get back right let's just run this and let's see what we get uh so have I done that right Crypt text oh I typed that wrong so that should be the cipher text sorry and then the plain text should be like so so there's the cipher text and then we actually want to print out our plain text sorry about that and we need to type in Cipher text as well there okay so there we go so it's decrypted it and it's returned it back to chelam which is the the word that we originally encrypted with those particular settings so one of the weird things about the Enigma is each character so an a character will never be encoded as an a character it will always be in charact it will always be encoded as a different character and that means that um we can use that in the mathematical crypto analysis um of this it's one of the weaknesses of the Enigma machine it makes it half as effective as possibly could be um so there we go that's the the first um encoding and decoding so we're going to move on to now um encoding a a more World War II like um longer message so let's go over to here I've started out again with the the same thing here we've got the the same settings just say be typing this out and what we're going to do is we're going to create a function that's called pad string we're going to have an input that we we put into this and this is going to essentially um space out at the end of our message if it doesn't if it's not divisible by five we can't send out um letters in groups of five so it just makes it easier if we put like a bunch of x's at the end of the message whatever that might be so this is what this function is going to do so if we say padding needed equals oops equals and then it's the five minus the length of the input and then we want to then do a modulus of five so do you know what modulus does that will um divide it without a remainder so let's now do a padding string equals input plus X and then we times that by the padding needed like so and then we just going to return the padded string oops padded string there we go so if we just uh print out um let's just say padding pad string if I say this is a message and then I print pad string there we go and then we just run that you'll see that it's says this is a message XXX because it's padded the string so that it's divisible by five that's all that that pad string does we're going to have another one which will uh put some nice spaces in between so I'm going to call this one format for transmission now in reality they if if the um the message didn't it wasn't divisible Wi-Fi they would just stop this is just because it looks prettier for our code so input string and what we're going to do here is um a fancy function we're going to say return see if you can keep up with this this is a bit of an advanced python for you so we're going to say join together and then the input string and I am reading off a thing over here to the side just to make sure I get this right so I I + 5 and then we say for I in range zero to the length of the string which is the input string and then comma five so what this is going to do is it's going to format our message into blocks of five letters we can test this out uh down here so let's do that so let's say um print uh format for transmission and then let's pass in this is a test like so let's just run this and see what happens um so it's put it in two blocks of if we do a longer message let's say let's say that there was no spaces in this first of all so this is a test let's do it the way that they would do it they would have x's in between there we go so it's going to put it into blocks of five you can see there okay so we would use this format for transmission along with the pad strings so that we always have blocks of five letters at a time right so let's have a a plain text message now let's just get rid of that plain text equals and let's say I need to have all this in capital so hey robot makers this is a secret message there we go and we're going then we're then going to do plain text equals plain text and we're going to replace any spaces with an x capital x because again the Enigma machine doesn't have spaces uh let's just correct that typo there makers like so and then let's pad the string so plain text equals pad string and then the plain text itself and then the cipher text is going to become equal to the machine. process and then plain text oops and then finally we can use our format for um transmission and we can put in the cipher text like so right let's run this uh why is that not happy Cipher text we go let's run this and see what happens oops so process needs to have process text I always think uh auto complete going to help me for that there we go so we've now got this Su uh Su gpy CV blah blah blah blah bar and that looks like what you would see on an intercept from one of the listening stations uh to station X so that's uh that's that's properly encrypted because I know we we look at that in our next piece of code so that's how we would um format it for for transmission and we would send it out using some MSE code interestingly as well the messages that were being um intercepted of course in German so not only do they have to uh decrypt the message they also had to speak German and understand that they've got a message that has been um encrypted properly uh so what we're going to do now is we're going to use um kind of put some of this together and I'll show you this what this means you can see there that message I've actually had this prepared so I know that that was the correct encryption there we can get rid of that uh Cipher text there so we're going to import time I want to show you how fast this takes to do a brute force uh decryption of a message so this is what this Brute Force code is going to do it's going to try every single permutation out uh we're going to see how long it takes to do that so because I'm I'm suspecting that we might have the word robot in this piece of um encrypted text um so we're going to we're going to use the crib text of robot because I hope that that's going to be in there now sometimes in the intercepts they would try lots of different cribs hopefully they would find one of them things like weather was quite a common one or British might be in there um or some slight some insult might be in there as well so that's the crib text the message is what we've uh We've captured from our lising station and then I've just created a um dictionary there or list of all possible rotor positions so 1 two 3 1 two 4 one25 and so on so all the uh the different positions we can have we're going to just try this plugboard setting out um we would have to try all the plugboard settings and all the slip ring positions as well uh but for this just to make this a workable in a show that's live I'm just going to go with these ones to begin with so that's just a bit of caveat there now we previously looked at how to decrypt a message this is exactly the same as what we've seen before um all I'm putting in here is U some parameters is the rotor Choice which is if we go up there these different which which one of the five rotors we're using in what order so it's just going to accept one of those uh uh Keys like that the start position is those three letters which is what the rotters are starting from and then there's the actual Cipher text itself so that's that's how we decrypt a message um so that code there is pretty simple we just set up the machine we set the display to be the start position we make the plain text um get processed from the cipher text and then we just replace any X's with spaces just to make it easy for us to read as people now um this find rotor position we need to build this out this is going to be doing the the real work of the um this particular Brute Force thing so we're going to say we're going to do I think is a weird thing within the function we're going to do an include an import so from uh Enigma oops Enigma you don't normally do this you would normally have these at the top is the convention so import and I'll tell you why that is in a second import uh Enigma machine um done that wrong so Enigma do machine import Enigma there we go so the reason we're doing this is we're going to actually send this function off to our cluster and it's going to take that and any Imports that it requires it's going to it's going to take them from this line so that's why we actually do in here we're actually going to send this function off to some remote machines um so we're then also going to say the alphabet so let's uh hope that if I can type the word alphabet and let's see if we can get a a co-pilot to help us with this because it'd be quite laborious to type this in anyway it's not going to do that if g h i j k l m n p r s t wxy z hopefully I've not missed anything there so we need that to be able to test every single permutation of letter in our code we're then going to set up our machine so our machine equals um Enigma machine um from Key sheet and we're just going to do the the same thing we've done before so the rotors uh equals rotor choice that we passed in as a parameter we're going to set sorry that auto completes get in the way there we're going to set our reflector to be our reflector we're going to go with B on this one again this is a bit of a cheat we would have to go through every single combination of those as well our ring setting is going to be equal to our slip ring so uh slip ring which I think we've set to 111 at the top there and what we're going to then have as well is the plugboard settings so plugboard settings equal two and we're going to put in plugboard as well there so plugboard that's also a where did we put it at the top there the plug boards and also a variable or content at the top there as well right so that's going to set up our machine uh and now what we need to do is we need to go through each one of the rotors and test out have we got the the correct setting there so for rotor one let's create a variable call that in alphabet for RoR two in alphabet and you've guessed it for RoR three in alphabet we're then going to we're going to work out the start position is so the start position is going to be rout of 1 plus r of 2 plus Ro of three so that's going to be we're going to test out each one of those and see if we get a a correct result with that so then we need to do machine. display set or set display is the start position and then we're going to try and um decrypt that so we're going to say plain text equals and then machine. process text like we've done before to decrypt the message so uh that's the cipher text that we passed in as a parameter now we need to test does our crib appear in that um decrypted text so plain so we've done that one sorry so let's let's actually just print out what we think that plain text is and let's then also put in here if the crib text that we passed in is in the plain text then that means we've got a hit we can say print bade settings found Let's uh do that and let's do an exclamation mark because that's great and a couple of stars just so this sticks out and then we are then going to say decrypt the message using our um decrypt function I'm going to print out the rotor choices we going to the start position and also the mage message itself is and then we're going to return all those so the rotor choice and the start position CU we want those and if none of those things work if all fails we can basically just say um return rot to Choice and then cannot find settings okay so that's that let's have a look what else we've got so on here then I I also want to um replace so message equals message not replace any of those spaces uh with nothing and that start time let's uh put a function in there start time oh sorry I've already got that duplicated there right so this start time and this end time they're going going to capture what the time was when we start the code running and then when we finished might be 14 seconds I think it is around that kind of time um it's going to capture the end time and then it's going to say what duration is end time man is start time and then say The Brute Force attack completed in however many seconds we're just going to round it up to one one uh decimal point so this is running on a Mac M1 uh and if we've done everything correctly let's see if we can run this now you can see there it's going through all the different permutations that are possible with the rot Choice with the plugboard settings that we provided with the ring positions that we provided uh and also the do we say the rotors that we've selected for this particular machine so I think this takes around 14 seconds or thereabouts for this to go through everything on a uh a Mac M1 if I typed the code in correctly that of course CU if not it'll keep going for a very long time if you've seen the movie um with Benedict Cumberbatch in there the um what's it called the ini game the initiation game thank you Alex um that was the name of a test that Alan churing devised to try and figure out um could a machine look like a person so this imitation game was to try to figure out could you build an AI um that would behave like a person I don't think that's worked properly given the fact that it's still R oh no there we go so it it's taken 53 seconds to find that answer and you can see that the valid settings have been found hey robot makers this is a secret message and the position is 253 the start position is qjf and there we got it finished in 53.6 seconds that's quite a while for it to do it on even like a modern machine and we're not even testing out all the different possible plugboard settings or slip ring settings as well so takes quite a while even on a very fast machine so that's a Brute Force just using one machine so what we're going to try next is doing some distributed code this is what today is really all about so we're going to use a a function a library that's called dispy uh distributed python is how you would consider this so let's uh start up with some code to import dispy again You' have to do pip install disp to get this uh installed so I've done that already now I'm going to say I've got some nodes I've got this installed on eight raspberry pies um just behind me that are flickering away I think we can see them on the the other camera that we can put on in a second so this because this is running they kind of advertise their um their ability to run code remotely so if I say one do star and then I do another one this this is the IP address of the machines or the range of IP addresses so that's one subnet and that's the other subnet oops there we go so the star just means all possible numbers between zero on 254 I think that is right U we're going to have a little function that we're going to pass to our um our nose to actually run this one's going to be a really really simple function it's simply just going to add a number to a number that's passed in uh we don't want it to work too hard in this particular example so result equals num + one and then return the result so that's all that's going to happen uh and we'll talk through oops result there we go we'll talk through what's happening in a second We Now set up our cluster it couldn't be easier than this so cluster equals dis pi do job cluster and we pass in the function that we want to run which is called add number we pass in the nodes that we want to run this on so uh nodes equals nodes and that's that little uh range there so we've not even provided specific IP addresses it'll find them and it'll find them real quick and we can even have a log level if we want as well so we can just say this py do um logger um do debug I think that's right dispy do logger wonder why oh there we go got a type or dis P right okay so what we need to then do is just Define a little dictionary or list of um jobs it's got nothing at the moment and we're just going to have a variable called ID I'm going to set that to one so for n in let's just give a range of numbers let's do one to 100 so we want to run our add number 100 times um we're going to say job equals cluster oops do submit we're going to pass in the value that we want to to run which is this ad number it's going to receive whatever n currently is so it's going to go from 1 to 100 we then say job. ID equals ID we're going to say job jobs. append so that we've got a list of jobs that we can look at just going to append job to that so why is it not oh there we go that should be jobs up there that's our list there we go and we want to just increment ID so ID equals ID + one okay so that's going to submit all the jobs then we we then want to just have a little prompt that just says waiting for the jobs to complete and we can just get to cluster. weight for that so that we'll basically just wait until all the jobs have completed and then we can go through each one of those jobs and uh just get the status back so for job in jobs print and if we do a little F string we can say job and then job ID and we can say result is and then job. result so whatever that returns will be in job. result right so let's run this and let's see what happens now so if I go there so it run very very quickly let me just move this up a little bit so we can see here we've got a 100 jobs being submitted to our um to our cluster of raspberry pies so you can see there we've got all these messages saying receive job running job and then receive results and then the results come back so for job 41 the result was 42 cuz it added one to the number and so on now that's actually run on a cluster of raspberry pies we can actually if we scroll up to the very top there um oh actually we we need to do job do status for that so let's just add a piece of code at the very end of that um so let's say cluster dot think it's status and let's do clust Stop close as well let's run this again and let's scroll up to the top we should get like a nice little table that shows us what's going on that's what I'm looking for um I thought it was status maybe it's print maybe that's what I've missed out cluster. status let's try that there we go we need to have a brackets on there and there we go has that done it nope uh what is it called let me just have a sneaky peek at my other piece of code and I shall tell you what that is uh print status it's called so let's just do it's actually called cluster. print status there we go that's what I trying to get you get this nice little table up there you go that shows you all the different nodes that it ran on so we can see there we've got eight different nodes so Dev 1 two 3 four nodes 1 2 3 four they've got four CPUs each so that's uh what 16 possible U 32 possible cores is that uh and then number of jobs that's been run on each of those nodes interestingly that's got slightly less but the more powerful rasby Pi 5ivs can run a few more jobs like a third more jobs and the time it took for them to run on each node as well so we can see that it took 0. one of a second to run a 100 jobs to add 100 numbers together so we want to do this now but but instead of submitting that simple number thing we want to do the find rotor so that's what we want to do next to actually crack so if I go over to this last piece of code so this is the same as what we've seen before on our local Brute Force so we've got all the rotor settings there we've got our crib text as robot got the actual text that we want to decrypt we're going to replace the any spaces in our in our text um in these five groups just get rid of the spaces in there that's what that does our Cipher text is going then going to equal that process text our ring choice is going to be 111 and we've got that function there that's called decrypt message so that's that's going to decrypt our message and then we've got this find rotor start so we're going to pass in the rotor choice so which one of the rotor possible choices what's the ring Choice what's the cipher text what's the crib text um so this is very similar to what we did it it's exactly the same in fact of what we did before in our Brute Force locally but now we're adding to this the the node setup the creation of the cluster printing out that cluster status setting up our jobs like we did before and then collecting those results back so we're then going to say found equals false we don't know if you've found a match yet so we're going to go through each one of these um these jobs so if they've finished we can then make the rotor setting ring setting and position equal to what the current jobs results are um if it isn't finished then we basically just say the job isn't finished um and it could just fail that particular one if it is finished and we we've run that code we can that looks like a bit of a duplication of above to be honest so we probably need to get rid of one of them we can then say if a start position was found we're going to test for that so if the start position is not equal to cannot find settings then we found the setting and then we can decrypt what the message is using the road settings the start position and there's the cipher text and then we can print out what the actual message was um using those settings otherwise if we haven't found it the attack was unsuccessful and we can print the status out and close the cluster right so how many seconds do we think it will take to crunch through this so let's run this and see what happens next so I'm just going to move this up a little bit uh so 53 seconds is the the number to beat because that's how long it took to to work through so let's go through here and it's found four possible robots pieces of text in there so let's see if I just uh I just move me out the way for a second so you can see that that very top one the rot positions there it says Hey robot makers this is a secret message so it's actually decrypted it uh but there's also three other pieces of text that contain the word robot uh but they don't like valid messages so they would that was just um because we're looking through so many permutations in there so we now know what the the correct rotor settings are for that day now let's see how quickly this actually executed so it executed in 12.1 seconds so running it on eight raspberry pies with four cores each is faster than running it on a Mac M1 with how many cores this has is it four cores um so it's much much faster using a bunch of raspberry pies so these Dev ones they're all raspby pi fivs and the other ones are raspby Pi fours just out of interest and I think one of them is like a one gig and the rest are four or two gig I think uh whereas the raspby pi 5S are all 8 gig uh of memory so you can see that it ran really really quick 12.8 seconds 12.1 seconds in fact uh to to run that code so how cool is that uh that's pretty cool that we can crack the Enigma code very very quickly um so if we added to that the plugboard settings the sliit ring settings as well we' get a much more accurate um and the reflector settings as well we get much more accurate um decryption of the Enigma code now what's really weird is if you take this piece of code and you run one of the original pieces of enigma code um captured text one of the intercepts you can see some of the messages in German like you know British column of British troops detected or something like that it's quite creepy to see some of that um and there's at least three different um versions of the Enigma machine as well there's one for the Air Force One for the um the Army and one for the Marines as well for the for the Navy cool okay let's get back to our keynote and let's have a so we've talked about that already yes I do go live every single Sunday 7:00 GMT oh that looks like um the wrong thing I wanted to include in there let me just get rid of that sorry I thought I had this all set up there we go so you can buy merch um from the our store if you go to Kev robots.com merch you can pick yourself up one of these amazing hats we got them in a few different colors uh depending what time of year it is you can have have different hats I wore one of these hats when I was um on holiday in Italy and there was a this table the couple next to us and um I kept on this um guy's partner she kept sort of whispering something to him she was like he's got a hat on and after we'd finished eating and everything I think uh Jenny had gone um to the L or something like that wait for a second the woman sort of said do you work with robots cuz you've got this hat that says robot maker she says my husband here he works for um and then there's the name of the company which I can't remember um but it was ESS State a very big robotics company so I was having a chat about that um can't remember the name it was a a medical robot that he worked on da Vinci I think it was called something like that so these hats are a talking point we do have a Discord Channel as well if you want to hang over uh head over to KES robots.com Discord that link is now working again there was an issue with that last week I think when somebody pointed out I was just I was using a different version of um um the the software that builds the website and it clearly hadn't had that uh little redirect in there for Discord but that's all fixed now you can head over to Discord uh get a link there and I'm on social media as well so if you want to see some behind the scenes stuff that I'm working on have a look at some of the the things on my desk for example then I post pictures of all those um to different social medias so I'm on threads at Kevin M threads. net I'm Kevin mlea 6 on Tik Tok I'm a Kevin Mia on Instagram I'm kevmac on X and kevm masteron social on masteron and kevm ATB sky. social on Blue Sky so if you've not joined Blue Sky I think that's now open to everyone so check that out as well and if you want to get your name in the end credits you can do this by going to KES robots.com coffee uh and then you'll be able to get your name in the end credits like some people I'm going to show in a second if you want to also help out the show let me make sure these are all switched on now so we'll be able to see these um we have super thanks for people who want to watch this on replay and hit the thanks button if you want to uh do a super chat you're watching this live you can hit the the chat Super Chat button and get your name up in lights on the screen right now and if you hit the thank the join button on YouTube on the player it's just down there somewhere next to the Subscribe button then you can join the YouTube membership program as well it all helps support the show as well so yes let's have a look at some of those supporters right now shall we so we have uh people who bought coffees recently so Nicholas has Wayne and Steve Robinson have all bought coffees recently and we've got a new member in our Bame a coffee membership we've got Lee who joined uh this this last week so we've got Alvaro Diaz Goodman as well we've got Mary Lise Mayer Jeff Johnson Dean Cy Marin Brent Tom shmy and Steve Phillips hi to everybody that's on the stream right now and then on the YouTube membership side side we've got Dale from hydri robotics we've got Bill Hoy Warren steel Steven Cross John lamu we have Jonathan R arrad 39 Vince aliser wear um did I say jum Paul Jolly Cassie from um Cassie I can't remember if you have your robotics Channel yourself actually but got cassieopia there got tinkering rocks JDM Johnny bites hands from Cher lights Michael and of course we have Tom as well so yes that's uh I think everything I wanted to cover off on the main part of the show today so if you're watching this on replay this is the part of the video where I shall say thank you so much for watching and I shall see you next time
Info
Channel: Kevin McAleer
Views: 2,058
Rating: undefined out of 5
Keywords: Raspberry Pi 5 Projects, raspberry pi 5, raspberry pi, raspberry pi 5 cluster, pi cluster, dispy, raspberry pi cluster, enigma machine, cracking enigma, enigma, alan turing, how to crack the enigma code, python, py-enigma, raspberry pi projects
Id: cIq2AEbYotA
Channel Id: undefined
Length: 53min 53sec (3233 seconds)
Published: Mon Feb 19 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.