【问题标题】:Pycrypto string too long to sign/verifyPycrypto 字符串太长,无法签名/验证
【发布时间】:2016-04-10 19:29:18
【问题描述】:

我有这段代码,但是当我运行它时,它只接受某个长度的“to_address”变量。当它太长时,我收到一个异常:

  Traceback (most recent call last):
** IDLE Internal Exception: 
  File "C:\Python27\lib\idlelib\run.py", line 325, in runcode
    exec code in self.locals
  File "C:\Python27\lib\idlelib\run.py", line 111, in main
    seq, request = rpc.request_queue.get(block=True, timeout=0.05)
  File "C:\Python27\lib\Queue.py", line 176, in get
    raise Empty
Empty

这是我的代码:

import hashlib
import sqlite3
import socket
import time
from Crypto.PublicKey import RSA

# import keys
key_file = open('keys.pem','r')
key = RSA.importKey(key_file.read())
public_key = key.publickey()
private_key_readable = str(key.exportKey())
public_key_readable = str(key.publickey().exportKey())
address = hashlib.sha224(public_key_readable).hexdigest()

to_address = str(raw_input ("Send to address: "))
amount = str(raw_input ("How much to send: "))
timestamp = str(time.time())

transaction = str(timestamp) +":"+ str(address) +":"+ str(to_address) +":"+ str(amount)
signature = key.sign(transaction, '')
print "Client: Signature: "+str(signature)

if public_key.verify(transaction, signature) == True:
    if int(amount) < 0:
        print "Client: Signature OK, but cannot use negative amounts"

    else:
        ...process...

else:
    print "Client: Invalid signature"
    raise
#enter transaction end

如果有人知道如何绕过这个长度限制,我们将不胜感激。我是否需要以某种方式加密字符串以使其更短以便验证它然后再次解密?

【问题讨论】:

    标签: python rsa digital-signature pycrypto


    【解决方案1】:

    也许签署交易的哈希。在验证时,您可以再次使用哈希进行验证。

    关于消息的签名,文档说:

    要使用 RSA 签名的数据。它可能不会在数值上更大 比 RSA 模块 (n)。

    不过要小心。 signverify 的文档说:

    注意:此函数执行普通的原始 RSA 加密 (教科书)。在实际应用中,您总是需要使用适当的 加密填充,你不应该直接验证数据 这种方法。不这样做可能会导致安全漏洞。它 建议使用模块 Crypto.Signature.PKCS1_PSS 或 Crypto.Signature.PKCS1_v1_5 代替。

    https://www.dlitz.net/software/pycrypto/api/current/Crypto.PublicKey.RSA._RSAobj-class.html#publickey

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-16
      • 2021-01-08
      • 1970-01-01
      • 2023-03-22
      • 2018-04-02
      相关资源
      最近更新 更多