mirror of https://github.com/jumpserver/jumpserver
55 lines
1.3 KiB
Python
55 lines
1.3 KiB
Python
|
import base64
|
|||
|
from Crypto.Cipher import AES
|
|||
|
|
|||
|
from django.conf import settings
|
|||
|
|
|||
|
|
|||
|
class AESCrypto:
|
|||
|
"""
|
|||
|
AES
|
|||
|
除了MODE_SIV模式key长度为:32, 48, or 64,
|
|||
|
其余key长度为16, 24 or 32
|
|||
|
详细见AES内部文档
|
|||
|
CBC模式传入iv参数
|
|||
|
本例使用常用的ECB模式
|
|||
|
"""
|
|||
|
|
|||
|
def __init__(self, key):
|
|||
|
if len(key) > 32:
|
|||
|
key = key[:32]
|
|||
|
self.key = self.to_16(key)
|
|||
|
|
|||
|
@staticmethod
|
|||
|
def to_16(key):
|
|||
|
"""
|
|||
|
转为16倍数的bytes数据
|
|||
|
:param key:
|
|||
|
:return:
|
|||
|
"""
|
|||
|
key = bytes(key, encoding="utf8")
|
|||
|
while len(key) % 16 != 0:
|
|||
|
key += b'\0'
|
|||
|
return key # 返回bytes
|
|||
|
|
|||
|
def aes(self):
|
|||
|
return AES.new(self.key, AES.MODE_ECB) # 初始化加密器
|
|||
|
|
|||
|
def encrypt(self, text):
|
|||
|
aes = self.aes()
|
|||
|
return str(base64.encodebytes(aes.encrypt(self.to_16(text))),
|
|||
|
encoding='utf8').replace('\n', '') # 加密
|
|||
|
|
|||
|
def decrypt(self, text):
|
|||
|
aes = self.aes()
|
|||
|
return str(aes.decrypt(base64.decodebytes(bytes(text, encoding='utf8'))).rstrip(b'\0').decode("utf8")) # 解密
|
|||
|
|
|||
|
|
|||
|
def get_aes_crypto(key=None):
|
|||
|
if key is None:
|
|||
|
key = settings.SECRET_KEY
|
|||
|
a = AESCrypto(key)
|
|||
|
return a
|
|||
|
|
|||
|
|
|||
|
aes_crypto = get_aes_crypto()
|