openssl genrsa -out private_key.pem 2048
openssl rsa -in private_key.pem -pubout -out public_key.pem
import rsa
with open('rsa/private_key.pem', 'r') as f:
private_key = rsa.PrivateKey.load_pkcs1(f.read().encode())
with open('rsa/public_key.pem', 'r') as fp:
public_key = rsa.PublicKey.load_pkcs1_openssl_pem(fp.read().encode())
tmp_str = "abcdABCD"
# 私钥加签
sign_msg = rsa.sign(tmp_str.encode(), private_key, 'SHA-1')
#公钥验签
rsa.verify(tmp_str.encode(), sign_msg, public_key)
运用模式:
客户端将内容按特定的字段、顺序组合进行私钥加签,再发送给服务端,服务端需以相同的顺序、字段进行验证。