很抱歉这个非常具体的问题,但我真的要疯了。我正在尝试制作一个模块,以便在需要签名或验证签名时简单地导入,但遇到了问题,无论我输入签名数据还是其他任何内容,验证器都会返回 true,代码如下:
RSA_Handler.py
from Crypto.PublicKey import RSA
from Crypto.Hash import SHA256
from Crypto.Signature import PKCS1_v1_5
import pickle
def sign(data, exported_key):
key = RSA.importKey(exported_key)
signed_data = []
signed_data.append(data)
signed_data.append(PKCS1_v1_5.new(key).sign(SHA256.new(pickle.dumps(data))))
return signed_data
def verify(signed_data, exported_key):
data = signed_data[0]
signature = signed_data[1]
key = RSA.importKey(exported_key)
h = SHA256.new(pickle.dumps(data))
try:
PKCS1_v1_5.new(key).verify(h, signature)
return True
except(ValueError, TypeError):
return False
test01.py
from RSA_Handler import *
import pickle
import os
with open("keys.txt", "rb") as rb:
keys = rb.read()
signed = sign("hello", keys)
trueorfalse = verify(["this will return"," true whatever I enter"], keys)
print(trueorfalse)
慕容森
森栏
相关分类