import sys
from Crypto.Cipher import AES
from binascii import b2a_hex, a2b_hex
class prpcrypt():
def __init__(self, key):
self.key = key
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))
return plain_text.rstrip('\0')
if __name__ == '__main__':
pc = PrpCrypt('keys')
e = pc.encrypt("tests")
d = pc.decrypt(e)
print ('加密:'+ e)
print ('解密:'+ d)
AES питон шифрования и дешифрования
рекомендация
отblog.csdn.net/weixin_43252204/article/details/104161331
рекомендация
ранжирование