1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
def decode(filename):
private_key = RSA.import_key(open(os.path.dirname(__file__)+"/privatekey/private.pem").read())
if filename.endswith(".bin"):
try:
with open(filename, 'rb') as file_in:
enc_session_key, nonce, tag, ciphertext = \
[ file_in.read(x) for x in (private_key.size_in_bytes(), 16, 16, -1) ]
# Decrypt the session key with the private RSA key
cipher_rsa = PKCS1_OAEP.new(private_key)
session_key = cipher_rsa.decrypt(enc_session_key)
# Decrypt the data with the AES session key
cipher_aes = AES.new(session_key, AES.MODE_EAX, nonce)
data = cipher_aes.decrypt_and_verify(ciphertext, tag)
file_in.close()
except:
return "can't decode", False
return data, True
else:
return "not .bin file ",False |
Partager