Pycrypto 字符串太长 sign/verify

Pycrypto string too long to sign/verify

我有这段代码,但是当我 运行 它时,它只接受一定长度的 "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

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

也许签署交易的哈希值。在验证时,您可以再次使用散列来验证。

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

The piece of data to sign with RSA. It may not be numerically larger than the RSA module (n).

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

Attention: this function performs the plain, primitive RSA encryption (textbook). In real applications, you always need to use proper cryptographic padding, and you should not directly verify data with this method. Failure to do so may lead to security vulnerabilities. It is recommended to use modules Crypto.Signature.PKCS1_PSS or Crypto.Signature.PKCS1_v1_5 instead.

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