-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencryption.py
84 lines (61 loc) · 2.51 KB
/
encryption.py
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
73
74
75
76
77
78
79
80
81
82
83
84
from constants import Constants
from terminal import Terminal
from Crypto.PublicKey import RSA
from Crypto.Cipher import AES, PKCS1_OAEP
from secrets import token_bytes
from typing import Any
class Encryption:
secret: bytes = b""
@staticmethod
def __setup_aes_key():
Encryption.secret = token_bytes(Constants.KEY_SIZE)
@staticmethod
def __generate_nonce() -> bytes:
return token_bytes(Constants.NONCE_SIZE)
@staticmethod
def __create_cipher(nonce: bytes):
return AES.new(Encryption.secret, AES.MODE_EAX, nonce=nonce, mac_len=Constants.TAG_SIZE)
@staticmethod
def __create_cipher_new_nonce():
nonce = Encryption.__generate_nonce()
return Encryption.__create_cipher(nonce)
@staticmethod
def encrypt_aes_with_rsa(key: bytes) -> bytes:
Terminal.debug(f"creating secret...")
Encryption.__setup_aes_key()
Terminal.debug(f"secret: {Encryption.secret}")
public_key = RSA.import_key(key)
Terminal.debug(f"imported public key: {public_key}")
rsa_cipher = PKCS1_OAEP.new(public_key)
Terminal.debug(f"created cipher: can encrypt: {rsa_cipher.can_encrypt()}")
Terminal.debug(f"created rsa cipher: {rsa_cipher}")
encrypted_secret = rsa_cipher.encrypt(Encryption.secret)
Terminal.debug(f"encrypted secret: {encrypted_secret}")
return encrypted_secret
@staticmethod
def encrypt_with_aes(data: bytes) -> list[bytes]:
cipher = Encryption.__create_cipher_new_nonce()
ciphertext, tag = cipher.encrypt_and_digest(data)
nonce = cipher.nonce
Terminal.debug(f"nonce: {nonce}")
Terminal.debug(f"tag: {tag}:")
Terminal.debug(f"ciphertext: {ciphertext}:")
return [nonce + tag + ciphertext]
@staticmethod
def decrypt_with_aes(data: bytes) -> bytes:
nonce_size = Constants.NONCE_SIZE
nonce_tag_size = Constants.NONCE_SIZE + Constants.TAG_SIZE
nonce = data[:nonce_size]
tag = data[nonce_size:nonce_tag_size]
ciphertext = data[nonce_tag_size:]
Terminal.debug(f"nonce: {nonce}")
Terminal.debug(f"tag: {tag}")
Terminal.debug(f"ciphertext: {ciphertext}")
cipher = Encryption.__create_cipher(nonce)
data = cipher.decrypt(ciphertext)
try:
cipher.verify(tag)
return data
except ValueError:
Terminal.error("error while decrypting data: decryption key is incorrect or message is corrupted.")
return b""