Question
I need to modify the python code bellow such that it can take a given text (plaintext), encrypt it with a given key, save the
I need to modify the python code bellow such that it can take a given text (plaintext), encrypt it with a given key, save the encrypted message in a new file(cyphertext), save the key in a new file, use the key to decrypt the message, and provide the decrypted message (back to the plaintext).
from __future__ import print_function, unicode_literals
from os import urandom
def genkey(length): """Generate a Key""" return urandom(length)
def xor_strings(s, t): """xor two strings together""" if isinstance(s, str): #text strings contain single characters return "".join(chr(ord(a) ^ ord(b))for a, b in zip(s,t))
else: #Python 3 bytes objects contain integer values in the range 0-255 return bytes([a ^ b for a, b in zip (s,t)])
message='This is a secret'
print ('message:', message)
key = genkey(len(message)) print ('key:', key)
cipherText = xor_strings(message.encode('utf8'),key)
print ('Cipher Text:', cipherText)
print('Decrypted message:', xor_strings(cipherText,key).decode('utf8'))
#verify
if xor_strings(cipherText,key).decode('utf8') == message: print ('Encryption and Decryption correct') else: print('Encryption and Decryption failed')
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