Reputation: 81
I am trying to secure messages that travel between a Python socket server and client. I have done research and found the following links: Wrapping an existing socket in SSL - Python Python socket client and server http://the.randomengineer.com/2013/10/11/a-practitioners-overview-to-ssl-and-viewing-the-certificate-chain-from-python/
and have viewed the Python documentation: https://docs.python.org/3.5/library/ssl.html
But I still do not understand exactly how to do this. Most tutorials found online are referring to Apache or HTTP servers.
Here is some code that may explain exactly what I am trying to ask:
Client:
import socket, sys
host = 'localhost'
port = 5558
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.connect((host,port))
except socket.error as e:
print (str(e))
y = input('Press enter to close')
sys.exit()
data = 'test'
s.sendall(str.encode(data))
Server:
import socket, time
host = ''
port = 5558
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((host, port))
s.listen(10)
sock, addr = s.accept()
data = sock.recv(2048)
print(data.decode('utf-8'))
Upvotes: 2
Views: 1670
Reputation: 1039
Try to simply encrypt your message with AES from PyCrypto.
Example:
from Crypto.Cipher import AES
from Crypto import Random
IV = Random.new().read(32)
c = AES.new('abcd1234efgh5678', AES.MODE_CFB, IV)
data = c.encrypt('test')
c.decrypt(data)
The code is not tested.
Upvotes: 3