Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Caesar Cipher Encryption using Python in IDLE The Caesar Cipher is simple way to encrypt alphabetic letters. All remaining punctuation symbols, numeric digits, or other

Caesar Cipher Encryption using Python in IDLE The Caesar Cipher is simple way to encrypt alphabetic letters. All remaining punctuation symbols, numeric digits, or other characters (spaces) remain unchanged.

Encrypting a message using the Caesar Cipher involves replacing each letter in a message with the letter k places further down the alphabet, wrapping around at the end of the alphabet when necessary. With k = 0, each letter is replaced by itself. With k = 20, each letter is shifted 20 places down the alphabet. To decode or decrypt the text you simply shift the encrypted letter in the opposite direction by k places and wrap around as necessary.

The letters in the Caesar Cipher alphabet start at 0 and continue through 25, with the letter "A" being 0 and "Z" being 25. If the user were to choose k = 3, the letter "A" (0) would be replaced by the letter "D" (3), while the letter "B" (1) would be replaced by the letter "E" (4). If a letter appears towards the end of the alphabet, the alphabet simply wraps around and starts again. So the letter "Z" (25) would be replaced by the letter "C" (25 + 3 = 28), which is 2 after wrapping around. The "wrap around" math is accomplished simply using the "modulo" operator in Python (%) which returns the remainder after integer division.

If x is the position of some letter we are trying to encrypt, it should be replaced by the letter at the position denoted by (x + k) % 26, which will be a number between 0 and 25. For the above example, 25 + 3 = 28, and 28 % 26 = 2, or C. To decrypt you simply subtract k instead of adding: (x - k) % 26. To reverse the previous example we take the result "C" = 2, with a shift of 3, so (2-3) % 26 = 25 giving us back the letter "Z".

Shifting the Letters in Python

Recall (from Chapter 4) that our alphabet in Python is encoded using UTF-8, from Pythons perspective A is not 0. Instead, an upper case "A" is represented by the number 65, "B" by 66, and so on, with capital "Z" represented by 90. While a lower case "a" is represented by the number 97, "b" by 98, and so on, with "z" represented by 122. Your program will have to account for this when shifting the letters using the formula above. Note: your program should not change the case of the original text or message.

To obtain the UTF-8 values, you will need to use the built-in ord(char) function, which accepts a string containing a single letter and returns the integer value of the letter. You will also need to use the built-in chr(int) function which accepts an integer and returns the corresponding Unicode/ASCIIletter. The methods isupper() and islower() may also beu seful.

Further, any remaining punctuation symbols, numeric digits, or other characters (spaces) should be unmodified and remain unchanged in the message. Chapter 4 contains examples of how to identify and skip these characters while encoding, as a hint you may need to import string.

Implementation

Your program should begin by prompting the user for a message (a string), a shift amount (an integer), and whether they would like to encrypt or decrypt, then our implementation will add some variation to the basic algorithm to make it a little harder to decode. When encrypting:

(1)You'll replace each "e" character in the original message with the letters "zw". If the user enters "Hello World!", the resulting message will be "Hzwllo World!". (2) After replacing the "e" characters, you'll add the word "hokie" to the beginning, middle (the middle is length//2), and the end of the message. Continuing with example above the message that's actually encrypted using the Caesar Cipher is "hokieHzwllohokie World!hokie".

Once you have the altered message then perform the Caesar Cipher encoding as described above, printing the result.

When decrypting, perform each step in reverse order. Decrypt the message using the Caesar Cipher, then remove the "hokie" strings, then change any occurrence of "zw" to "e". You may assume the all occurrences of "zw" in the result are really "e" characters, and further you may assume the "hokie" strings are in the encrypted message at the appropriate place, however there may be other occurrences of "hokie" in the original message.

Finally, your program must be able to encrypt/decrypt an arbitrary number of messages. When youve completed the encrypting/decrypting a message the user should be asked if they want to enter another message. Your program should continue until the user types N.

Sample Execution

Enter Message: Hello World! Enter shift amount: 3 Encode (E) or Decode (D)? E Result: krnlhKczoorkrnlh Zruog!krnlh Go again? (Y/N): Y 
Enter Message: krnlhKczoorkrnlh Zruog!krnlh Enter shift amount: 3 Encode (E) or Decode (D)? D Result: Hello World! 
Go again? (Y/N): N 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional SQL Server 2012 Internals And Troubleshooting

Authors: Christian Bolton, Justin Langford

1st Edition

1118177657, 9781118177655

More Books

Students also viewed these Databases questions