关于base64和AES结合加密解密中python3报错的情况

python2执行下面代码可以成功,python3中中文总是不能加密

'''

#coding: utf-8


import 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后端)