Skip to content

Instantly share code, notes, and snippets.

@febimudiyanto
Created February 13, 2022 16:07
Show Gist options
  • Save febimudiyanto/fb00a34415b73e74cd088dfcaed6e340 to your computer and use it in GitHub Desktop.
Save febimudiyanto/fb00a34415b73e74cd088dfcaed6e340 to your computer and use it in GitHub Desktop.
def decrypt(dataFile, privateKeyFile):
'''
use EAX mode to allow detection of unauthorized modifications
'''
# read private key from file
with open(privateKeyFile, 'rb') as f:
privateKey = f.read()
# create private key object
key = RSA.import_key(privateKey)
# read data from file
with open(dataFile, 'rb') as f:
# read the session key
encryptedSessionKey, nonce, tag, ciphertext = [ f.read(x) for x in (key.size_in_bytes(), 16, 16, -1) ]
# decrypt the session key
cipher = PKCS1_OAEP.new(key)
sessionKey = cipher.decrypt(encryptedSessionKey)
# decrypt the data with the session key
cipher = AES.new(sessionKey, AES.MODE_EAX, nonce)
data = cipher.decrypt_and_verify(ciphertext, tag)
# save the decrypted data to file
[ fileName, fileExtension ] = dataFile.split('.')
decryptedFile = fileName + '_decrypted.' + fileExtension
with open(decryptedFile, 'wb') as f:
f.write(data)
print('Decrypted file saved to ' + decryptedFile)
@Mahmoud-Ibrahem1
Copy link

don't work what to do please?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment