因为支付宝当中需要自行实现签名,所以就用到了SHA265和RSA2,将拼接好的信息用私钥进行签名,并进行Base64编码,然后解密就用支付宝回传给用户的公钥解密就ok了,所以我就使用Crypto模块,因此就稍微学习了一下Crypto模块的使用。
#!/usr/bin/env python # -*- coding:utf-8 -*- import base64 from Crypto import Random from Crypto.Hash import SHA256 from Crypto.Signature import PKCS1_v1_5 as Signature_pkcs1_v1_5 from Crypto.Cipher import PKCS1_v1_5 as Ciper_pkcs1_v1_5 from Crypto.PublicKey import RSA # random_generator = Random.new().read # rsa = RSA.generate(1024, random_generator) # private_pem = rsa.exportKey() # public_pem = rsa.publickey().exportKey() message = "123456"
#对信息进行签名 private_key = ‘‘‘xxx‘‘‘ rsakey = RSA.import_key(private_key) signer = Signature_pkcs1_v1_5.new(rsakey) digest = SHA256.new() digest.update(message.encode("utf-8")) sign = signer.sign(digest) signature = base64.b64encode(sign) print(signature) #对信息进行解密 public_key = ‘‘‘xxx‘‘‘ rsakey = RSA.import_key(public_key) verifier = Signature_pkcs1_v1_5.new(rsakey) digest = SHA256.new() digest.update(message.encode("utf-8")) is_verify = verifier.verify(digest, base64.b64encode(signature)) print(is_verify)