python aes加解密
程序员文章站
2024-03-14 18:18:40
...
#coding: utf8
import sys
from Crypto.Cipher import AES
from binascii import b2a_hex, a2b_hex
from const import *
class AESHelper():
def __init__(self):
self.key = encode_secret
self.mode = AES.MODE_CBC
def encrypt(self, text):
cryptor = AES.new(self.key, self.mode, self.key)
length = 16
count = len(text)
if(count % length != 0) :
add = length - (count % length)
else:
add = 0
text = text + ('\0' * add)
self.ciphertext = cryptor.encrypt(text)
return b2a_hex(self.ciphertext)
def decrypt(self, text):
cryptor = AES.new(self.key, self.mode, self.key)
plain_text = cryptor.decrypt(a2b_hex(text))
string = plain_text.rstrip('\0')
result = ''
strings = string.split(encode_salt)
if len(strings) > 0:
result = strings[0]
return result
上一篇: 智能指针的例子
下一篇: Python实现AES加解密算法