Write a python program to achieve Advanced Encryption Standard 128 bit key length encoding and decoding of text.
Program should be algorithm based rather than using any library function. Write both ENCODING and DECODING.
It should have the ability to encode even a FILE.
Input text
“To be, or not to be, that is the question:
Whether 'tis nobler in the mind to suffer
The slings and arrows of outrageous fortune,
Or to take arms against a sea of troubles
And by opposing end them. To die—to sleep,
Expected Encoded:
O426pZT/JplIF8kyaiz3BDnCS5gfsMqAQS3P4ddM6ymaAlpSJBd8BNkZMJwux18ScbGM/HQn8RGNb/5hS4x81tMZ4QGerqPt3eI+qx/JJGSXM234gWesQsdJp66AoNLUCxDqXSU2IgR58rQ65kavEv/WknnN9pFir9D8pMqMO+y5xlfckTKZzBtWfJfkmz2HN2aS5UJVfzHFoXH8nVHL9tj+qX7+hAS5bi7u+PFBWQk26O8gy5eYPYQ+HZqo6TUmc3Jrf6CqNsx+ljfg/JIAjs17058hM8wl19gdRSW9/CNkYM/00aTK9vMpjC2BSraUMDyIezefqv5JYTHpFpEa+k1peN0FxrJCvLtiMmYxlM0sjNrACU1mZu+
from cryptography.fernet import Fernet
K = Fernet.generate_key()
with open('secret.key', 'wb') as NewKey:
NewKey.write(K)
file = open('inputFile.txt')
info = file.read()
info = info.encode()
x = Fernet(K)
text = x.encrypt(info)
print('\nEncrypted message is: \n')
print(text)
with open('secret.key', 'rb') as symmetric_Key:
K = symmetric_Key.read()
x = Fernet(key)
text = f.decrypt(text)
decrypted = cleartext.decode()
print("\nDecrypted message:\n")
print(decrypted)
Comments
Leave a comment