Docsity

Caesar Cipher Program in Python, Schemes and Mind Maps of Computer Programming

Toggle sidebar
bg1
instructables
Caesar Cipher Program in Python
by rj2006
Step 1: Declaring Variables and Getting Inputs
The Caesar Cipher is an ancient and widely used
cipher that is easy to encr ypt and decr ypt. I t works by
shif ting the letters of the alph abet over to create an
entirely new alphabet (ABCDEF could shift over 4
letters and wou ld become EFGHIJ).
Caesar Ciphers ar e not the most secure ciph ers out
ther e but are good for small tasks such as passing
secret notes or m aking passwords a little str onger. It is
really easy t o decipher the code, but it can be tedious
to encrypt one if you don't have the special alphabet
memorized .
To make t his process easier, we can u se the power of
compu ters, mor e specically the programming
language Python.
This I nstructable will show you how to create a
program that converts messages into a cipher at your
command.
Supplies:
All you need is a P ython int erpreter: IDLE, Pych arm,
and Thonny are som e good, f ree options (I used
Pycharm)
Basic kn owledge of Python
Caesar Cipher Program in Python: Page 1
pf3
pf4

Partial preview of the text

Download Caesar Cipher Program in Python and more Schemes and Mind Maps Computer Programming in PDF only on Docsity!

instructables

Caesar Cipher Program in Python

by rj

Step 1: Declaring Variables and Getting Inputs

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

Step 2: Creating the New Alphabet

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.

CODE:

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?

Step 4: Additional

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