Answered step by step
Verified Expert Solution
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.
Scenario: Saving the key & IV
Save the key and IV in two files eg "AES.key" & "AES.IV
Load the key & IV to do the decryption when needed.
Scenario : generate the key & IV from a hash of a password
Input a password and use the SHA 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 getrandombytes
import hashlib
def generatekeyiv:
key getrandombytes # bit key
iv getrandombytes # bit IV
return key, iv
def padmessagemessage:
blocksize AES.blocksize
padding blocksize lenmessage blocksize
return message padding chrpadding
def unpadmessagepaddedmessage:
padding ordpaddedmessage
return paddedmessage:padding
def encryptmessagemessage key, iv:
cipher AES.newkey AES.MODECBC iv
paddedmessage padmessagemessage
encryptedmessage cipher.encryptpaddedmessage
return encryptedmessage
def decryptmessageencryptedmessage, key, iv:
cipher AES.newkey AES.MODECBC iv
decryptedmessage cipher.decryptencryptedmessage
return unpadmessagedecryptedmessage
def encryptfilefilepath, key, iv:
with openfilepath, rb as f:
plaintext fread
encrypteddata encryptmessageplaintext key, iv
encryptedfilepath filepath 'enc'
with openencryptedfilepath, wb as f:
fwriteencrypteddata
return encryptedfilepath
def decryptfileencryptedfilepath, key, iv:
with openencryptedfilepath, rb as f:
encrypteddata fread
decrypteddata decryptmessageencrypteddata, key, iv
decryptedfilepath encryptedfilepath.replaceenc 'dec.txt
with opendecryptedfilepath, wb as f:
fwritedecrypteddata
return decryptedfilepath
# Generate key and IV
key, iv generatekeyiv
# Encrypt and decrypt message
message "Cyber Security is fun"
encryptedmessage encryptmessagemessageencode key, iv
decryptedmessage decryptmessageencryptedmessage, key, iv
printDecrypted Message:", decryptedmessage.decode
# Encrypt and decrypt a file
filepath "test.txt
encryptedfilepath encryptfilefilepath, key, iv
decryptedfilepath decryptfileencryptedfilepath, key, iv
# Check if decrypted file is identical to original file
with openfilepath, rb as f opendecryptedfilepath, rb as f:
originalcontent fread
decryptedcontent fread
if originalcontent decryptedcontent:
printDecrypted file is identical to original file"
else:
printDecrypted file is not identical to original file"
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