-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrypto.py
44 lines (36 loc) · 1.39 KB
/
crypto.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
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import padding
import os
# NOT SECURE reuses iv+key, EXPERIMENT
class EncryptionManager:
def __init__(self):
self.key = os.urandom(32)
self.iv = os.urandom(16)
def encrypt_message(self, message):
encryptor = Cipher(algorithms.AES(self.key), modes.CBC(self.iv), backend=default_backend()).encryptor()
padder = padding.PKCS7(128).padder()
padded_message = padder.update(message)
padded_message += padder.finalize()
ciphertext = encryptor.update(padded_message)
ciphertext += encryptor.finalize()
return ciphertext
def decrypt_message(self, ciphertext):
decryptor = Cipher(algorithms.AES(self.key), modes.CBC(self.iv), backend=default_backend()).decryptor()
unpadder = padding.PKCS7(128).unpadder()
padded_message = decryptor.update(ciphertext)
padded_message += decryptor.finalize()
message = unpadder.update(padded_message)
message += unpadder.finalize()
return message
enc = EncryptionManager()
plaintexts = [
b"SHORTY",
b"MEDMEDMEDMEDMEDMED",
b"LONGLONGLONGLONGLONGLONG"
]
for text in plaintexts:
e = enc.encrypt_message(text)
print(e)
d = enc.decrypt_message(e)
print(d)