


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



instructables
by rj
The Caesar Cipher is an ancient and widely used cipher that is easy to encrypt and decrypt. It works by shifting the letters of the alphabet over to create an entirely new alphabet (ABCDEF could shift over 4 letters and would become EFGHIJ). Caesar Ciphers are not the most secure ciphers out there but are good for small tasks such as passing secret notes or making passwords a little stronger. It is really easy to decipher the code, but it can be tedious to encrypt one if you don't have the special alphabet memorized. To make this process easier, we can use the power of computers, more speci*cally the programming language Python. This Instructable will show you how to create a program that converts messages into a cipher at your command. Supplies: All you need is a Python interpreter: IDLE, Pycharm, and Thonny are some good, free options (I used Pycharm) Basic knowledge of Python
To actually store the string (text) values of the alphabet, message, shift, etc. we need to use variables. We start by declaring the variables 'alphabet', 'partialOne', 'partialTwo', and 'newAlphabet'. I've written the names of the variables in Camel Case in my code (the *rst word is lowercase and second uppercase) but you can write it any way you want, as long as you remember to change it throughout the rest of the code too. The alphabet variable has the value "abcdefghijklmnopqrstuvwxyz". All of the other variables are set to "", which is an empty string as we do not yet have their values. What this is doing is setting up the Partial system, which is what we are using to actually create the shift. This will be explained in a later step. After this, we have to get the message and shift value from the user. We use the input function to do this. This part of the code asks the user for a message and a number to shift the alphabet by.
alphabet = "abcdefghijklmnopqrstuvwxyz" partialOne="" partialTwo="" newAlphabet="" message = input("Please enter the message you want to translate: ").lower() key = int(input("Please enter the number you want to shift by: "))
Isn't this one of the assignments from CS100 Introduction to Computer Science from Harvard?
Now we have our alphabet and the new alphabet. All that is left is to switch the message into the code. First, we set a new variable and call it 'encrypted' and set it to "". Then we write a really complicated for-loop which checks each letter in the message and switches it to the new letter. It outputs the result and there you have it, a successfully converted code! CODE: encrypted="" for message_index in range(0,len(message)): if message[message_index] == " ": encrypted+= " " for alphabet_index in range(0,len(newAlphabet)): if message[message_index] == alphabet[alphabet_index]: encrypted+= newAlphabet[alphabet_index] print(encrypted) Attached is the code *le. https://www.instructables.com/ORIG/FKT/YNX9/KDFYFB6O/FKTYNX9KDFYFB6O.py Download