Answered step by step
Verified Expert Solution
Question
1 Approved Answer
How to fix this python code to do this: 1 . Your program should be able to replace part of one message with part of
How to fix this python code to do this:
Your program should be able to replace part of one message with part of the other message. The exercise suggests to begin by replacing the last block of the new message with the last block of the older message. This probably does not decrypt exactly like you thought.
Consider making changes to the message format to make it easier to switch one block for another. For example, you may want to change the format of the date so that it uses the same number of digits for all dates.
Modify your program so that it asks the user which part of the message to change From Who, To Whom, Date, or Meeting place
Your program should take the user input and swap out the respective blocks of one message with the respective blocks from another message. The newly created ciphertext should decrypt to something that looks like a real message.
This is the prompt: "Take two different ciphertexts from Alice to Bob with different meeting instructions on different dates. Splice the ciphertext from the body of the first message into the body of the second message. that is start by replacing the last block of the newer message with the last block or blocks if it was longer of the previous message. Does the message decrypt? did you change where Bob goes to meet Alice?"
The code:
from Crypto.Cipher import AES
from Crypto.Random import getrandombytes
from Crypto.Util.Padding import pad, unpad
# Key for encryption
key getrandombytes
# Encrypt function using AES
def encryptmessagemessage:
cipher AES.newkey AES.MODECBC
ciphertext cipher.encryptpadmessageencode AES.blocksize
return ciphertext, cipher.iv
# Decrypt function using AES
def decryptmessageciphertext iv:
cipher AES.newkey AES.MODECBC iv
decryptedmessage unpadcipherdecryptciphertext AES.blocksizedecode
return decryptedmessage
# Function to switch message parts
def switchmessagepartsmessage message parttochange:
# Split messages into parts
parts messagesplit
parts messagesplit
# Switch the respective values
if parttochange "From Who":
parts parts
parts parts # Swap the 'From Who' parts
elif parttochange To Whom":
parts parts
parts parts # Swap the To Whom' parts
elif parttochange "Date":
parts parts
parts parts # Swap the 'Date' parts
elif parttochange "Meeting place":
parts parts
parts parts # Swap the 'Meeting place' parts
# Return the updated parts
newmessage
joinparts
newmessage
joinparts
return newmessage newmessage
# Example messages
message "Alice
Bob
Park"
message "Alice
Bob
Restaurant"
# Encrypt messages
ciphertext iv encryptmessagemessage
ciphertext iv encryptmessagemessage
# Switch values
parttochange inputWhich part of the message to switch? From Who, To Whom, Date, or Meeting place:
# Decrypt messages before switching
decryptedmessage decryptmessageciphertext iv
decryptedmessage decryptmessageciphertext iv
# Switch parts and reconstruct messages
newmessage newmessage switchmessagepartsdecryptedmessage decryptedmessage parttochange
# Encrypt the NEW messages
ciphertextnew ivnew encryptmessagenewmessage
ciphertextnew ivnew encryptmessagenewmessage
# Print new messages
printNew Message :
printnewmessage
printNew Message :
printnewmessage
decryptedmessagenew decryptmessageciphertextnew ivnew
decryptedmessagenew decryptmessageciphertextnew ivnew
print
Decrypted New Message :
printdecryptedmessagenew
printDecrypted New Message :
printdecryptedmessagenew
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