RSA中要加密的最大文本长度

时间:2015-03-03 21:59:59

标签: sockets python-2.7 network-programming python-sockets

我正在学习python中的网络编程,并正在尝试使用socket.recv方法,我观察了以下内容 我创建了一个套接字,在使用sendall方法连接到服务器之后将数据发送到服务器,服务器具有像

这样的recv方法

客户端:

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
secretText = publickey.encrypt("Hello, this is Rich.", 32)
s.sendall(pickle.dumps(secretText))
s.close()

服务器:

clientsock, clientaddr = s.accept()
print "got connection from ", clientsock.getpeername()
clientsock.send(pickle.dumps(RSAPubKey))
rcstring = ''
while 1:
     buf = clientsock.recv(1024)
     rcstring += buf
     if not len(buf):
        break
clientsock.close()
encmessage = pickle.loads(rcstring)
print RSAKey.decrypt(encmessage)

观察:

当我发送一个小文本时,输出将与输入相同(如预期的那样),但是当我发送一个大字符串时,输出将是所有乱码。我想知道你对这个案子的想法吗?

正确输出:

input:Hello, this is Rich.

Server is running on port 12345; press Ctrl-C to terminate.
got connection from  ('127.0.0.1', 2880)
Hello, this is Rich.

输出不正确:

input:Hello, this is Rich. (repeated 30 times)

Server is running on port 12345; press Ctrl-C to terminate.
got connection from  ('127.0.0.1', 2880)
ªû9ì8/├ÅY!P6in⌡¢4≡îïö╩1]µ╝ⁿ╪╞α@^É╨u'↔¿êL.¶P∩çj♀V▌J ö╚↨è& Çë£┌Σh┬⌂┼kG:┤yf╔     $♂º╝☻╤Y@°Üó8┐αO≈K§═1ª.↓@♦¼Φ↑╧┘╩>√`}ªû·P░/µE╦éjì0⌐

由于 查兰

更新:

根据建议我尝试使用make文件,这里是服务器端的实现,我没有修改客户端代码

sockfile=clientsock.makefile("rwb")
while 1:
    data = '';
    while True:
        buf=sockfile.read(2048);
        data=data+buf
        print len(buf)
        if len(buf)==0:
            print "insde the if data"
            break
    print data;
    sockfile.flush();
    sockfile.close();
    encmessage = pickle.loads(data)
    print RSAKey.decrypt(encmessage)
    clientsock.close()

即使在这种情况下,我仍然会遇到同样的错误。

更新2:

很抱歉早期的错误,我认为问题出在网络套接字上,而我的@ikegami指出的情况显然并非如此。

问题是我正在尝试使用RSA加密整个字符串,这清楚地表明存在大小限制。

我已经更新了标题,以便将来可以帮助其他人。

0 个答案:

没有答案