-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencryption_util.py
More file actions
72 lines (53 loc) · 2.1 KB
/
encryption_util.py
File metadata and controls
72 lines (53 loc) · 2.1 KB
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import os
from os import urandom
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad
from cryptography.hazmat.primitives.asymmetric import padding
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.serialization import load_pem_public_key
from utils import getConfigData, exitHandler
# Get the configuration data
json_data = getConfigData()
# Get the path of public key
public_key_path = json_data.get("public_key_path")
if public_key_path is None:
exitHandler("Public key path is not configured!")
# redefine the variable with expanded path
public_key_path = os.path.expanduser(public_key_path)
# Perform necessary validations
if not os.path.exists(public_key_path):
exitHandler(f"Public key not found! {public_key_path}")
if os.path.getsize(public_key_path) == 0:
exitHandler(f"Public key file is empty! {public_key_path}")
# AES block_size
block_size = AES.block_size
def encrypt_file(file_path, file_data, key):
"""Encodes the file data, Encrypts it and writes the data into file"""
# Generate a random initialization vector (IV)
iv = urandom(block_size)
# Create AES cipher in CBC mode
cipher = AES.new(key, AES.MODE_CBC, iv)
# Pad the plaintext to be a multiple of block size
padded_data = pad(file_data.encode("utf-8"), block_size)
# Encrypt the data
ciphertext = cipher.encrypt(padded_data)
# Write the IV + ciphertext to the output file
with open(file_path, "wb") as f:
f.write(iv + ciphertext)
def encrypt_aes_key(aes_key_path, aes_key):
"""Encrypts the encryption key with public key"""
# Load the public key from the file
with open(public_key_path, "rb") as f:
public_key = load_pem_public_key(f.read())
# Encrypt the AES key with the public key
encrypted_aes_key = public_key.encrypt(
aes_key,
padding.OAEP(
mgf=padding.MGF1(algorithm=hashes.SHA256()),
algorithm=hashes.SHA256(),
label=None,
),
)
# Save the encrypted AES key to a file
with open(aes_key_path, "wb") as f:
f.write(encrypted_aes_key)