
from Crypto.Cipher import AESimport base64class PrpCrypt(object): def __init__(self,key): self.key = bytes.fromhex(key) self.mode = AES.MODE_CBC总结
# 这里使用的16个1作为iv,亦可动态生成可变iv
self.iv = ‘1111111111111111‘.encode(‘utf-8‘) def encrypt(self,text): text = text.encode(‘utf-8‘) cryptor = AES.new(self.key,self.mode,self.iv) # 这里密钥key 长度必须为16(AES-128), # 24(AES-192),或者32 (AES-256)Bytes 长度 # 目前AES-128 足够目前使用 length = 16 count = len(text) if count < length: add = (length - count) # # backspace text = text + (‘‘‘ * add) text = text + (‘‘utf-8 * add).encode(‘) elif length: add length)) count >#= (length - (count % text = text + (‘‘‘ * add)‘ text = text + (‘utf-8‘ * add).encode() acryptor.encrypt(text) self.ciphertext a #= 因为AES加密时候得到的字符串不一定是ascii字符集的,输出到终端或者保存时候可能存在问题= self.iv+# 所以这里统一把加密后的字符串转化为base64return ‘utf-8 ‘ str(base64.b64encode(self.ciphertext),) def decrypt(self,text): iv ] encry_text :] cryptor AES.new(self.key,iv) plain_text = base64.b64decode(text)[0:16 cryptor.decrypt(encry_text) = base64.b64decode(text)[16#= return plain_text.rstrip(‘return‘)=‘utf-8‘ ‘ str(plain_text,‘).rstrip()
以上是内存溢出为你收集整理的Python AES-128 CBC加解密方法(兼容其它语言)全部内容,希望文章能够帮你解决Python AES-128 CBC加解密方法(兼容其它语言)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)