[Repost] Caesar Cipher

[This was originally posted last year, on an old blog to promote computational thinking and programming in schools. I’ll be reposting these over the next month as I think they’re still useful to schools embarking on the new Computer Science course] caes

So I’ve been playing around some more with Python and finally, after plenty of reading, internet scouring and debugging, come up with a pretty cool caesar cipher for kids. I won’t pretend this is all my own, I found the skeleton of it here . Although, most pre-created versions were overly complicated and would be pretty difficult to teach and explain to students. The version I liked stumped me initially as it has some unnecessary code in it, which I’ve removed. If you’re wondering what the hell a Caesar Cipher is, it’s the most basic way to code a text string. You probably did it as a child at some point. Move each letter along by 1, or 2, or 3 etc. As long as they all move by the same amount. So abba, with a caesar shift of 1, would become bccb. You could sell it to the students as some kind of top secret mission, back story bond blah blah blah. jaws There’s not much code, but students need to have been introduced to the concept of ‘for’ loops, alongside the ‘if else’ statements from the last Python post. This project also introduces a few other cool things like the chr() and ord() functions. chr() (character) takes a numerical value and converts it into its text equivalent from an ASCII table. ord()  (ordinal) takes the ASCII value and converts it into its numerical equivalent (the complete opposite of chr() ). Confused yet? Well, start by reading about the ASCII table here or just take a quick glance at the table itself here. Essentially, this program will ask the user for a text string (word, sentence, whatever), then ask them to enter a caesar shift value (1-26). In the background, it will then convert each character in turn to its numerical equivalent in the ASCII table, then add the caesar shift value to it, then convert it back into its (new) text equivalent. scr Unfortunately I’m not quite good enough to write this code from nothing, and most of the examples I could find didn’t do, or look like they were doing what I just described – which would make it almost impossible to teach for secondary school students. Anyway, the code, and plenty of comments within the code, are below. Note, this program only works with CAPITAL LETTERS for the original text. You could easily do a lower-case version by changing the number values in the IF statements (using the ASCII table of course). To do one that can handle both lower and upper case letters would take a bit more work. Could be an extension task! code To copy and paste:

def main():
otxt = input(“Please enter your secret message: “)
caesar = int(input(“Please enter the Caesar Shift (1-26): “))
result = ”
for char in otxt:
x = ord(char)
x = x + caesar
if x > 90:
x = x – 26
elif x == 32 + caesar:
x = 32
result += chr(x)
print (otxt)
print (result)

__ >>> main() Please enter your secret message: ENJOY YOURSELVES Please enter the Caesar Shift (1-26): 17 ENJOY YOURSELVES VEAFP PFLIJVCMVJ >>>

One thought on “[Repost] Caesar Cipher

  1. To turn this into a nasty cipher you can include the space as a character. When a space is read change its code to 64 and add the caesar. When the output code is > 90 subtract 27. If the result is 64 then output the space character.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s