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

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 Databases Questions!