2

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'))
1

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Just read your answer - out of curiousity, why did you write 'abcd1234efgh5678' in the fourth line of your code?
The password needs to be 16 bytes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.