首页 > 编程语言 > 详细

python 实现RSA数字签名

时间:2020-12-25 11:44:18      阅读:157      评论:0      收藏:0      [点我收藏+]
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_v1_5 as Cipher_pkcs1_v1_5
from Crypto.Signature import PKCS1_v1_5 as Signature_pkcs1_v1_5
import base64

# 私钥
private_key = ‘‘‘-----BEGIN RSA PRIVATE KEY-----
5353dfggd
-----END RSA PRIVATE KEY-----
‘‘‘

# 公钥
public_key = ‘‘‘-----BEGIN PUBLIC KEY-----
hfgghftetet
-----END PUBLIC KEY-----‘‘‘
def rsa_sign(plaintext, hash_algorithm=Crypto.Hash.MD5):
    """RSA 数字签名,私钥进行签名"""
    signer = Signature_pkcs1_v1_5.new(RSA.importKey(private_key))

    # hash算法必须要pycrypto库里的hash算法,不能直接用系统hashlib库,pycrypto是封装的hashlib
    hash_value = hash_algorithm.new(plaintext.encode(utf-8))
    signature = signer.sign(hash_value)
    signature = base64.b64encode(signature)
    return signature.decode()


def rsa_verify(sign, plaintext, hash_algorithm=Crypto.Hash.MD5):
    """校验RSA 数字验签,公钥进行验签"""
    sign = base64.b64decode(sign)
    hash_value = hash_algorithm.new(plaintext.encode(utf-8))
    verifier = Signature_pkcs1_v1_5.new(RSA.importKey(public_key))
    return verifier.verify(hash_value, sign)

 

python 实现RSA数字签名

原文:https://www.cnblogs.com/mian-1122/p/12991052.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!