python加解密

 1 # -*- coding: UTF-8 -*-
 2 
 3 import sys
 4 from Crypto.Cipher import AES
 5 from binascii import b2a_hex, a2b_hex
 6 
 7 class mycrypt():
 8     def __init__(self, key):
 9         self.key = key
10         self.mode = AES.MODE_CBC
11 
12     def encrypt(self, text):
13         cryptor = AES.new(self.key, self.mode, self.key)
14         length = 16
15         count = len(text)
16         add = length - (count % length)
17         text = text + ('\0' * add)
18         self.ciphertext = cryptor.encrypt(text)
19         return b2a_hex(self.ciphertext)
20 
21     def decrypt(self, text):
22         cryptor = AES.new(self.key, self.mode, self.key)
23         plain_text = cryptor.decrypt(a2b_hex(text))
24         return plain_text.rstrip('\0')

猜你喜欢

转载自www.cnblogs.com/falcon007/p/10558910.html