Created
February 13, 2022 16:07
-
-
Save febimudiyanto/fb00a34415b73e74cd088dfcaed6e340 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
don't work what to do please?