废话不多说,直接开始干活!
以下是网友要求!
安装python openssl
pip install pyopenssl
私钥证书
server.key
-----BEGIN RSA PRIVATE KEY-----
MIIEogIBAAKCAQEArsp+p8ZHoBbga9YsfUSrJEp+e4q6D5mVElOtUGBytjSY1I2a
WPADRL8llv53MzhezrMhVSuTyeqtyaJ1Y/NSCAMZ6U50XXU5mH3ghu2SH6VPYnll
iwngnoxInhtUSjxwoBwFxtZGdpM+NhPnGHJBNbGNF+MeQyTuylz4MktMkB/ibTsN
RnNmpH10DMqEDF8tLgeTci1rcY6JZvgvsE5/C4hX9EguwoxaqIm+QlKEUh/7LViM
8nMtB1ebIAJvmIikGRpr5HJr11z5/f02d12TWb59gYTw4GR0SdqkftuBsjMNfW5+
V5WxNXzowC+D189vWgFSd1gzWOmmV2RkgI3g7QIDAQABAoIBAAkitElN57h0+aMp
PTIJlhhTfGuLQ1DwFLEx28EeXrXUke2qE63KUva+NtdpfWQZR3tDiYkgufINE1WS
lCghM4xk/Rm17AcRIBgu8WlGns3gn1bk7sBm8m0sT9n+UQLVnd1VxEeG3i/xtN1c
Cka7fK+zE/jHpMaa9jrFboN9GVyponjCW6yQ4AJrzpNr15ekmAcnQfH6M5WFgoSV
7h/pKiOvqIPfXup3o0cBnQYJUTX/tiEANbOQt2JQ4f3P9uDWi4q8tpY82J+3rMl1
opx7pobM80EUKshNoe6n+693b861FbImn2EsLAcLF3LhtY4h8tvV/E4sfAaKCZFa
R2D04VECgYEA4NMZ0kN5ajbKg2p3IhxNukp/2N2hCHxC8AQrDW9qmtWpaoN5a4of
prCrsnb2vMNkaFRZK8blI7y2SFfA/aQDyRigarqEIMy7fbtGO5la9X9M0hMBRkHx
8Dw2Vrba1dhau9M/3n8+ofwqB7DxvVZ3VOIvAbOPkNLVWW8VvTi//+8CgYEAxwdI
qM5riy3v2d868UXAwdSJVzylezGZ670/7U0zQjdUssg1fgabtPdm9mVXc4EchfCK
FtkpxIGEZujLaIcyUTCwHXFsCVSMa+7T+QPOxe23K9QAPSsfJCsxuyNY6l7PhQqe
hChkbCfzbYSKxZ7HI4CglGo9QojQoVs2+1ehEOMCgYBCsv4/HhuAQl8UW9qaORFq
Sj3hpebncI4fTO1E4CAdbXQEkJrOJ706I8HcgWS0HbfFJ/kIfspuLsdZcQdleGpT
F8ZHw6v9Wx/ork2o0tdka+7riA3hvSC85/ArC7aiqLxKauLQ7Kk1P21suLcPN/7R
JQVTGOVnztTS4pztdU+o6wKBgEr/u6mg33W7Ab8gF54Dovdnl7l/6PvbXR0wW36Y
sNAq+YfFWiL7aoFvGSWQWrvz00PFuMOTYeHizHnbD4t8JsHhjvwJWaTKb3/cp5QW
kzySr4ucf9qM/Vcrz7mtQDxRLfH8Zm92qSD4jQqf/4soofIG7nv+7Ma9t6CWNsnP
46nxAoGAT45oUv7pG33NpzJ+yur+Lka3Aa80Pl6SkY6Ot9OTTN6VT0kPKFfSkQ2k
iI2skurIPUtmo0NsSNF+9ixdG012P4Al09SrP4X6v+kUJg6IZHsFvAzikGPoc1X+
RWOP+TNVR1+W/MgEDuTOn4X583SEj3DF2WlsfSUaM/E5VWb4F48=
-----END RSA PRIVATE KEY-----
直接上代码
import warnings
warnings.filterwarnings("ignore") #取消报错警告
from OpenSSL.crypto import load_privatekey, FILETYPE_PEM, sign
import base64
key = load_privatekey(FILETYPE_PEM, open("server.key").read())
content = 'test_message'
d = sign(key, content, 'sha1') # d为经过SHA1算法进行摘要、使用私钥进行签名之后的数据
b = base64.b64encode(d) # 将d转换为BASE64的格式
print(key)
print(b)
结果图
D:\py3.8\src\venv\Scripts\python.exe D:/py3.8/src/马拉松/openssl.py
<OpenSSL.crypto.PKey object at 0x0000000002132438>
b'LoZwn3CVDqQFihkdWlIcPsb99C56upV+oQXflqA+B6RUtzrQ3p+j9MCJvIIHa7MtRKBcLzPt7LnKyKtZMeJbuPCb7MNVPtXUEPpjkdyspuNsh7MmYVKucNUS4bAPugZvLzQ38vPk6n748iSnQXfpjkGT1GZRrOMrPr5/m7GmGOmNPj3gTL0555I1kLbTz0INaqETi/WmI7tm1ZFh9L+Wn/1Sf11SwsScQi317O6/7nUaysjNSIEDtqK33AEcBW11HKYAUvYpzoiPpHBP6fQnB8mz7kwu/7c6Gfzk8M7ra9adfjl/cpAyB223oKdjfIYbP+MIzxAjq6X0vYRU9Xl/VQ=='
Process finished with exit code 0