-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcipher.py
49 lines (41 loc) · 1.36 KB
/
cipher.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
#-*- coding: utf-8 -*-
import base64
import hashlib
from Crypto.Cipher import AES
import json
key = "pangishandsome"
BS = 16
pad = (lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS).encode())
unpad = (lambda s: s[:-ord(s[len(s)-1:])])
class AESCipher(object):
def __init__(self, key):
self.key = hashlib.sha256(key.encode()).digest()
def encrypt(self, message):
message = message.encode()
raw = pad(message)
cipher = AES.new(self.key, AES.MODE_CBC, self.__iv().encode('utf-8'))
enc = cipher.encrypt(raw)
return base64.b64encode(enc).decode('utf-8')
def decrypt(self, enc):
#print("DECRYPT1 : ",enc) # test
enc = base64.b64decode(enc)
#print("DECRYPT2 : ",enc) # test
cipher = AES.new(self.key, AES.MODE_CBC, self.__iv().encode('utf-8'))
dec = cipher.decrypt(enc)
#print("DECRYPT3 : ",dec) # test
try:
return unpad(dec).decode('utf-8')
except Exception as e:
#print("err in cipher:", e)
error = {"msg":"Decrypt Error!"}
return json.dumps(error)
def __iv(self):
return chr(0) * 16
def packet_encrypt(message):
aes = AESCipher(key)
encrypt = aes.encrypt(message)
return encrypt
def packet_decrypt(message):
aes = AESCipher(key)
decrypt = aes.decrypt(message)
return decrypt