Pycrypto字符串太长,无法签名/验证

时间:2016-04-10 19:29:18

标签: python rsa digital-signature pycrypto

我有这个代码,但是当我运行它时,它只接受某个lenth的“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

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

1 个答案:

答案 0 :(得分:4)

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

关于签名消息,文档说:

  

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

注意。 signverify的文档说:

  

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

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