File Encryption and Decryption Using Python 3.x Write a program that uses the di
ID: 3678580 • Letter: F
Question
File Encryption and Decryption
Using Python 3.x
Write a program that uses the dictionary to assign "codes" to each letter of the alphabet. For example: codes = { 'A' : '~', 'a' : '`', 'B' :'!', 'b' : '1', 'C' : '@', 'c' : '2', 'D' : '#', 'd' : '3', 'E' : '$', 'e' : '4',
'F' : '%', 'f' : '5', 'G' : '^', 'g' : '6', 'H' :, etc...}
Using this example, the letter A would be assigned the symbol `, the letter a would be assigned the symbol `, the letter B would be assigned the symbol !, and so forth.
This program should open a specified text file, read its contents, and then use the dictionary to write an encrypted version of the file's contents to a second file. Each character in the second file should contain the code of the corresponding character in the first file.
Write a second program that opens an encrypted file and displays its decrypted contents on the screen.
Explanation / Answer
def main():
choice = 0
while choice != QUIT_CHOICE:
display_menu()
choice = int(input(Please enter your choice: '))
if choice == ENCRYPT:
encrypt()
elif choice == DECRYPT:
decrypt()
elif choice == QUIT_CHOICE:
print('Exiting the program...')
else:
print('Error: invalid selection.')
def encrypt():
result = convert()
output_name = input("Enter the name of the output file: ")
output_file = open(output_name, 'w')
output_file.write(result)
output_file.close()
def decrypt():
result = convert()
print(result)
def convert():
input_name = input("Enter the name of the input file: ")
input_file = open(input_name, 'r')
result = ''
text = input_file.read()
for ch in text:
if ch.isspace():
result = result + ch
else:
result = result + CODE[ch]
return result
def display_menu():
print(' ')
print(' Encryption/Decryption ')
print(' ')
print(' Encrypt a file')
print(' Decrypt a file')
print(' Exit')
main()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.