Python3 AES加解密(AES/ECB/PKCS5Padding)

class AesEncry(object):
	key = "wwwwwwwwwwwwwwww"  # aes秘钥

	def encrypt(self, data):
		data = json.dumps(data)
		mode = AES.MODE_ECB
		padding = lambda s: s + (16 - len(s) % 16) * chr(16 - len(s) % 16)
		cryptos = AES.new(self.key, mode)
		cipher_text = cryptos.encrypt(padding(data).encode("utf-8"))
		return base64.b64encode(cipher_text).decode("utf-8")

	def decrypt(self, data):
		cryptos = AES.new(self.key, AES.MODE_ECB)
		decrpytBytes = base64.b64decode(data)
		meg = cryptos.decrypt(decrpytBytes).decode('utf-8')
		return meg[:-ord(meg[-1])]

  

posted @ 2019-09-05 17:33  Moses^  阅读(5197)  评论(0编辑  收藏  举报