From 5d826f409b1bdda606d90d4a91fcdde611f16d5e Mon Sep 17 00:00:00 2001 From: RubenCGomes Date: Tue, 12 Nov 2024 10:41:18 +0000 Subject: [PATCH] encryption functions added (may require adaptation) --- delivery1/cryptLib/decryptionFuncts.py | 26 ++++++++++++++++++ delivery1/cryptLib/encryptionFuncts.py | 23 ++++++++++++++++ delivery1/cryptLib/keyPair.py | 38 ++++++++++++++++++++++++++ 3 files changed, 87 insertions(+) create mode 100644 delivery1/cryptLib/decryptionFuncts.py create mode 100644 delivery1/cryptLib/encryptionFuncts.py create mode 100644 delivery1/cryptLib/keyPair.py diff --git a/delivery1/cryptLib/decryptionFuncts.py b/delivery1/cryptLib/decryptionFuncts.py new file mode 100644 index 0000000..5d7379a --- /dev/null +++ b/delivery1/cryptLib/decryptionFuncts.py @@ -0,0 +1,26 @@ +import sys + +from cryptography.hazmat.primitives import serialization, hashes +from cryptography.hazmat.primitives.asymmetric import rsa, padding + + +def decryptFile(private_key, cipher_text): + plain_text = private_key.decrypt( + cipher_text, + padding.OAEP( + mgf=padding.MGF1(algorithm=hashes.SHA256()), + algorithm=hashes.SHA256(), + label=None + ) + ) + return plain_text + + +def load_private_key(file, paswd=None): + with open(file, 'rb') as key_file: + private_key = serialization.load_pem_private_key( + key_file.read(), + password=paswd, + ) + + return private_key diff --git a/delivery1/cryptLib/encryptionFuncts.py b/delivery1/cryptLib/encryptionFuncts.py new file mode 100644 index 0000000..4531dcc --- /dev/null +++ b/delivery1/cryptLib/encryptionFuncts.py @@ -0,0 +1,23 @@ +import sys + +from cryptography.hazmat.primitives import serialization, hashes +from cryptography.hazmat.primitives.asymmetric import rsa, padding + +def encryptFile(public_key, text): + ciphertext = public_key.encrypt( + text, + padding.OAEP( + mgf=padding.MGF1(algorithm=hashes.SHA256()), + algorithm=hashes.SHA256(), + label=None + ) + ) + return ciphertext + +def load_public_key(file): + with open(file, 'rb') as key_file: + public_key = serialization.load_pem_public_key( + key_file.read(), + ) + + return public_key diff --git a/delivery1/cryptLib/keyPair.py b/delivery1/cryptLib/keyPair.py new file mode 100644 index 0000000..fe7c7e4 --- /dev/null +++ b/delivery1/cryptLib/keyPair.py @@ -0,0 +1,38 @@ +import sys + +from cryptography.hazmat.primitives import serialization, hashes +from cryptography.hazmat.primitives.asymmetric import rsa, padding + + +def generate_key_pair(passwd=None): + pub_name, priv_name, key_size = sys.argv[1:] + + private_key = rsa.generate_private_key( + public_exponent=65537, + key_size=int(key_size) + ) + public_key = private_key.public_key() + + with open(pub_name, 'wb') as f: + f.write(public_key.public_bytes( + encoding=serialization.Encoding.PEM, + format=serialization.PublicFormat.SubjectPublicKeyInfo + )) + + if not passwd: + with open(priv_name, 'wb') as f: + f.write(private_key.private_bytes( + encoding=serialization.Encoding.PEM, + format=serialization.PrivateFormat.TraditionalOpenSSL, + encryption_algorithm=serialization.NoEncryption() + )) + else: + with open(priv_name, 'wb') as f: + f.write(private_key.private_bytes( + encoding=serialization.Encoding.PEM, + format=serialization.PrivateFormat.TraditionalOpenSSL, + encryption_algorithm=serialization.BestAvailableEncryption(passwd.encode()) + )) + + return pub_name, priv_name +