加密解密脚本

加密解密脚本

#!/usr/bin/env python
# -*- coding:utf-8 -*-
from Crypto.Cipher import AES
from binascii import b2a_hex,a2b_hex
# from deveops.settings import SECRET_KEY
SECRET_KEY = '1x$!#dwp2_6^tdgs1nv8pwgutbc#4m%#qaz!m!0h_f*%6fp+vt'
KEY = SECRET_KEY
KEY_LENGTH=16
def encrypt(text):
    # 这里密钥key 长度必须为16(AES-128)、24(AES-192)、或32(AES-256)Bytes 长度.目前AES-128足够用
    cryptor = AES.new(KEY[0:KEY_LENGTH], AES.MODE_CBC, KEY[0:KEY_LENGTH])
    length = KEY_LENGTH
    count = len(text)
    add = length - (count % length)
    text = text + ('' * add)
    ciphertext = cryptor.encrypt(text)
    # 因为AES加密时候得到的字符串不一定是ascii字符集的,输出到终端或者保存时候可能存在问题
    # 所以这里统一把加密后的字符串转化为16进制字符串
    return b2a_hex(ciphertext)
def decrypt(text):
    cryptor = AES.new(KEY[0:16], AES.MODE_CBC, KEY[0:16])
    plain_text = cryptor.decrypt(a2b_hex(text))
    return plain_text.rstrip('')
if __name__ == '__main__':
    e = encrypt("123456") #加密明文123456
    d = decrypt('e51bc8b1c7f3810573d0fb9d7c2f8c01') #解密密文
    print e, d
原文地址:https://www.cnblogs.com/carry00/p/14354920.html