Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have to REUSE the same key & IV if I want to keep the files encrypted and decrypt them when needed. Scenario 1 :

I have to REUSE the same key & IV if I want to keep the files encrypted and decrypt them when needed.
Scenario1: Saving the key & IV
*Save the key and IV in two files e.g. "AES.key" & "AES.IV"
*Load the key & IV to do the decryption when needed.
Scenario 2: generate the key & IV from a hash of a password
*Input a password and use the SHA256 code given in the lecture notes
*Discuss ways to use that in practice.
Discuss the Pros & Cons of saving the key & IV vs using it generated from a password. Can you think of other solutions?
This is the code I have:
import os
from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes
import hashlib
def generate_key_iv():
key = get_random_bytes(16) # 128-bit key
iv = get_random_bytes(16) # 128-bit IV
return key, iv
def pad_message(message):
block_size = AES.block_size
padding = block_size - len(message)% block_size
return message + padding * chr(padding)
def unpad_message(padded_message):
padding = ord(padded_message[-1])
return padded_message[:-padding]
def encrypt_message(message, key, iv):
cipher = AES.new(key, AES.MODE_CBC, iv)
padded_message = pad_message(message)
encrypted_message = cipher.encrypt(padded_message)
return encrypted_message
def decrypt_message(encrypted_message, key, iv):
cipher = AES.new(key, AES.MODE_CBC, iv)
decrypted_message = cipher.decrypt(encrypted_message)
return unpad_message(decrypted_message)
def encrypt_file(file_path, key, iv):
with open(file_path, 'rb') as f:
plaintext = f.read()
encrypted_data = encrypt_message(plaintext, key, iv)
encrypted_file_path = file_path + 'enc'
with open(encrypted_file_path, 'wb') as f:
f.write(encrypted_data)
return encrypted_file_path
def decrypt_file(encrypted_file_path, key, iv):
with open(encrypted_file_path, 'rb') as f:
encrypted_data = f.read()
decrypted_data = decrypt_message(encrypted_data, key, iv)
decrypted_file_path = encrypted_file_path.replace('enc', 'dec.txt')
with open(decrypted_file_path, 'wb') as f:
f.write(decrypted_data)
return decrypted_file_path
# Generate key and IV
key, iv = generate_key_iv()
# Encrypt and decrypt message
message = "Cyber Security is fun"
encrypted_message = encrypt_message(message.encode(), key, iv)
decrypted_message = decrypt_message(encrypted_message, key, iv)
print("Decrypted Message:", decrypted_message.decode())
# Encrypt and decrypt a file
file_path = "test.txt"
encrypted_file_path = encrypt_file(file_path, key, iv)
decrypted_file_path = decrypt_file(encrypted_file_path, key, iv)
# Check if decrypted file is identical to original file
with open(file_path, 'rb') as f1, open(decrypted_file_path, 'rb') as f2:
original_content = f1.read()
decrypted_content = f2.read()
if original_content == decrypted_content:
print("Decrypted file is identical to original file")
else:
print("Decrypted file is not identical to original file")

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

Transactions On Large Scale Data And Knowledge Centered Systems Iv Special Issue On Database Systems For Biomedical Applications Lncs 6990

Authors: Abdelkader Hameurlain ,Josef Kung ,Roland Wagner ,Christian Bohm ,Johann Eder ,Claudia Plant

2011th Edition

3642237398, 978-3642237393

More Books

Students also viewed these Databases questions

Question

Have I incorporated my research into my outline effectively?

Answered: 1 week ago