Use the crytographic objects (AESCrypto and RSACrypto)
- Install necessary cryptographic module(s).
- Verify the hash of the supplied part 1 files (publickey.pem, part1.txt.enc, part1.txt.sig) with their hashes (part1.sha256).
- Decrypt the encrypted text file (part1.txt.enc) using AES-128. The key is sfhaCS2023
- Verify the plaintext using the provided signature and public key (part1.txt.sig, publickey.pem)
import random
import base64
import hashlib
from Crypto.Cipher import AES
from Crypto.Hash import SHA256
from Crypto.PublicKey import RSA
from Crypto.Signature import pkcs1_15
# Define AES class
class AESCrypto:
def __init__(self, key):
self.key = key.encode('utf-8')
def encrypt(self, cleartext):
Block_Size = AES.block_size
pad = lambda s: s + (Block_Size - len(s) % Block_Size) * chr(Block_Size - len(s) % Block_Size)
cleartext_blocks = pad(cleartext)
iv = random.new().read(Block_Size)
crypto = AES.new(self.key, AES.MODE_CBC, iv)
return base64.b64encode(iv + crypto.encrypt(cleartext_blocks.encode()))
def decrypt(self, enctext):
enctext = base64.b64decode(enctext)
iv = enctext[:16]
crypto = AES.new(self.key, AES.MODE_CBC, iv)
unpad = lambda s: s[:-ord(s[-1])]
return unpad(crypto.decrypt(enctext[16:]))
# Verify the hash of files
def verify_file_hash(filename, expected_hash):
try:
with open(filename, 'rb') as file:
file_hash = hashlib.sha256(file.read()).hexdigest()
return file_hash == expected_hash
except FileNotFoundError:
return False
# Decrypt using AES-128
def decrypt_aes(file_path, key):
try:
with open(file_path, 'rb') as file:
encrypted_data = base64.b64decode(file.read())
aes = AESCrypto(key)
decrypted_data = aes.decrypt(encrypted_data)
return decrypted_data.decode('utf-8')
except FileNotFoundError:
return None
# Verify plaintext using RSA signature
def verify_rsa_signature(plaintext, signature_path, public_key_path):
try:
with open(public_key_path, 'rb') as public_key_file:
public_key = RSA.import_key(public_key_file.read())
with open(signature_path, 'rb') as signature_file:
signature = signature_file.read()
h = SHA256.new(plaintext.encode('utf-8'))
pkcs1_15.new(public_key).verify(h, signature)
return True
except (ValueError, TypeError, FileNotFoundError):
return False
# Verify hash
part1_hash = None
try:
with open("part1.sha256", 'r') as hash_file:
part1_hash = hash_file.read().strip()
except FileNotFoundError:
pass
if part1_hash and \
verify_file_hash("publickey.pem", part1_hash) and \
verify_file_hash("part1.txt.enc", part1_hash) and \
verify_file_hash("part1.txt.sig", part1_hash):
print("File hashes verified successfully.")
else:
print("File hashes do not match or some files are missing.")
# Decrypt the encrypted text file
decrypted_text = decrypt_aes("part1.txt.enc", "sfhaCS2023")
if decrypted_text is not None:
print("Decrypted text:", decrypted_text)
# Verify the plaintext using RSA signature
if verify_rsa_signature(decrypted_text, "part1.txt.sig", "publickey.pem"):
print("RSA verified successfully.")
else:
print("RSA verification failed.")
else:
print("Failed to decrypt the file.")