Download presentation
Presentation is loading. Please wait.
Published byBrice Walters Modified over 6 years ago
1
Whatcha doin'? Aims: To understand the Caesar Cipher.
To encrypt and decrypt messages using this encryption technique. Objectives: All: Understand the principle of the Caesar Cipher and ASCII codes. Most: Encrypt, send, recieve and decrypt messages. Some: Consider the weakness of the Caesar Cipher as an encryption technique.
2
Plain-text: ABACUS Cipher-text: DEDFXV Caesar Cipher
The Caesar Cipher (named after Julius Caesar) is a very simple method of encryption. Every letter in the message is shifted up or down the alphabet exactly the same number of places. Caesar often used a shift of three. If we use this shift on the word "ABACUS", we would get: Plain-text: ABACUS Cipher-text: DEDFXV
3
Modulus to the rescue We have used modulus (%) before, mainly to check if a number is divisible by another number. Modulus is also very helpful when we have situations where we might go beyond the end of a list. If you try to access an index beyond the end of a list, you get an error and your program will crash (probably).
4
Let's say we are enciphering the letter 'T' with a shift of 10.
One way we might do this is to have a list (or a string – same thing really) of the letters in the alphabet. If we ask for the letter at an index past the end of the list (or string), we know we'll get an error. So we use modulus. Get all the letters with a list comprehension. Find the index of 'T'. Add 10, but use the answer modulus 26 (ie. 3) to get the correct letter – 'D'.
5
First, I want to introduce a simple function.
I call it cipherWords() and it prepares a string in which the cipher-text is broken up into five-word lines of five-letter cipher words. This is helpful when we come to print out an enciphered message. When you have entered this, test it out with a message. We initialise msg to the first character of text. Then we add characters to msg, one at a time. We use the modulus operator to help us add a space after every 5 cipher characters and a newline after every 25 cipher characters.
6
Some Input Validation As you can see, this little function will only return if the user enters a valid number, I use the "raise ValueError" line to get the function to treat numbers that are out of range as an error - the same kind of error that is triggered if we try to apply int() to the wrong kind of input. This means I can treat both problems in the same way with one "except:" clause.
7
ASCII Code Our task is made a lot easier by the fact that the computer stores letters as numbers. You may have heard of ASCII codes. These are the numbers that were assigned to letters (and other characters) starting in the 1960s. Although other systems (such as Unicode) are now in place, we can still think in terms of ASCII codes for simple applications such as this one. In Python, we can find the code of a letter with ord() and get the character associated with a number with chr(). The upper-case letters are between 65 and 90. The lower-case letters are between 97 and 122.
8
Another feature we are going to use in this program is join().
We use join() a bit like a list comprehension, but when we want to build up a string instead of a list. Here are a couple of examples: First, I selected only the upper-case letters with even codes. Then I took all characters from 65 to 122. Thirdly, I asked for just the letters between 65 and 122. You can use join() together with format() too.
9
You may have noticed I used the expression isalpha() in one of those join() instructions.
As you might expect, this returns “True” if the character in question is a letter. It works on strings too. Python has many helpful string methods. You can read about them on: Here are some examples and upper() and lower() too.
10
Everything else happens in main().
First, we set base to the value of 'A'. Usually, it will be 65, but this just makes sure we have the correct value. Then we get the shift from the user, relying on getNum() to do the validation. The next step is to find out if the user wants to decrypt or encrypt. If he or she is decrypting, we just need to use the inverse of the shift.
11
Now, down to business. First we take the upper-case version of only the letters that the user entered. We store them in msg. Then we build up the cipher-text. We take each character in msg and find its value with ord(). We subtract the base and add the shift. We divide by 26 and use the remainder. Finally we add the character corresponding to this number plus the base value. If the user was decrypting, we don't use cipherWords(). The message will not have any spaces, but hopefully should be readable!
12
*** Caesar Cipher *** Please enter a number from -26 to 26: 3 Enter 'd' to decipher, anything else to encipher: Please enter your message: If you go down to the woods today you're sure of a big surprise. If you go down to the woods today you'd better go in disguise. For every bear that ever there was will gather there for certain Because today's the day the Teddy Bears have their picnic. Here is your message, prepared for encryption: IFYOUGODOWNTOTHEWOODSTODAYYOURESUREOFABIGSURPRISEIFYOUGODOWNTOTHEWOODSTODAYYOUDBETTERGOINDISGUISEFOREVERYBEARTHATEVERTHEREWASWILLGATHERTHEREFORCERTAINBECAUSETODAYSTHEDAYTHETEDDYBEARSHAVETHEIRPICNIC Here is the enciphered message: LIBRX JRGRZ QWRWK HZRRG VWRGD BBRXU HVXUH RIDEL JVXUS ULVHL IBRXJ RGRZQ WRWKH ZRRGV WRGDB BRXGE HWWHU JRLQG LVJXL VHIRU HYHUB EHDUW KDWHY HUWKH UHZDV ZLOOJ DWKHU WKHUH IRUFH UWDLQ EHFDX VHWRG DBVWK HGDBW KHWHG GBEHD UVKDY HWKHL USLFQ LF
13
Just to prove it works ... *** Caesar Cipher *** Please enter a number from -26 to 26: 3 Enter 'd' to decipher, anything else to encipher: d Please enter your message: LIBRX JRGRZ QWRWK HZRRG VWRGD BBRXU HVXUH RIDEL JVXUS ULVHL IBRXJ RGRZQ WRWKH ZRRGV WRGDB BRXGE HWWHU JRLQG LVJXL VHIRU HYHUB EHDUW KDWHY HUWKH UHZDV ZLOOJ DWKHU WKHUH IRUFH UWDLQ EHFDX VHWRG DBVWK HGDBW KHWHG GBEHD UVKDY HWKHL USLFQ LF Here is your message, prepared for encryption: LIBRXJRGRZQWRWKHZRRGVWRGDBBRXUHVXUHRIDELJVXUSULVHLIBRXJRGRZQWRWKHZRRGVWRGDBBRXGEHWWHUJRLQGLVJXLVHIRUHYHUBEHDUWKDWHYHUWKHUHZDVZLOOJDWKHUWKHUHIRUFHUWDLQEHFDXVHWRGDBVWKHGDBWKHWHGGBEHDUVKDYHWKHLUSLFQLF Here is the deciphered message: IFYOUGODOWNTOTHEWOODSTODAYYOURESUREOFABIGSURPRISEIFYOUGODOWNTOTHEWOODSTODAYYOUDBETTERGOINDISGUISEFOREVERYBEARTHATEVERTHEREWASWILLGATHERTHEREFORCERTAINBECAUSETODAYSTHEDAYTHETEDDYBEARSHAVETHEIRPICNIC
14
Task This version removes all non-alphabetic characters before encryption. I'd like you to alter the program so that any such characters are left unchanged by the encryption process. I'd also like to preserve the case of the letters. “Hi, I'm 4 today!” should encrypt to: “Kl, L'p 4 wrgdb!” Why is it less secure to keep the spaces and punctuation? Hint: how many one-letter words are there in English? Hints: You'll find these string methods useful! You probably won't use list comprehensions to do the encryption, a simple “for” loop is more appropriate for this task.
15
Extension Compose a message to a friend or classmate. Choose a secret shift and encipher it with the Caesar Cipher. Send the message to him or her (and secretly tell them the shift). See if he or she can decipher the message successfully. Ask someone to send you one! Extra Credit Try to intercept a secret message. Can you decipher it without knowing the shift that was used?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.