Answered step by step
Verified Expert Solution
Link Copied!

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:
1. 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.
2. 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.
3.Modify your program so that it asks the user which part of the message to change (From Who, To Whom, Date, or Meeting place).
4. Your program should take the user input and swap out the respective block(s) of one message with the respective block(s) 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 get_random_bytes
from Crypto.Util.Padding import pad, unpad
# Key for encryption
key = get_random_bytes(16)
# Encrypt function using AES
def encrypt_message(message):
cipher = AES.new(key, AES.MODE_CBC)
ciphertext = cipher.encrypt(pad(message.encode(), AES.block_size))
return ciphertext, cipher.iv
# Decrypt function using AES
def decrypt_message(ciphertext, iv):
cipher = AES.new(key, AES.MODE_CBC, iv)
decrypted_message = unpad(cipher.decrypt(ciphertext), AES.block_size).decode()
return decrypted_message
# Function to switch message parts
def switch_message_parts(message1, message2, part_to_change):
# Split messages into parts
parts1= message1.split('
')
parts2= message2.split('
')
# Switch the respective values
if part_to_change == "From Who":
parts1[0]= parts2[0]
parts2[0]= parts1[0] # Swap the 'From Who' parts
elif part_to_change =="To Whom":
parts1[1]= parts2[1]
parts2[1]= parts1[1] # Swap the 'To Whom' parts
elif part_to_change == "Date":
parts1[2]= parts2[2]
parts2[2]= parts1[2] # Swap the 'Date' parts
elif part_to_change == "Meeting place":
parts1[3]= parts2[3]
parts2[3]= parts1[3] # Swap the 'Meeting place' parts
# Return the updated parts
new_message1='
'.join(parts1)
new_message2='
'.join(parts2)
return new_message1, new_message2
# Example messages
message1= "Alice
Bob
2024-06-10
Park"
message2= "Alice
Bob
2024-06-15
Restaurant"
# Encrypt messages
ciphertext1, iv1= encrypt_message(message1)
ciphertext2, iv2= encrypt_message(message2)
# Switch values
part_to_change = input("Which part of the message to switch? (From Who, To Whom, Date, or Meeting place): ")
# Decrypt messages before switching
decrypted_message1= decrypt_message(ciphertext1, iv1)
decrypted_message2= decrypt_message(ciphertext2, iv2)
# Switch parts and reconstruct messages
new_message1, new_message2= switch_message_parts(decrypted_message1, decrypted_message2, part_to_change)
# Encrypt the *NEW* messages
ciphertext_new1, iv_new1= encrypt_message(new_message1)
ciphertext_new2, iv_new2= encrypt_message(new_message2)
# Print new messages
print("New Message 1:")
print(new_message1)
print("New Message 2:")
print(new_message2)
decrypted_message_new1= decrypt_message(ciphertext_new1, iv_new1)
decrypted_message_new2= decrypt_message(ciphertext_new2, iv_new2)
print("
Decrypted New Message 1:")
print(decrypted_message_new1)
print("Decrypted New Message 2:")
print(decrypted_message_new2)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Concepts of Database Management

Authors: Philip J. Pratt, Mary Z. Last

8th edition

1285427106, 978-1285427102

More Books

Students also viewed these Databases questions

Question

understand the application of management and leadership theories

Answered: 1 week ago

Question

=+analysis, and social media communication audit

Answered: 1 week ago