OpenSSL验签
# !/usr/bin/python3
# -*- coding: utf-8 -*-
import OpenSSL
from OpenSSL.crypto import sign,verify
import time
from dateutil import parser
def VerifyCrtAndKey(crtFileUrl, keyFileUrl) ->bool:
try:
with open(crtFileUrl) as e:
crt_data = e.read()
with open(keyFileUrl) as e:
key_data = e.read()
sskey = OpenSSL.crypto.load_privatekey(OpenSSL.crypto.FILETYPE_PEM, key_data)
cert = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM, crt_data)
signdata = sign(sskey, 'hello'.encode(), 'sha1')
conse = verify(cert, signdata, 'hello'.encode(), 'sha1')
if conse==None:
return True
except:
return False
Cryptodome验签
import rsa,base64
from Cryptodome.PublicKey import RSA
from Cryptodome.Hash import SHA256,MD5
from Cryptodome.Cipher import PKCS1_v1_5
from Cryptodome.Signature import PKCS1_v1_5
data = 'hello'
key = base64.b64decode(serverskey)
print(key)
prikey = RSA.importKey(key)
print(prikey)
signer = PKCS1_v1_5.new(prikey)
hash_obj = MD5.new(data.encode('utf-8'))
signature = base64.b64encode(signer.sign(hash_obj))
print(signature)
data2 = 'hello'
public_keyBytes = base64.b64decode(serverpkey)
pubKey = RSA.importKey(public_keyBytes)
h = MD5.new(data2.encode('utf-8'))
verifier = PKCS1_v1_5.new(pubKey)
print(verifier.verify(h, base64.b64decode(signature)))
768

被折叠的 条评论
为什么被折叠?



