openssl rsa 加密

openssl rsa 生成:https://zhuanlan.zhihu.com/p/181378111
rsa: https://www.jianshu.com/p/7a4645691c68

from Crypto.PublicKey import RSA
from Crypto.Hash import SHA
import base64
from Crypto.Signature import PKCS1_v1_5 as PKCS1_signature
 
 
message = "需要加密的信息"
message = "busi_info=xxx&channel_id=xxx&time_stamp=xxx&uin=xxx"
# 使用私钥生成签名
with open('private_a.pem') as f:
    key = f.read()
    pri_key = RSA.importKey(key)
    signer = PKCS1_signature.new(pri_key)
    digest = SHA.new()
    digest.update(message.encode("utf8"))
    sign = signer.sign(digest)
    print(sign)
    signature = base64.b64encode(sign)
#    print(signature.decode('utf-8'))
    print(signature)
 
# 使用公钥验证签名
with open('public_a.pem') as f:
    key = f.read()
    pub_key = RSA.importKey(key)
    verifier = PKCS1_signature.new(pub_key)
    digest = SHA.new()
    digest.update(message.encode("utf8"))
   # print(verifier.verify(digest, base64.b64decode(signature)))

posted @ 2021-06-07 17:00  电竞杰森斯坦森  阅读(116)  评论(0编辑  收藏  举报