python加密 AES加密解密等同于java AES加密 解密

#!/usr/bin/python
# coding:utf-8
from Crypto.Cipher import AES
from decimal import Decimal
import time
import md5
import base64
import requests
import BiDataCalc
import json
import datetime
import arrow
import sys
import pymysql
reload(sys)
sys.setdefaultencoding('utf-8')

# MD5加密
PADDING = '\0'
pad_it = lambda s: s + (16 - len(s) % 16) * PADDING
key = 'test'
iv = 'test'

# 创建md5对象
AES_KEY = "test"
MD5_KEY = "test"
# 使用aes算法,进行加密解密操作
# 为跟java实现同样的编码,注意PADDING符号自定义
def encrypt_aes(sourceStr):
    generator = AES.new(key, AES.MODE_CBC, iv)
    crypt = generator.encrypt(pad_it(sourceStr))
    crypted = base64.b64encode(crypt)
    return crypted


def decrypt_aes(cryptedStr):
    generator = AES.new(key, AES.MODE_CBC, iv)
    cryptedStr = base64.b64decode(cryptedStr)
    recovery = generator.decrypt(cryptedStr)
    decryptedStr = recovery.rstrip(PADDING)
    return decryptedStr

你可能感兴趣的:(工作,python)