Question: What is a Caesar Cipher? If we wanted to print the letter ' I ' , in Python we would write that as: print (
What is a Caesar Cipher? If we wanted to print the letter I in Python we would write that as:
printfruit
Strings are Really Numbers
At the beginning of the term, we learned that letters are actually interpreted as numbers. These numbers are derived by the ASCII table. As you will recall the a lower case a is actually assigned to The lower case letter z is assigned to The computer uses these numeric values in order to do comparisons. We are going to use them to formulate our cipher.
The Algorithm to Encrypt
Below is the algorithm we will use to write a program that will encrypt text entered by the user:
For index to length of string, step
Assign one letter to the variable oneletter
Determine the ASCII value
If asciilettervalue then ASCII equivalent of z
Blse if asciilettervalue then
Set newasciiivalue this sets all Zs to A
Else
Set newasciivalue asciilettervalue shift to next letter
End if
Assign letters to our new string based on the ASCII values
Translation to Python
The function below translates our algorithm above into Python so we can start to encrypt our messages. It is designed to take in to it a string message. It will then pass through each character of the string assigning the ASCII number. It utilizes the ord method in order to determine the ASCII numeric value. It also uses the chr method to take our ASCII numbers and convert them to text.
def encryptstrmessage:
#declare variables
newstring str
asciilettervalue int
oneletter str
newasciivalue int
#loop to assign new ASCII value
for index in range lenmessage:
oneletter messageindex
asciilettervalue ordoneletter
#checking if the letter is Z or Z
if asciilettervalue :
newasciivalue
elif asciilettervalue :
newasciivalue
#assigning a new ascii value if not Z or Z
else:
newasciivalue asciilettervalue
#creating the new string
newstring newstring chrnewasciivalue
return newstring
Finishing Up
Using this code, complete the following:
Write an additional function to decrypt a message
HINT: The body of this function is very similar to the encrypt function. You are simply doing the inverse of the encrypt function.
Build a main function that:
a Contains a loop that will asks the user if they want to encrypt text, decrypt text or end the program. This loop will continue to execute until the user decides to quit the program.
b If the user wishes to encrypt or decrypt text, the program will ask them what word they want to either encrypt or decrypt.
c If the user selects encrypt, main will call the encrypt function and pass into it the word the user wishes to encrypt.
d If the user selects to decrypt, main will call the decrypt function and pass into it the word the user wishes to decrypt.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
