Python实现AES加密解密

import base64
from Crypto.Cipher import AES


def aes_encrypt(value):
    '''
    >>> aes_encrypt('123456789')
    'CtiRP54bSBPzXFqWf2zw5g=='
    '''
    cipher = AES.new('<>?:"{}|,./;\'[]\\', AES.MODE_CBC, '~!@#$%^&*()_+{:"')
    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, '~!@#$%^&*()_+{:"')
    value = cipher.decrypt(base64.b64decode(value))
    return value[:-value[-1]].decode()

 

你可能感兴趣的:(Python)