 Hey everyone, it's Mr. Gibson with the next lesson in cryptography and today we're going to be taking a look at how to program the tabular recta cipher here in Python. So to get it started, I opened up a new data hub page, I dropped in the code for the text clean function, our character to integer conversion function and our integer to character conversion function that we've seen before, and we're going to get to work here right away at programming the tabular recta function. So I'm going to go ahead and define that function. It's going to take in a piece of text, it's going to take in a key, it's going to take in an optional keyword argument of in cipher that will default to true, and then our letters, which will contain our 26 letter English alphabet by default. Just to get us going here. Remember the tabular recta cipher works by taking a single letter from a message and then a single letter from a running key, and then using the tabular recta to look up the either the plain text letter or the cipher text letter depending on the order you're using it. So we're going to have a goal here, we're going to document this in a documentation string is that our inputs are arguments. When we have our argument of text, which is a string, it's going to represent a single letter. And when we have key, which is a string, it to will just represent a single letter. So we're not going to try and do tabular recta here where it does the entire in ciphering or deciphering of a message for us, just a single letter at a time. We'll round out our documentation string here by saying in cipher, which is a Boolean, and then our letters, which is a string optional, just like the Boolean is optional. And the Boolean determines if in cipher or decipher decipher would be false, and in cipher would be true. And our optional is just the alphabet that the text is written in. And we'll check this typo over here. All right. And we know that it will return the idea of a tabular recta is that it's going to return some string. Okay, let's get to work on the actual code. So for the code here, if you think about it, we're going to want to make sure that we've got our single letter text, and we want to make sure that it is clean. And in this case, if it's just a single character, what that really means is we want to make sure it's capitalized. So, but we should run the text clean function because it'll also check to see is it a valid character and all those other good things you want to make sure that that are true about our text. And then I'm going to do the same thing for the key. I want to make sure that our key is formatted correctly, and that it's an appropriate letter that's in the alphabet. Okay. And we want this one function to be able to do both in cyphering and deciphering. So let's go ahead and create those two branches of code. Now, if in Cypher equals true for now, I'm just going to put a pass statement there. And then else will have another pass statement there. And then when we are done, we're going to want to return whatever output we just generated, which should be a string. So let's keep that in mind that we actually need to create that string somewhere along the way. So this function probably would run but it's not going to do anything. So let's go ahead here and actually put some meat here into this if statement. So if you think about what we want the tabular recta to do is we want it to take our text or single letter text and convert that to an integer and then do the same with the key and then add them together mod by the length of the alphabet and that will determine our new cypher text value. Remember, this is just a repeated Caesar cypher is all that the tabular recta does is that it just keeps reusing Caesar cypher over and over again. So that's what we're going to try and recreate here. This code will probably seem very familiar. So let's start by getting a numerical version of our plain text since we are in the encypher mode. We know that the text that we were given is plain text. So we'll do a character to integer conversion on that piece of text and store that to a numerical plain text variable. And now we're going to do the exact same thing with the key. We're going to take our single character key and run that through the character to integer conversion function. And then to get our numerical cypher text value, we're going to add those two together. So we're going to in parentheses do numerical plain text, add that to numerical key. And then we're going to mod by in theory 26. But let's just keep the code flexible here. Let's just say the length of letters. We know that that's 26 for us right now. But if we ever change this so that we have a different length alphabet, then this will change as well. So keeping it all at the date, very flexible. And now lastly, we want to get our cipher text character, we have the number for it. So we're going to do our integer to character conversion of our numerical cipher text value. And then that's what we're going to want to return eventually is that we're going to want to return the cipher text. So I guess we don't need that return at the bottom. All right, before we run just caught another typo, we should be doing text clean on the input value key and assigning that to key. So now we should be all set before we had texture and that wouldn't that wouldn't make any sense. So let's try this now, if we do the tabular recta function on the letter B, and we give it the letter D, we should know behind the scenes that B is one and D is three. So we add those two values together, we should get a cipher text value of E and we do. All right, so now we need to write the decipher function. So we'll go back here. And the decipher should be really similar. We're going to take our text only now when we're deciphering the text is cipher text, so we'll name that variable numerical cipher text. And that will take our character and convert it to integer of our text. And we'll do the same thing with the key numerical key equals char to int of the key. And to make our numerical plain text that we're creating through our deciphering, it's going to be very similar to what we did above. Only remember when we're deciphering, we subtract the key from the value of the cipher text plain text. And then we will still mod by however many letters are in our alphabet that we're using. So in this case, 26. And then lastly, we're going to get our plain text letter by taking the actual numerical plain text and do an int to character conversion on numerical plain text. And then that's what we'll return is the plain text back. So let's see if that's true. If we had a letter of E with a key letter of D, and we set our in cipher mode to false, we should get back to be which is correct because E is for D is three subtract the difference there and we get one and one is equivalent to B. So we have here is a functioning tabular recta code base here that takes a single character for the text a single character for the key and a Boolean to decide whether we're going to encrypt with that key or decrypt with that key. And it returns the resulting plain text or cipher text letter. So this will be a really helpful function for the rest of our tabular recta based ciphers like vision air, auto key, try themious, we'll just have to wrap this up in a larger function that is responsible for iterating through all the characters in the message one at a time. And then we can encipher each character using this tabular recta function. So it'll just be it'll do a lot of the legwork here for future functions that will write that really just need to make sure that you guys your text and your running key already ready to go and you can pass them off to this one one letter at a time and this will do all the rest. All right, that should do it. Thanks for watching. We'll catch you in the next one.