Using the Ceasar Cipher encryption algorithm, you take each character in the original message
and shift it over a specified number of characters in the alphabet. If you shift the character A by
one space, you get the character B. If you shift the character A by two spaces, you get the
character C. The figure below shows some characters shifted by 3 spaces (A becomes C, B
becomes D, C become E, D becomes F, etc).
The number of letters to shift the characters in your message is called the encryption key.
If we want to send the message hello using a key of 3, then we can create the encrypted message
like this:
h shifted 3 is k
e shifted 3 is h
l shifted 3 is o
l shifted 3 is o
o shifted 3 is r
So our encrypted message would be khoor.
Think about how to create a program that will perform the Ceasar Cipher on a text message that
is entered by the user. The user will also enter the integer key that should be used for the
encryption. Your program should display the encrypted message at the end of the program.
(HINT: In the processing step, youll repeatedly use the shift algorithm on each character in the
message. Try to write the processing step so that it has a format similar to the way a loop looks
in Python, but dont worry about the details of how to perform the shift algorithm yet. Just note
Use Shift Algorithm to find character.)
Now lets learn how to implement the shift algorithm to calculate the encrypted value of that
character. First lets learn about how to convert a letter to its equivalent ASCII value. Even
when data is represented as character for humans, the computer likes to think of the characters as
numbers. The computer doesnt think about the character a as a letter. Instead it assigns a
number to each character and thinks about characters using their corresponding number. The
most common character to number translation used by a computer is called ASCII (which stands
for American Standard Code for Information Interchange). Heres a table that shows the ASCII
value for most characters.
Table 14-1: The ASCII Table
The good news is that you dont need to memorize the table. The computer already knows all
about how to translate a character into its ASCII value. We just need to know the command to
perform this translation.
To tell the computer that you want to use the ASCII value of a character, you use the ord( )
function to type cast the character value into an ordinal data type (which is an ASCII value).
EX:
character = input(Enter a character)
asciiEquivalent = ord(character)
print(The ASCII value of , character, is , asciiEquivalent)
Write a Python program that just includes these 3 lines to test out the conversion process for
yourself. Do you always get the ASCII value shown for the characters in the table above? (Its a
good sign if you do!)
Now lets think about how to use the ASCII value to achieve our Caesar Cipher shift. If we have
the ASCII value of a character, we can add the key to that value to find the ASCII value of the
encrypted character.
EX:
a has the ASCII value 97
if we use a key of 3 to encrypt a, we expect to get d as the encrypted value
if we add 3 to 97, we get a total of 100.
100 is the ASCII value of d
Add on to your program so that in addition to reading a character and finding the ASCII value,
the updated program also asks the user to enter a key and then calculates the ASCII value of the
encrypted character. If you run the program with the character a and a key of 3 as input, the
output should look like this:
Enter a char: a
Enter a key: 3
The ASCII equivalent of a is 97
Encrypting a with a key of 3 results in an ASCII value of 100
The final step needed in the shifting algorithm is to turn the ASCII value of the encrypted
character from a number back into a character. We can do this by type casting the result as a
character.
encryptedChar = chr(asciiValueOfEncryptedChar)
Add this line to the end of your program and display the output. Be sure that these steps all work
for a single character of input. You should be able to produce output like this:
Enter a char: a
Enter a key: 3
The ASCII equivalent of a is 97
Encrypting a with a key of 3 results in an ASCII value of 100
So the encrypted character is: d
Now were ready to add the repetition statement to our program. We want to repeat the shift
algorithm (that we created above) once for each character in the original message. Remember
that you can use the position of item in the list as a means of accessing that item.
EX:
Remember that the first item in a list is at position 0.
The 3rd item in the string message could be accessed by typing:
position = 2
character = message[position]
In the final version of your Caesar Cipher program, instead of printing the encrypted character at
the end of the shift step, concatenate it to a new string message in your program. After the loop
completes and all characters have been shifted, print the encrypted message.
When youve tested your program, turn in your .py file on Canvas. You only need to turn in your
IPO chart if you have questions about it or feel like you need help.

