#用aes加密,再用base64 encode def aes_encrypt(data): from Crypto.Cipher import AES import base64 key=settings.aes_key #加密时使用的key,只能是长度16,24和32的字符串 BS = AES.block_size pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS) cipher = AES.new(key) encrypted = cipher.encrypt(pad(data)) #aes加密 result = base64.b64encode(encrypted) #base64 encode return result
#把加密的数据,用base64 decode,再用aes解密 def aes_decode(data): key=settings.aes_key from Crypto.Cipher import AES import os import base64 unpad = lambda s : s[0:-ord(s[-1])] cipher = AES.new(key) result2 = base64.b64decode(data) decrypted = unpad(cipher.decrypt(result2)) return decrypted