Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

python Write a program that repeatedly encrypts plain text messages or decrypts cipher text messages as indicated Develop your program using this template provided in

python

Write a program that repeatedly encrypts plain text messages or decrypts cipher text messages as indicated

Develop your program using this template provided in the attached file: Assignment8Student.py

Your program will prompt the user for a message and, as long as one is given, ask the user if the message should be encrypted or decrypted ('E' or 'e' for encrypt, 'D' or 'd' for decrypt)

Your program will then ask the user for the rotation key to use

If this is for encryption, it will be the distance to rotate the characters

If this is for decryption, it will be the distance used previously to encrypt the file

In either case, the rotation key may be negative or positive, but must be an integer

Then the message should be processed and the result printed out

Instead of using just using alphabet characters as with the examples above, your messages can include any of the printable ASCII characters except "Delete" (i.e., ASCII 32, up to but not including ASCII 127)

Note that unlike the examples above, the first character has ordinal 32

Rotating the character with ordinal 126 by rotation key 1 will yield the character with ordinal 32

Rotating the character with ordinal 32 by rotation key -1 will yield the character with ordinal 126

Basic flow of the program

You will prompt the user for a message

As long as there is a message you will

Get the operation to be performed and validate it

Get the rotation key and validate it

Convert the key (i.e., make it numeric) and flip the sign if necessary (i.e., if operation is 'd' or 'D')

For each character in the message you will

Convert the character to its ASCII code (using ord())

Add the positive or negative rotation key to the character code

If the numeric value of the character is out of bound, rotate it back until it is within bounds

Concatenate the result with a string accumulator that was previously initialized

Print out the result

Ask the user for another message

E.g.: Given the character 'z', operation 'e', and rotation key 25:

ASCII code for 'z' is 122 (code for highest printable ASCII character is 126)

122 + 25 = 147 (This value is 21 characters past the "edge" of 126, so we will have to

wrap around to the other side and go in 21 characters)

Since there are 95 printable characters, that should give us 147 - 95 = 52 to rotate us back within bounds, andchr(52) is '4'

Decryption would work the same way, but in the opposite direction

Given the character '4', operation 'd', and original rotation key 25:

Multiply rotation key by -1 since operation is decrypt: now distance is -25

ord('4') is 52, 52 - 25 = 27, which is 5 characters before the "edge" of 32, so we will have to wrap around to the other side and go in 5 characters

Since there are 95 printable characters, that should give us 27 + 95 = 122 to rotate back within bounds, and chr(122) is 'z', bringing us back to our plain text character

You should use the following str operators in this assignment:

[], [:], ==, >=, <, +, %, and in

You should use the following str methods in this assignment:

isdigit(), lower(), chr(), and ord()

Develop and test your program incrementally, comment out (do not remove) the debug code

Make sure that your test input includes invalid data, and that your program handles it as discussed in class

For your test runs, use the following message and perform both encryption and decryption on it using distance values of 1, -1, 10, -10, 95, -95, 127, -127, 570 and -570:

The quick brown fox jumps over the lazy dog

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

More Books

Students also viewed these Databases questions

Question

=+ For what reasons can and do unions go on strike?

Answered: 1 week ago