PBE解密python實現

java加解密參考:https://www.cnblogs.com/duanxz/p/4385713.html
import base64
from Crypto.Hash import MD5
from Crypto.Cipher import DES
import hashlib
import pyDes

def Encrypt(params):
    pwd = '12345'
    salt = '12345'
    count = 100

    hasher = MD5.new()
    hasher.update(pwd.encode())
    hasher.update(salt.encode())
    result = hasher.digest()

    for i in range(1, count):
        hasher = MD5.new()
        hasher.update(result)
        result = hasher.digest()

    des = pyDes.des(result[:8], pyDes.CBC, padmode=pyDes.PAD_PKCS5, IV=result[8:16])
    encrypt_data = des.encrypt(params.encode())
    return base64.b64encode(encrypt_data)


def Decrypt(params):
    pwd = '12345'
    salt = '12345'
    count = 100

    decrypt_data  = base64.b64decode(params.encode())

    hasher = MD5.new()
    hasher.update(pwd.encode())
    hasher.update(salt.encode())
    result = hasher.digest()

    for i in range(1, count):
        hasher = MD5.new()
        hasher.update(result)
        result = hasher.digest()

    des = pyDes.des(result[:8], pyDes.CBC, padmode=pyDes.PAD_PKCS5, IV=result[8:16])
    return str(des.decrypt(decrypt_data).decode('utf-8'))

etrTimeCode = Encrypt("20191224090212")
print(Decrypt( str(etrTimeCode, encoding="utf-8")))
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章