Using the Ceasar Cipher encryption algorithm, you take each characte.pdf

  • 1.
    Using the CeasarCipher encryption algorithm, you take each character in the original message and shift it over a specified number of characters in the alphabet. If you shift the character A by one space, you get the character B. If you shift the character A by two spaces, you get the character C. The figure below shows some characters shifted by 3 spaces (A becomes C, B becomes D, C become E, D becomes F, etc). The number of letters to shift the characters in your message is called the encryption key. If we want to send the message hello using a key of 3, then we can create the encrypted message like this: h shifted 3 is k e shifted 3 is h l shifted 3 is o l shifted 3 is o o shifted 3 is r So our encrypted message would be khoor. Think about how to create a program that will perform the Ceasar Cipher on a text message that is entered by the user. The user will also enter the integer key that should be used for the encryption. Your program should display the encrypted message at the end of the program. (HINT: In the processing step, youll repeatedly use the shift algorithm on each character in the message. Try to write the processing step so that it has a format similar to the way a loop looks in Python, but dont worry about the details of how to perform the shift algorithm yet. Just note Use Shift Algorithm to find character.) Now lets learn how to implement the shift algorithm to calculate the encrypted value of that character. First lets learn about how to convert a letter to its equivalent ASCII value. Even when data is represented as character for humans, the computer likes to think of the characters as numbers. The computer doesnt think about the character a as a letter. Instead it assigns a number to each character and thinks about characters using their corresponding number. The most common character to number translation used by a computer is called ASCII (which stands for American Standard Code for Information Interchange). Heres a table that shows the ASCII value for most characters. Table 14-1: The ASCII Table The good news is that you dont need to memorize the table. The computer already knows all about how to translate a character into its ASCII value. We just need to know the command to perform this translation.
  • 2.
    To tell thecomputer that you want to use the ASCII value of a character, you use the ord( ) function to type cast the character value into an ordinal data type (which is an ASCII value). EX: character = input(Enter a character) asciiEquivalent = ord(character) print(The ASCII value of , character, is , asciiEquivalent) Write a Python program that just includes these 3 lines to test out the conversion process for yourself. Do you always get the ASCII value shown for the characters in the table above? (Its a good sign if you do!) Now lets think about how to use the ASCII value to achieve our Caesar Cipher shift. If we have the ASCII value of a character, we can add the key to that value to find the ASCII value of the encrypted character. EX: a has the ASCII value 97 if we use a key of 3 to encrypt a, we expect to get d as the encrypted value if we add 3 to 97, we get a total of 100. 100 is the ASCII value of d Add on to your program so that in addition to reading a character and finding the ASCII value, the updated program also asks the user to enter a key and then calculates the ASCII value of the encrypted character. If you run the program with the character a and a key of 3 as input, the output should look like this: Enter a char: a Enter a key: 3 The ASCII equivalent of a is 97 Encrypting a with a key of 3 results in an ASCII value of 100 The final step needed in the shifting algorithm is to turn the ASCII value of the encrypted character from a number back into a character. We can do this by type casting the result as a character. encryptedChar = chr(asciiValueOfEncryptedChar) Add this line to the end of your program and display the output. Be sure that these steps all work for a single character of input. You should be able to produce output like this: Enter a char: a Enter a key: 3 The ASCII equivalent of a is 97 Encrypting a with a key of 3 results in an ASCII value of 100 So the encrypted character is: d
  • 3.
    Now were readyto add the repetition statement to our program. We want to repeat the shift algorithm (that we created above) once for each character in the original message. Remember that you can use the position of item in the list as a means of accessing that item. EX: Remember that the first item in a list is at position 0. The 3rd item in the string message could be accessed by typing: position = 2 character = message[position] In the final version of your Caesar Cipher program, instead of printing the encrypted character at the end of the shift step, concatenate it to a new string message in your program. After the loop completes and all characters have been shifted, print the encrypted message. When youve tested your program, turn in your .py file on Canvas. You only need to turn in your IPO chart if you have questions about it or feel like you need help.