/
encryption.py
57 lines (37 loc) · 1.18 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
import base64
import os
import hashlib
from Crypto import Random
from Crypto.Cipher import AES
class AESCipher:
def __init__(self, key):
self.key = base64.b64decode(key)
self.bs = AES.block_size
def encrypt(self, raw):
raw = self.pad(raw)
iv = Random.new().read(AES.block_size)
cipher = AES.new(self.key, AES.MODE_CBC, iv)
return base64.b64encode(iv + cipher.encrypt(raw.encode("utf-8")))
def decrypt(self,enc):
enc = base64.b64decode(enc)
iv = enc[:16]
cipher = AES.new(self.key, AES.MODE_CBC, iv)
plain = cipher.decrypt(enc[16:])
plain = self.unpad(plain)
return plain
def pad(self, s):
return s + (self.bs - len(s) % self.bs) * "\x00"
def unpad(self, s):
s = s.decode("utf-8")
return s.rstrip("\x00")
def generateKey():
key = base64.b64encode(os.urandom(32))
return key.decode()
def ENCRYPT(PLAIN, KEY):
c = AESCipher(KEY)
enc = c.encrypt(PLAIN)
return enc.decode()
def DECRYPT(ENC, KEY):
c = AESCipher(KEY)
dec = c.decrypt(ENC)
return dec