python2执行下面代码可以成功,python3中中文总是不能加密
'''
#coding: utf-8import base64
import hashlib
from Crypto import Random
from Crypto.Cipher import AES
BS = 16
pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS)
unpad = lambda s : s[:-ord(s[len(s)-1:])]
import base64
from Crypto.Cipher import AES
from Crypto import Random
class AESCipher:
def __init__( self, key ):
self.key = key
def encrypt( self, raw ):
raw = pad(raw)
iv = Random.new().read( AES.block_size )
cipher = AES.new( self.key, AES.MODE_CBC, iv )
return base64.b64encode( iv + cipher.encrypt( raw ) )
def decrypt( self, enc ):
enc = base64.b64decode(enc)
iv = enc[:16]
cipher = AES.new(self.key, AES.MODE_CBC, iv )
return unpad(cipher.decrypt( enc[16:] ))
xxx = AESCipher('1234567812345678')
text = 'abcd中国'
text1 = xxx.encrypt(text)
text2 = xxx.decrypt(text1)
print text
print text2
'''
其中一种解决办法就是改变加密模式:
AES.new(self.key, MODE_CFB, iv )
本文介绍了一个Python中使用AES加密中文字符串的问题,在Python2与Python3环境下表现不同,并提供了解决方案,即通过更改加密模式为CFB模式来解决中文加密问题。
2073

被折叠的 条评论
为什么被折叠?



