-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencrypt.py
More file actions
104 lines (82 loc) · 3.87 KB
/
encrypt.py
File metadata and controls
104 lines (82 loc) · 3.87 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import os
import hashlib
import struct
import sys
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.ciphers.algorithms import ChaCha20
from Crypto.Cipher import Salsa20
from Crypto.Cipher import Blowfish
from cryptography.hazmat.primitives import padding
from secrets import token_bytes
CIPHER_CONFIG = {
"AES-CTR-LIB": {"KEY_SIZE": 32, "BLOCK_SIZE": 16, "NONCE_SIZE": 16},
"CHACHA20-LIB": {"KEY_SIZE": 32, "BLOCK_SIZE": 16, "NONCE_SIZE": 16},
"SALSA20-LIB": {"KEY_SIZE": 32, "BLOCK_SIZE": 16, "NONCE_SIZE": 8},
"BLOWFISH-LIB": {"KEY_SIZE": 32, "BLOCK_SIZE": 8, "NONCE_SIZE": 8},
"CAMELLIA-LIB": {"KEY_SIZE": 32, "BLOCK_SIZE": 16, "NONCE_SIZE": 8}
}
def aes_ctr_mode_lib(data: bytes, key: bytes, nonce: bytes) -> bytes:
cipher = Cipher(algorithms.AES(key), modes.CTR(nonce), backend=default_backend())
encryptor = cipher.encryptor()
return encryptor.update(data) + encryptor.finalize()
def chacha20_lib(data: bytes, key: bytes, nonce: bytes) -> bytes:
if len(nonce) < 16:
nonce = nonce.ljust(16, b'\x00')
elif len(nonce) > 16:
nonce = nonce[:16]
algorithm = ChaCha20(key, nonce)
cipher = Cipher(algorithm, mode=None, backend=default_backend())
encryptor = cipher.encryptor()
return encryptor.update(data)
def salsa20_lib(data: bytes, key: bytes, nonce: bytes) -> bytes:
if isinstance(data, str):
data = data.encode('utf-8')
cipher = Salsa20.new(key=key, nonce=nonce)
return cipher.encrypt(data)
def blowfish_lib(data: bytes, key: bytes, nonce: bytes) -> bytes:
cipher = Blowfish.new(key, Blowfish.MODE_CTR, nonce=nonce[:4])
return cipher.encrypt(data)
def camellia_encrypt_lib(data: bytes, key: bytes, nonce: bytes) -> bytes:
iv = nonce.ljust(16, b'\x00')[:16]
padder = padding.PKCS7(128).padder()
padded_data = padder.update(data) + padder.finalize()
algorithm = algorithms.Camellia(key)
cipher = Cipher(algorithm, modes.CBC(iv), backend=default_backend())
encryptor = cipher.encryptor()
return encryptor.update(padded_data) + encryptor.finalize()
def camellia_decrypt_lib(ciphertext: bytes, key: bytes, nonce: bytes) -> bytes:
iv = nonce.ljust(16, b'\x00')[:16]
algorithm = algorithms.Camellia(key)
cipher = Cipher(algorithm, modes.CBC(iv), backend=default_backend())
decryptor = cipher.decryptor()
padded_data = decryptor.update(ciphertext) + decryptor.finalize()
unpadder = padding.PKCS7(128).unpadder()
return unpadder.update(padded_data) + unpadder.finalize()
def encrypt_dataA(key: bytes, data: bytes, mode: str, nonce: bytes) -> bytes:
global chave_publica
if mode == "AES-CTR-LIB":
return aes_ctr_mode_lib(data, key, nonce)
elif mode == "CHACHA20-LIB":
return chacha20_lib(data, key, nonce)
elif mode == "SALSA20-LIB":
return salsa20_lib(data, key, nonce)
elif mode == "BLOWFISH-LIB":
return blowfish_lib(data, key, nonce)
elif mode == "CAMELLIA-LIB":
return camellia_encrypt_lib(data, key, nonce)
raise ValueError(f"Modo de cifra '{mode}' não suportado.")
def decrypt_dataA(key: bytes, data: bytes, mode: str, nonce: bytes) -> bytes:
global chave_privada
if mode == "AES-CTR-LIB":
return aes_ctr_mode_lib(data, key, nonce)
elif mode == "CHACHA20-LIB":
return chacha20_lib(data, key, nonce)
elif mode == "SALSA20-LIB":
return salsa20_lib(data, key, nonce)
elif mode == "BLOWFISH-LIB":
return blowfish_lib(data, key, nonce)
elif mode == "CAMELLIA-LIB":
return camellia_decrypt_lib(data, key, nonce)
raise ValueError(f"Modo de cifra '{mode}' não suportado.")
AVAILABLE_CIPHERS = ["AES-CTR-LIB", "CHACHA20-LIB", "SALSA20-LIB", "BLOWFISH-LIB", "CAMELLIA-LIB"]