Question: Write a Linux Python utility aes.py for encrypting and decrypting a single text file. The text file is encrypted on the command line by using

Write a Linux Python utility aes.py for encrypting and decrypting a single text file. The text file is encrypted on the command line by using the AES standard encryption method.
$python aes.py e filename.txt filename.txt.aes 123(for encrypting filename.txt with output file named filename.txt.aes)
$python aes.py d filename.txt.aes filename.txt.back AES_key (for dencrypting filename.txt.aes with output file named filename.txt.back)
Note: Do not wite the program from scratch, just complete the code for the function decrypt of the program template below:
import sys
from cryptography.fernet import Fernet
def encrypt(infile, outfile):
key = Fernet.generate_key()
print('AES Key: ', key)
fernet = Fernet(key)
for line in infile:
name = line.strip()
encName = fernet.encrypt(name.encode())
outfile.write(encName.decode()+"
")
with open("AES_Key_"+sys.argv[2],'w') as aes_key: aes_key.write(key.decode())
infile.close()
outfile.close()
def decrypt(infile, outfile, key):
# TODO for homework 3.1
infile.close()
outfile.close()
# Call the main function.
if __name__=='__main__':
if len(sys.argv)!=5:
print("Usage: python aes.py mode infile.txt outfile.txt key")
exit()
operation = sys.argv[1]
infile = open(sys.argv[2],'r')
outfile = open(sys.argv[3],'w')
key = sys.argv[4]
if operation =='e':
# encrypts
encrypt(infile,outfile)
elif operation =='d':
# decrypt
decrypt(infile,outfile,key)
else:
# error
print("Invalid operation.")

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!