Python如何通过套接字发送元组数据(签名)

时间:2018-05-22 01:52:54

标签: python

我想发送签名&在服务器(签名)中验证但收到以下错误:

TypeError: send() argument 1 must be string or buffer, not tuple 

如何发送元组?或者有其他方式吗?

客户

from Crypto.PublicKey import RSA
from Crypto import Random
import socket
import hashlib

random_generator = Random.new().read
private_key = RSA.generate(1024, random_generator)
pri_key_str=private_key.exportKey()

pri_key_str ="""-----BEGIN RSA PRIVATE KEY-----
blah balh~
-----END RSA PRIVATE KEY-----"""

pri_key = RSA.importKey(pri_key_str)

filename = 'a.txt'
blocksize = 65536


def hashvalue(filename):        #hash value
    hash = hashlib.sha256()
    with open(filename, "rb")as f:
        for block in iter(lambda: f.read(blocksize), b""):
            hash.update(block)
    return hash.hexdigest()

print hashvalue(filename)

signature = pri_key.sign(hashvalue(filename),'')
print signature



public_key = private_key.publickey()
pub_key_str = public_key.exportKey()

pub_key_str="""-----BEGIN PUBLIC KEY-----
blah blah~
-----END PUBLIC KEY-----"""

pub_key=RSA.importKey(pub_key_str)      #create public key

if pub_key.verify(hashvalue(filename),signature):
print ("The signature is authentic")
else:
    print ("The signature is not authentic")


HOST = '127.0.0.1'
PORT = 7777
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((HOST, PORT))

client.send(hashvalue(filename))
client.send(signature )

服务器

from Crypto.PublicKey import RSA
import socket

pub_key_str="""-----BEGIN PUBLIC KEY-----
blah blah~
-----END PUBLIC KEY-----"""

pub_key=RSA.importKey(pub_key_str)      #create public_key
HOST= '127.0.0.1'
PORT = 7777
server=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
server.bind((HOST, PORT))

server.listen(10)

client, addr = server.accept()

hashv=client.recv(1024)
print "receive data ( hash ) : " ,hashv
signature=client.recv(1024)
print "recv signature (type : str)" ,signature


if pub_key.verify(hashv,a):
    print ("The signature is authentic")
else:
    print ("The signature is not authentic")

错误! 我想在服务器中验证 TypeError:send()参数1必须是字符串或缓冲区,而不是元组 请帮帮我

1 个答案:

答案 0 :(得分:0)

您不能通过套接字传递字符串或字节缓冲区以外的任何内容。

为此,您必须先将值序列化为字符串或缓冲区,然后再将其传递给套接字(发送方),然后再将其反序列化(接收方)。