赞
踩
import base64 from Cryptodome.Cipher import AES class AESECB: def __init__(self, key): self.key = key # 加密密钥 self.mode = AES.MODE_ECB # 设置为ECB模式 self.bs = 16 # block size self.PADDING = lambda s: s + (self.bs - len(s) % self.bs) * chr(self.bs - len(s) % self.bs) def encrypt(self, text): generator = AES.new(self.key, self.mode) # ECB模式无需向量iv crypt = generator.encrypt(self.PADDING(text).encode('utf-8')) crypted_str = base64.b64encode(crypt).decode('utf-8') return crypted_str def decrypt(self, text): generator = AES.new(self.key, self.mode) # ECB模式无需向量iv decrpyt_bytes = base64.b64decode(text) meg = generator.decrypt(decrpyt_bytes).decode('utf-8') return meg[:-ord(meg[-1])] if __name__ == '__main__': aes = AESECB('NW9dsRKN7seEyWXv'.encode("utf-8")) print(aes.encrypt('{"x":152.675,"y":5}')) print(aes.decrypt('vfI2O7n3Wz1K7/MezgkwVVKcyMLSoZA6UOTbxjQzUM8='.encode("utf-8")))
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。