https://github.com/Legrandin/pycryptodome
1、加载密钥
from Crypto.PublicKey import RSA
key = RSA.import_key(open(key_path).read())
2、签名与验签
用RSA-PSS using SHA-256举例
from Crypto.Hash import SHA256
from Crypto.Signature import PKCS1_PSS
def Rsa256_sign_data(data, rsa_key):
try:
h = SHA256.new(data)
rsa_sign = PKCS1_PSS.new(rsa_key)
#new(rsa_key, mgfunc=None, saltLen=None, randfunc=None)
if rsa_sign.can_sign():
return rsa_sign.sign(h)
else:
raise Exception
except Exception as err:
log_print("RSA sign fail", '', err)
return signature
def Rsa256_verify_data(data, sig, rsa_key):
try:
h = SHA256.new(data)
return PKCS1_PSS.new(rsa_key).verify(h, sig)
except Exception as err:
print("RSA verify fail", '', err)
re