需导入库 from Crypto.PublicKey import RSA
1. 生成私钥:privateKey = RSA.generate(size) 注意参数size 必须是256的倍数,且应大于等于1024,通常情况下,size越大,密钥越难被破解。该方法返回一个私钥对象。
2. 获取私钥对应的公钥:publicKey = privateKey.publickey() 该方法返回一个私钥对应的公钥对象。
3. 获取公/私钥的具体内容:pubStr = publicKey.exportKey() privStr = privateKey.exportKey() 这两个方法返回一个string字符串对象
4. 根据公私钥string字符串获得对应的实例:public = RSA.importKey(pubStr) private = RSA.importKey(privStr)
5. 使用私钥生成签名:signature = privateKey.sign(plaintext) plaintext即为需要签名的数据内容,也可先求hash再进行签名
from Crypto.Hash import MD5
hash = MD5.new(plaintext).digest()
signature = privateKey.sign(hash)
6. 使用公钥进行验签:publicKey.verify(plaintext, signature) 返回boolean标明验签是否成功。