forked from aaPanel/BaoTa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
panelAes.py
93 lines (80 loc) · 3.01 KB
/
panelAes.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
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
# coding: utf-8
# | Python AES
# +--------------------------------------------------------------------
from Crypto.Cipher import AES
import base64,sys
class aescrypt_py3():
def __init__(self,key,model = 'ECB',iv = None,encode_='utf-8'):
self.encode_ = encode_
self.model = {'ECB':AES.MODE_ECB,'CBC':AES.MODE_CBC}[model]
self.key = self.add_16(key)
if model == 'ECB':
self.aes = AES.new(self.key,self.model)
elif model == 'CBC':
self.aes = AES.new(self.key,self.model,iv)
def add_16(self,par):
par = par.encode(self.encode_)
while len(par) % 16 != 0:
par += b'\x00'
return par
def aesencrypt(self,text):
text = self.add_16(text)
self.encrypt_text = self.aes.encrypt(text)
return base64.encodebytes(self.encrypt_text).decode().strip().replace("\n","")
def aesdecrypt(self,text):
text = base64.decodebytes(text.encode(self.encode_))
self.decrypt_text = self.aes.decrypt(text)
return self.decrypt_text.decode(self.encode_).strip('\0')
# base64 编码
def encode_base64(self, data):
str2 = (data).strip()
if sys.version_info[0] == 2:
return base64.b64encode(str2)
else:
return str(base64.b64encode(str2.encode('utf-8')))
# base64 解码
def decode_base64(self, data):
import base64
str2 = (data).strip()
if sys.version_info[0] == 2:
return base64.b64decode(str2)
else:
return str(base64.b64decode(str2))
class aescrypt_py2():
def __init__(self,key,model = 'ECB',iv = None,encode_='utf-8'):
self.encode_ = encode_
self.model = {'ECB':AES.MODE_ECB,'CBC':AES.MODE_CBC}[model]
self.key = self.add_16(key)
if model == 'ECB':
self.aes = AES.new(self.key,self.model)
elif model == 'CBC':
self.aes = AES.new(self.key,self.model,iv)
def add_16(self,par):
par = par.encode(self.encode_)
while len(par) % 16 != 0:
par += b'\x00'
return par
def aesencrypt(self,text):
text = self.add_16(text)
self.encrypt_text = self.aes.encrypt(text)
return base64.b64encode(self.encrypt_text).decode().strip().replace("\n","")
def aesdecrypt(self,text):
text = base64.b64decode(text.encode(self.encode_))
self.decrypt_text = self.aes.decrypt(text)
return self.decrypt_text.decode(self.encode_).strip('\0')
############## base64 #############
# base64 编码
def encode_base64(self, data):
str2 = (data).strip()
if sys.version_info[0] == 2:
return base64.b64encode(str2)
else:
return str(base64.b64encode(str2.encode('utf-8')))
# base64 解码
def decode_base64(self, data):
import base64
str2 = (data).strip()
if sys.version_info[0] == 2:
return base64.b64decode(str2)
else:
return str(base64.b64decode(str2))