Python實現AES加密解密
阿新 • • 發佈:2018-12-12
import base64
from Crypto.Cipher import AES
def aes_encrypt(value):
'''
>>> aes_encrypt('123456789')
'CtiRP54bSBPzXFqWf2zw5g=='
'''
cipher = AES.new('<>?:"{}|,./;\'[]\\', AES.MODE_CBC, '[email protected]#$%^&*()_+{:"')
pad_length = 16 - len(value.encode()) % 16
pad_string = pad_length * chr(pad_length)
return base64.b64encode(cipher.encrypt('%s%s' % (value, pad_string))).decode()
def aes_decrypt(value):
'''
>>> aes_decrypt('CtiRP54bSBPzXFqWf2zw5g==')
'123456789'
'''
cipher = AES.new('<>?:"{}|,./;\'[]\\', AES.MODE_CBC, ' [email protected]#$%^&*()_+{:"')
value = cipher.decrypt(base64.b64decode(value))
return value[:-value[-1]].decode()