def AES_Encrypt(self, aesKey, vi, data):
if len(aesKey) <= 0 or len(vi) <= 0:
loggin.log_info('请输入aesKey或者vi偏移量值!')
else:
try:
cipher = AES.new(aesKey.encode('utf-8'), AES.MODE_GCM, vi.encode('utf-8')) # 加密模式为: GCM
encryptedbytes = cipher.encrypt(data.encode('utf-8'))
encrypt_value = base64.b64encode(encryptedbytes).decode('utf-8')
return encrypt_value
except Exception as e:
loggin.log_info("Exception捕获到现异常了:{0}-->{1}\n".format(e, traceback.format_exc()))
'''
进行解密操作
'''
def AES_Decrypt(self, aesKey, vi, data):
if len(aesKey) <= 0 or len(vi) <= 0:
loggin.log_info('请输入aesKey或者vi偏移量值!')
else:
try:
data_bit = base64.decodebytes(data.encode('utf-8'))
de_cipher = AES.new(aesKey.encode('utf-8'), AES.MODE_GCM, vi.encode('utf-8'))
decr_value = de_cipher.decrypt(data_bit)
return decr_value.decode('utf-8')
except Exception as e:
loggin.log_info("Exception捕获到现异常了:{0}-->{1}\n".format(e, traceback.format_exc()))
python的AES加解密
于 2024-07-19 10:50:02 首次发布