diff --git a/delivery1/lib/decryption_functs.py b/delivery1/lib/asymmetric_functs.py similarity index 52% rename from delivery1/lib/decryption_functs.py rename to delivery1/lib/asymmetric_functs.py index 31799b6..638a533 100644 --- a/delivery1/lib/decryption_functs.py +++ b/delivery1/lib/asymmetric_functs.py @@ -1,11 +1,77 @@ -import sys +import sys, os from cryptography.hazmat.primitives import serialization, hashes -from cryptography.hazmat.primitives.asymmetric import padding +from cryptography.hazmat.primitives.asymmetric import rsa, padding +from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes from cryptography.hazmat.backends import default_backend +# ----------------- +# encrypt functions +# ----------------- +# function to generate a 256-bit symmetric key +def generate_symmetric_key(): + return os.urandom(32) + + +# function to encrypt data using a symmetric key +def encrypt_symmetric(key, plain_text): + # generate a random IV + iv = os.urandom(16) + + # cipher the data using AES in CFB mode + cipher = Cipher(algorithms.AES(key), modes.CFB(iv), backend=default_backend()) + encryptor = cipher.encryptor() + ciphertext = encryptor.update(plain_text) + encryptor.finalize() + + return iv + ciphertext + + +# function that calls and combines the symmetric and asymmetric encryption +def encrypt_hybrid(public_key, plaintext): + # generate a random symmetric key + symmetric_key = generate_symmetric_key() + + encrypted_data = encrypt_symmetric(symmetric_key, plaintext) + + # encrypt the symmetric key with the public key + encrypted_symmetric_key = public_key.encrypt( + symmetric_key, + padding.OAEP( + mgf=padding.MGF1(algorithm=hashes.SHA256()), + algorithm=hashes.SHA256(), + label=None + ) + ) + + # combine the symmetric key and the encrypted data + return encrypted_symmetric_key + encrypted_data + + +# main function to encrypt the file +def encrypt_file(public_key, original_file, encrypted_file): + with open(original_file, 'rb') as f: + plaintext = f.read() + + encrypted_content = encrypt_hybrid(public_key, plaintext) + + with open(encrypted_file, 'wb') as f: + f.write(encrypted_content) + + +# function to load a public key from a file +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 + +# ----------------- +# decrypt functions +# ----------------- # function to decrypt data using a symmetric key def decrypt_symmetric(key, ciphertext): diff --git a/delivery1/lib/encryption_functs.py b/delivery1/lib/encryption_functs.py deleted file mode 100644 index c788a97..0000000 --- a/delivery1/lib/encryption_functs.py +++ /dev/null @@ -1,67 +0,0 @@ -import sys, os - -from cryptography.hazmat.primitives import serialization, hashes -from cryptography.hazmat.primitives.asymmetric import rsa, padding -from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC -from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes -from cryptography.hazmat.backends import default_backend - - -# function to generate a 256-bit symmetric key -def generate_symmetric_key(): - return os.urandom(32) - - -# function to encrypt data using a symmetric key -def encrypt_symmetric(key, plain_text): - # generate a random IV - iv = os.urandom(16) - - # cipher the data using AES in CFB mode - cipher = Cipher(algorithms.AES(key), modes.CFB(iv), backend=default_backend()) - encryptor = cipher.encryptor() - ciphertext = encryptor.update(plain_text) + encryptor.finalize() - - return iv + ciphertext - - -# function that calls and combines the symmetric and asymmetric encryption -def encrypt_hybrid(public_key, plaintext): - # generate a random symmetric key - symmetric_key = generate_symmetric_key() - - encrypted_data = encrypt_symmetric(symmetric_key, plaintext) - - # encrypt the symmetric key with the public key - encrypted_symmetric_key = public_key.encrypt( - symmetric_key, - padding.OAEP( - mgf=padding.MGF1(algorithm=hashes.SHA256()), - algorithm=hashes.SHA256(), - label=None - ) - ) - - # combine the symmetric key and the encrypted data - return encrypted_symmetric_key + encrypted_data - - -# main function to encrypt the file -def encrypt_file(public_key, original_file, encrypted_file): - with open(original_file, 'rb') as f: - plaintext = f.read() - - encrypted_content = encrypt_hybrid(public_key, plaintext) - - with open(encrypted_file, 'wb') as f: - f.write(encrypted_content) - - -# function to load a public key from a file -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/lib/tests/test_encryption.py b/delivery1/lib/tests/test_encryption.py index f7944ff..9329554 100644 --- a/delivery1/lib/tests/test_encryption.py +++ b/delivery1/lib/tests/test_encryption.py @@ -1,7 +1,6 @@ import os -from decryption_functs import * -from encryption_functs import * +from asymmetric_functs import * from key_pair import * def test_encryption_no_pwd():