RSA 校验指南



RSA 公钥

-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAspBtck6nudJahz/5lEvR
wYV5PF1zTj3UnZU9768TEK20nBTSU5BW+LuMKO6h7diyYfMlAUUsxabuDqC5T1pR
yJATt46m0kmCoWvCr86IoFMU8nzVAIUBpxpMZ7qsKV4ygfBE8MRRel4ECnkQRAbH
U/nu732oAYqHhB46w3zcQCGb7TCQDo5VWO5BqKWfY8Y/zu1Uxb2OQKVPRt6kX7mW
jRgdP5PsQpRRcktkwjEu/v0/QdRArKwy3vKiHOo9X08S5MWENo6Z0RUTTrT2v641
FUka2WFUrxHbjvNpZOHK0nsdCrt+SQr+gjH/SoPeUVmyFkDz0JS5T0YaLAXzDgG2
wQIDAQAB
-----END PUBLIC KEY-----
    

示例代码:校验


Requirements
pip install pycryptodome
    
Code
import json, base64, hashlib
from Crypto.PublicKey import RSA
from Crypto.Hash import SHA256
from Crypto.Signature import PKCS1_v1_5 as PKCS1_signature
from Crypto.Cipher import PKCS1_v1_5 as PKCS1_cipher

public_rsa = RSA.importKey(open("rsa_public_key.pem").read())
public_signer = PKCS1_signature.new(public_rsa)

exportJson = json.loads(open("xxxxxx.json").read())

verify = exportJson['info']['verify']
del exportJson['info']['verify']

verifyJsonStr = json.dumps(exportJson, ensure_ascii=False, separators=(',',':'))

digest = SHA256.new()
digest.update(verifyJsonStr.encode("utf-8"))

result = public_signer.verify(digest, base64.b64decode(verify))
print(result) # True即为校验成功