RSA签名和解签

from Crypto.PublicKey import RSA
from Crypto.Signature import PKCS1_v1_5
from Crypto.Hash import MD5
import base64


#签名
def RsaSign(encrData):
    #私钥
    privateKey = '''MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBALns/pt3ltkDBuPX
HQSbVO1DZChJ/kLz4+pX+S6TNSdjKfx7kTnbRvdqNw2t6rKeEfg5oworAktHQR/h
TNpmgb1Mn7SfDilrSzdoPR/uks4LC0C1XylQ7WqccwY/+qxnT0/iFCerPcIlFmGv
JQpDIFNhZe2xWHDir1f9dWk07ETJAgMBAAECgYB6A8b/Phc36Z2e7m/zswCkCIL1
UcHULHxZMUytPRw38gL+2BTxdC75dbIVfxo0CDpgvWp65MjHoEZj5pZqCbV/6BKt
7rAjp2vRL93aSywu3Cnvh3jLiyXVie/A90kK+2NddcHFk3hDAMCMG6qzz5HyQ2O3
cR3uOLiczVwIBwl0hQJBAOACDKiSixFLhGVJc1YzBE4vgueCtyYwGHWTKOn23a3u
3+GiCrMZ4EhyoCWM/FbtFL4tZtkNZCeQ6r14vXwaQeMCQQDUep8FNtmrE9UDOzsp
xdCzJUDhyxI/NQEntL9D2TxI1GNaPWp9Z+hbDlsFTnbGluLerw+KBNpY5uIdz2ng
ui5jAkEAjo5fVewkmnYPpJHQ15uR8tvjl8elHKVg+3oSgIp9WxJ44dPIsWAXTqO7
7QnDEOLB36FvIVSXV2KvMLMlC1R+dwJARD5R/sG8LYdEHjKS8Pq46iDn8t1fMe/b
PB6AY9FM3HNiqaQaJMcPyNcWguDNo0DpAu6lBgZC/tTBASQR9Hy30wJAOr0oCkGg
YKcN5nspkZSksjp2A19f6aab+3POejvnndJpiabNkwz7PLBnMfzC479STR43obtq
gKHacyQaCAAvcQ='''
    private_keyBytes = base64.b64decode(privateKey)
    priKey = RSA.importKey(private_keyBytes)
    #priKey = RSA.importKey(privateKey)
    signer = PKCS1_v1_5.new(priKey)
    hash_obj = MD5.new(data.encode('utf-8'))
    signature = base64.b64encode(signer.sign(hash_obj))
    return signature


#解签
def verify(signature,encrData):
    #公钥
    publicKey = '''MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCJ3haUf1Tj0/XuknJqsxg/N8nu3NqXhvo6Mdbw
UFuLYux0pjKlY2lCvV14Rlnl2bf0XoIaFcHZIvbexv8bXfakDhpQokyToKvK6kIlbkgKCqCzjVm0
ZthNou5cnqVyRurn3UXxELn1VfHkLc4nSZie+pMwiBRB4ViuAH1+w/gyvwIDAQAB'''
    public_keyBytes = base64.b64decode(publicKey)
    pubKey = RSA.importKey(public_keyBytes)
    #pubKey = RSA.importKey(publicKey)
    h = MD5.new(data.encode('utf-8'))
    verifier = PKCS1_v1_5.new(pubKey)
    return verifier.verify(h, base64.b64decode(signature))

你可能感兴趣的:(RSA签名和解签)