python pycryptodome 加密解密

 

pip3 install pycryptodome

my_private_rsa_key.bin

-----BEGIN RSA PRIVATE KEY-----
MIICXQIBAAKBgQDlOJu6TyygqxfWT7eLtGDwajtNFOb9I5XRb6khyfD1Yt3YiCgQ
WMNW649887VGJiGr/L5i2osbl8C9+WJTeucF+S76xFxdU6jE0NQ+Z+zEdhUTooNR
aY5nZiu5PgDB0ED/ZKBUSLKL7eibMxZtMlUDHjm4gwQco1KRMDSmXSMkDwIDAQAB
AoGAfY9LpnuWK5Bs50UVep5c93SJdUi82u7yMx4iHFMc/Z2hfenfYEzu+57fI4fv
xTQ//5DbzRR/XKb8ulNv6+CHyPF31xk7YOBfkGI8qjLoq06V+FyBfDSwL8KbLyeH
m7KUZnLNQbk8yGLzB3iYKkRHlmUanQGaNMIJziWOkN+N9dECQQD0ONYRNZeuM8zd
8XJTSdcIX4a3gy3GGCJxOzv16XHxD03GW6UNLmfPwenKu+cdrQeaqEixrCejXdAF
z/7+BSMpAkEA8EaSOeP5Xr3ZrbiKzi6TGMwHMvC7HdJxaBJbVRfApFrE0/mPwmP5
rN7QwjrMY+0+AbXcm8mRQyQ1+IGEembsdwJBAN6az8Rv7QnD/YBvi52POIlRSSIM
V7SwWvSK4WSMnGb1ZBbhgdg57DXaspcwHsFV7hByQ5BvMtIduHcT14ECfcECQATe
aTgjFnqE/lQ22Rk0eGaYO80cc643BXVGafNfd9fcvwBMnk0iGX0XRsOozVt5Azil
psLBYuApa66NcVHJpCECQQDTjI2AQhFc1yRnCU/YgDnSpJVm1nASoRUnU8Jfm3Oz
uku7JUXcVpt08DFSceCEX9unCuMcT72rAQlLpdZir876
-----END RSA PRIVATE KEY-----

code

# coding=utf-8

from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP, PKCS1_v1_5
import base64
from urllib import parse

def decrypt_data(inputdata, code="123456"):
    # URLDecode
    data = parse.unquote(inputdata)
    # base64decode
    data = base64.b64decode(data)
    private_key = RSA.import_key(open("/Users/macname/Desktop/encrype/my_private_rsa_key.bin").read(),passphrase=code)
    # 使用 PKCS1_v1_5,不要用 PKCS1_OAEP
    # 使用 PKCS1_OAEP 的话,前端 jsencrypt.js 加密的数据解密不了
    cipher_rsa = PKCS1_v1_5.new(private_key)
    # 当解密失败,会返回 sentinel
    sentinel = None
    ret = cipher_rsa.decrypt(data, sentinel)
    return ret

print(decrypt_data("fhZf1TKEk2HiAG3BW/kuhuSXdCvSXzxW29re1OGgtQVUyO++l6KwxorpPQdZRGgu7E4muN5HyCBRBI6QTth+RGaNnlh/7DP8y3SmHjWyiYbXxAGCY5b8Vzog3IAqOZWF1QTrU7Azn+HoMe4MO4MXmH3uNXEi63nUMJlytcEpi14="))

 

 

参考:

https://github.com/Legrandin/pycryptodome

 

posted @ 2019-09-08 16:04  anobscureretreat  阅读(716)  评论(0编辑  收藏  举报