forked from stewartmcgown/uds
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncoder.py
More file actions
26 lines (22 loc) · 688 Bytes
/
Encoder.py
File metadata and controls
26 lines (22 loc) · 688 Bytes
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
import cryptography
import os
import base64
def encrypt(chunk):
backend = default_backend()
key = os.urandom(32)
iv = os.urandom(16)
cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=backend)
encryptor = cipher.encryptor()
ct = encryptor.update(b"a secret message") + encryptor.finalize()
return ct
def decrypt(chunk):
decryptor = cipher.decryptor()
decryptor.update(ct) + decryptor.finalize()
def encode(chunk):
enc = base64.b64encode(chunk).decode()
return enc
def decode(chunk):
missing_padding = len(chunk) % 4
if missing_padding != 0:
chunk += b'='* (4 - missing_padding)
return base64.decodestring(chunk)