29 lines
907 B
Python
29 lines
907 B
Python
import base64
|
|
from cryptography.hazmat.backends import default_backend
|
|
from cryptography.hazmat.primitives import serialization, hashes
|
|
from cryptography.hazmat.primitives.asymmetric import padding
|
|
|
|
def load_keys_private(private_key_file):
|
|
with open(private_key_file, 'rb') as f:
|
|
private_key_data = f.read()
|
|
|
|
private_key = serialization.load_pem_private_key(
|
|
private_key_data,
|
|
password=None,
|
|
backend=default_backend()
|
|
)
|
|
return private_key
|
|
|
|
def decrypt_message(encrypted_message, private_key_file):
|
|
private_key = load_keys_private(private_key_file)
|
|
encrypted_bytes = base64.b64decode(encrypted_message)
|
|
decrypted = private_key.decrypt(
|
|
encrypted_bytes,
|
|
padding.OAEP(
|
|
mgf=padding.MGF1(algorithm=hashes.SHA256()),
|
|
algorithm=hashes.SHA256(),
|
|
label=None
|
|
)
|
|
)
|
|
return decrypted.decode()
|