Repository navigation
Expand file tree
/
Copy pathKEncryption.py
More file actions
51 lines (41 loc) · 1.73 KB
/
Copy pathKEncryption.py
File metadata and controls
51 lines (41 loc) · 1.73 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
import os
from cryptography.hazmat.primitives import hashes, serialization
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend
from cryptography.fernet import Fernet
import base64
# Salting and Hashing Encryption
def derive_key(password: str, salt: bytes):
kdf = PBKDF2HMAC(
algorithm=hashes.SHA256(),
length=32,
salt=salt,
iterations=100000,
backend=default_backend()
)
return base64.urlsafe_b64encode(kdf.derive(password.encode()))
# Double Encryption
def encrypt_data(key, plaintext):
# AES Encryption
cipher = Cipher(algorithms.AES(key), modes.OFB(b'16 byte string'), backend=default_backend())
encryptor = cipher.encryptor()
ciphertext = encryptor.update(plaintext.encode()) + encryptor.finalize()
# Fernet Encryption
fernet_key = Fernet.generate_key()
cipher_suite = Fernet(fernet_key)
encrypted_text = cipher_suite.encrypt(ciphertext)
return fernet_key, encrypted_text
def decrypt_data(key, fernet_key, encrypted_text):
cipher_suite = Fernet(fernet_key)
decrypted_aes_text = cipher_suite.decrypt(encrypted_text)
# AES Decryption
cipher = Cipher(algorithms.AES(key), modes.OFB(b'16 byte string'), backend=default_backend())
decryptor = cipher.decryptor()
return decryptor.update(decrypted_aes_text) + decryptor.finalize()
SALT = os.urandom(16)
PASSWORD = os.environ.get("PASSWORD")
derived_key = derive_key(PASSWORD, SALT)
API_KEY = "YOUR_API_KEY"
fernet_key, encrypted_data = encrypt_data(derived_key, API_KEY)
decrypted_data = decrypt_data(derived_key, fernet_key, encrypted_data).decode()