我正在尝试验证已签名的消息,但我不断收到错误消息;
AttributeError:'_io.BufferedReader'对象没有属性'n'
我真的不知道是什么原因导致了这个错误
首先,我对AES加密的数据进行签名,然后对它进行base64编码。 然后在运行脚本时将打印出的json.dump管道传输到文件
def get_signature(message):
h = SHA256.new(message)
signature = pkcs1_15.new(priv_keyObj).sign(h)
return signature
ENCODING = 'utf-8'
print(json.dumps({
'EncryptedString': base64.standard_b64encode(encrypted_data).decode(ENCODING),
'SignedDataString': base64.standard_b64encode(get_signature(encrypted_data)).decode(ENCODING),
}))
我首先将文件读取为json,然后当我验证时,我读取了base64编码的味精并开始进行b64解码;
def verify_signature(message, signature):
h = SHA256.new(message)
try:
pkcs1_15.new(pub_key_new).verify(h, signature)
print("The signature is valid.")
except (ValueError, TypeError):
print("The signature is not valid.")
verify_signature(base64.standard_b64decode(data['EncryptedString']), base64.standard_b64decode(data['SignedDataString']))
我试图使这个问题简单易懂-请告诉我是否需要提供更多信息。
完整的追溯是;
>Traceback (most recent call last):
> File "C:/PATH/Scipts/crypto/decrypt.py", line 9, in <module>
print(default_decrypt(read_json_file(filename)).decode("utf-8"))
> File "C:\PATH\Scipts\crypto\crypt_helper_new.py", line 127, in default_decrypt
verify_signature(base64.standard_b64decode(data['EncryptedString']),
base64.standard_b64decode(data['SignedDataString']))
encoded msg: <class 'str'>
> File "C:\PATH\Scipts\crypto\crypt_helper_new.py", line 65, in verify_signature
pkcs1_15.new(pub_key_new).verify(h, signature)
message: b'S\xacU\x14\xb2E\xec\x08\xc3\x83\x18\x8ey\x98\x069'
> File "C:\PATH\AppData\Local\Programs\Python\Python36\lib\site-packages\Crypto\Signature\pkcs1_15.py", line 106, in verify
modBits = Crypto.Util.number.size(self._key.n)
> AttributeError: '_io.BufferedReader' object has no attribute 'n'
答案 0 :(得分:1)
您不能将缓冲区直接传递给该函数。您应该从文件中读取字节以创建密钥对象:
pub_key_new = RSA.import_key(open('foo.pub').read())
self._key
(即pub_key_new
)的类型应为:
<class 'Crypto.PublicKey.RSA.RsaKey'>