Python PyCryptome RSA签名与验证

Python PyCryptome RSA签名与验证,python,sockets,pycrypto,pycryptodome,Python,Sockets,Pycrypto,Pycryptodome,我正在开发一个程序,该程序使用套接字模块通过python在internet上传输数据。我现在尝试使用pycryptodome模块实现加密。我使用Salsa20传输普通消息,并使用RSA传输Salsa20密钥。问题是代码在验证哈希时引发了ValueError。这是协议: 客户端连接到服务器并生成Salsa20密钥 服务器生成RSA密钥对并将公钥发送到客户端 客户端生成自己的密钥对并发送公钥 客户端使用公钥加密Salsa20密钥 客户端签名并发送加密Salsa20密钥的散列 客户端发送加密的Sals

我正在开发一个程序,该程序使用套接字模块通过python在internet上传输数据。我现在尝试使用pycryptodome模块实现加密。我使用Salsa20传输普通消息,并使用RSA传输Salsa20密钥。问题是代码在验证哈希时引发了ValueError。这是协议:

客户端连接到服务器并生成Salsa20密钥 服务器生成RSA密钥对并将公钥发送到客户端 客户端生成自己的密钥对并发送公钥 客户端使用公钥加密Salsa20密钥 客户端签名并发送加密Salsa20密钥的散列 客户端发送加密的Salsa20密钥 服务器对未签名的加密密钥进行散列,并验证它是否与签名密钥相同。这是问题发生的行 服务器解密未签名的密钥 我反复检查和打印,哈希值是一样的。插座不是问题所在

recv_message和send_message函数是我用来打包发送和接收协议的函数。在程序中,套接字是预先设置好的,因此下面是服务器的重要部分

import socket
from Crypto.Cipher import Salsa20, PKCS1_OAEP
from Crypto.Signature import pkcs1_15
from Crypto.Hash import SHA256
from Crypto.PublicKey import RSA

def send_message(message_to_send, connection):
    if type(message_to_send) is bytes:
        message = message_to_send
    else:
        message = str(message_to_send).encode()
    length_of_msg = str(len(message))
    length_of_msg = ('0' * (5-len(length_of_msg))) + length_of_msg  # This part is adding zeros as padding so that the first message is always 5 chars
    connection.send(length_of_msg.encode())
    connection.send(message)

def recv_message(connection, no_decode=False):
    length_of_msg = int(connection.recv(5))
    message = connection.recv(length_of_msg)
    if not no_decode:
        message = message.decode('utf-8')
    return message

def get_key(connection):
    rsa_server_key = RSA.generate(2048)
    send_message(rsa_server_key.publickey().exportKey(), connection)
    rsa_client_key = RSA.import_key(recv_message(connection, no_decode=True))
    key_signed = recv_message(connection, no_decode=True)
    key_unsigned = recv_message(connection, no_decode=True)
    hash_verify = pkcs1_15.new(rsa_client_key)
    hash_verify.verify(SHA256.new(data=key_unsigned), key_signed)
    rsa_cipher = PKCS1_OAEP.new(rsa_server_key)
    key = rsa_cipher.decrypt(key_unsigned)
    return key
客户端具有相同的套接字相关函数和导入的模块

def share_key(key_to_send, connection):
    rsa_server_key = RSA.import_key(recv_message(connection, no_decode=True))
    rsa_client_key = RSA.generate(2048)
    send_message(rsa_server_key.publickey().exportKey(), connection)
    rsa_cipher = PKCS1_OAEP.new(rsa_server_key)
    salsa_key_unsigned = rsa_cipher.encrypt(key_to_send)
    key_signer = pkcs1_15.new(rsa_client_key)
    send_message(key_signer.sign(SHA256.new(data=salsa_key_unsigned)), connection)
    send_message(salsa_key_unsigned, connection)
回溯是:

Traceback (most recent call last):
  File "0,9Serverside.py", line 686, in <module>
    main()
  File "0,9Serverside.py", line 670, in main
    key_input = get_key(server)
  File "0,9Serverside.py", line 243, in get_key
    hash_verify.verify(SHA256.new(data=salsa_key_unsigned), salsa_key_signature)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/Crypto/Signature/pkcs1_15.py", line 139, in verify
    raise ValueError("Invalid signature")
ValueError: Invalid signature

你认为验证不起作用的原因是什么。谢谢您的帮助。

客户端正在发回服务器的公钥,而不是它自己的公钥。 该行:

 send_message(rsa_server_key.publickey().exportKey(), connection)
应该是:

 send_message(rsa_client_key.publickey().exportKey(), connection)

发布ValueError的完整堆栈跟踪。还有,这是python 3吗?@JamesKPolk是的,这是python 3。回溯最近一次调用:文件0,9Serverside.py,第686行,在主文件0,9Serverside.py,第670行,在主键输入=get_keyserver文件0,9Serverside.py,第243行,在get_key hash_verify中。verifySHA256。新数据=salsa_key_unsigned,salsa_key_签名文件/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/Crypto/signature/pkcs1_15.py,第139行,在验证提升值错误无效签名值错误:无效签名