gpt4 book ai didi

python - PyCrypto 导出/导入签名

转载 作者:太空宇宙 更新时间:2023-11-04 10:24:36 26 4
gpt4 key购买 nike

创建了一个带套接字的客户端-服务器应用程序,我正在尝试将签名从客户端传输到服务器。我将它从元组转换为字符串,然后再转换回元组。但签名停止工作。如何解决?

from Crypto.Hash import SHA256
from Crypto.PublicKey import RSA

public_key_file = open('public.pem','r')
public_key = RSA.importKey(public_key_file.read())

signature = "(90392831408741910958006452852395405116864328891950288888434929210668328849466319419951775157374761930395371626801844365799774616689823184955256615103504859356914334395152128600862146719619859327119380994333493461955529620578485576675021993313219918726432622856542420570716350341841652548574072964446809201965L,)"
signature_tuple = signature.split(",")
message = "Block_Height:1 From:c52030257a864a67ae4ef8a726282ed2b6b273fbccb474885027a857 To:2 Amount:3"

if public_key.verify(message, signature_tuple) == True:
print "Signature valid"

.

Traceback (most recent call last):
File "C:\Users\kucerjan\Desktop\test\sco\public_test.py", line 12, in <module>
if public_key.verify(message, signature_tuple) == True:
File "build\bdist.win32\egg\Crypto\PublicKey\RSA.py", line 221, in verify
return pubkey.pubkey.verify(self, M, signature)
File "build\bdist.win32\egg\Crypto\PublicKey\pubkey.py", line 126, in verify
return self._verify(M, signature)
File "build\bdist.win32\egg\Crypto\PublicKey\RSA.py", line 257, in _verify
return self.key._verify(m, s)
File "build\bdist.win32\egg\Crypto\PublicKey\_slowmath.py", line 73, in _verify
return self._encrypt(sig) == m
File "build\bdist.win32\egg\Crypto\PublicKey\_slowmath.py", line 65, in _encrypt
return pow(m, self.e, self.n)
TypeError: unsupported operand type(s) for pow(): 'str', 'long', 'long'

此签名已使用 str(signature) 转换为字符串。我基本上需要将它转换为字符串并返回。

函数引用:https://www.dlitz.net/software/pycrypto/api/current/Crypto.PublicKey.RSA._RSAobj-class.html#verify

公钥:

-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDFiMH7Lbd4JPFug8TaxX1DT8ad
lzzGm7CG1js0IQn2pCPPWBS+io1i0iUPmj78IOtUuoBqtEYGPgwqguYHozBuvdJy
Lcz4C2bYcjb2l8mQ4PM7iaCN4eHB+4xa+iJduogTjq8gx5m3j5mttEGUbZc2Q/AO
yde592P2iuRIrXcLuwIDAQAB
-----END PUBLIC KEY-----

最佳答案

问题在于反序列化签名元组。

PyCrypto 需要一个以整数作为第一个值的元组,您向它传递一个以括号“(”开头的字符串,然后是一个数字的字符串版本。

而不是这样做:

signature_tuple = signature.split(",")

这样做

signature_tuple = eval(signature)

这将正确解析签名。

现在,有 security risks with using eval .所以,如果我是你,我会想出一个更好的序列化/反序列化过程。

关于python - PyCrypto 导出/导入签名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30102357/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com