Question
Python (Writing a function) Reuse the functions we have defined during the lecture within a program that presents the following list of actions as a
Python (Writing a function)
Reuse the functions we have defined during the lecture within a program that presents the following list of actions as a menu for a user:
1. enter a key;
2. encrypt a text input by the user;
3. decrypt a text input by the user;
4. read a file containing plaintext and write the encrypted text to another file;
5. read a file containing ciphertext and write the decrypted text to another file;
6. exit the program.
The key has to be provided by the user, even if your menu does not include action 1 (in this case actions 25 must incorporate action 1). The program will perform the action chosen by the user and, unless the user chooses to exit the program, will present again the list of action.
P.S. Code from the lecture:
def char_encrypt(c,shift): return chr(ord('A') + (ord(c.upper()) - ord('A') + shift) % 26)
def char_decrypt(c,shift): return chr(ord('a') + (ord(c.lower()) - ord('a') - shift) % 26)
def encrypt(plaintext, key): ciphertext = '' k_index = 0 for c in plaintext: if c.isalpha(): ciphertext = ciphertext + char_encrypt(c,key[k_index]) k_index = (k_index + 1) % len(key) else: ciphertext = ciphertext + c return ciphertext
def decrypt(ciphertext, key): plaintext = '' k_index = 0 for c in ciphertext: if c.isalpha(): plaintext = plaintext + char_decrypt(c,key[k_index]) k_index = (k_index + 1) % len(key) else: plaintext = plaintext + c return plaintext
plain_msg = 'My name is Adam Johnson' print(plain_msg) key_1 = [ 2, 4, 1, 6, 5 ] cipher_msg = encrypt(plain_msg,key_1) print(cipher_msg) decrypted_msg = decrypt(cipher_msg,key_1) print(decrypted_msg)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started