我正在开发使用套接字的客户机/服务器应用程序,其中客户机使用Cipher AES-256加密发送加密的JSON数据,服务器负责解密接收到的那些文件并打印出来.
我在本地主机上尝试了解密,但是设置Centos Server时不起作用.来自客户端的加密数据被接收但未被解密.
这是服务器代码:
服务器代码
#!/usr/bin/python
import socket
import threading
import Encryption
class ThreadedServer(object):
def __init__(self, host, port):
self.host = host
self.port = port
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.sock.bind((self.host, self.port))
def listen(self):
self.sock.listen(5)
while True:
client, address = self.sock.accept()
client.settimeout(60)
threading.Thread(target = self.listenToClient,args = (client,address)).start()
def listenToClient(self, client, address):
size = 4096
while True:
print("Receiving")
try:
data = client.recv(size)
if data:
cipher = Encryption.Encryption('mysecretpassword')
jsondata = cipher.decrypt(data)
print(jsondata)
self.request.close()
else:
raise socket.error('Client disconnected')
except:
client.close()
return False
if __name__ == "__main__":
ThreadedServer(adress,port).listen()
加密文件的灵感来自here
解决方法:
此问题是unpad lambda函数中的兼容版本:
Python 2
unpad = lambda s : s[0:-ord(s[-1])]
Python 3
unpad = lambda s : s[0:-s[-1]]