For a project, I need a server and a client. Not a problem so far. The difficulty is for me, that the connection must be encrypted since sensible information will be sent. You could use RSA encryption. I just don't know yet how to exchange the keys so nobody could intercept them or get any other chance to reach them. Since I don't know, how to do it in general, I did not try anything so far.
-
1For a key exchange, diffie-helman is worth looking at, as is elliptic-curve-diffie-helman. However, setting up a TLS connection would be a good idea because it uses a well established protocol that is widely usedSamG101– SamG1012019-10-23 14:09:19 +00:00Commented Oct 23, 2019 at 14:09
-
thank you! Could you write it as an answer with a particular code example/scheme? It would be great.byTreneib– byTreneib2019-10-23 15:01:45 +00:00Commented Oct 23, 2019 at 15:01
-
please make sure to upvote my post if you think it can be helpful for other peoplebyTreneib– byTreneib2020-07-30 14:21:39 +00:00Commented Jul 30, 2020 at 14:21
Add a comment
|
1 Answer
Here is a TLS connection implementation in Python. All key exchanging and encrypting data is done within the protocol.
import socket
import ssl
def main():
#Define Host Name And Port (Port 443 Is Typical Encrypted Web Connection Port)
host_name = "www.google.com"
host_port = 443
#Create Unencrypted Connection And Then Encrypted It By Wrapping It
unencrypted_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
unencrypted_socket.settimeout(10)
encrypted_socket = ssl.wrap_socket(unencrypted_socket,ssl_version=ssl.PROTOCOL_TLSv1) #Optional Ciphers Spec Parameter Too
#Connect To The Host, Send Data And Wait To Recieve Data
encrypted_socket.connect((host_name,host_port))
encrypted_socket.send(b"Hello")
response = encrypted_socket.recv(512)
#Close The Connection
encrypted_socket.close()
main()
Note: I am using Python 3.6, and I think that a newer version of TLS is available to use as of Python 3.7.
2 Comments
byTreneib
On serverside everything works out the same, except you don't connnect to a server but bind to an address etc., as you would normally when working with sockets, right?
SamG101
Yes, it has to bind, listen and then accept an incoming connection. Each incoming connection must be wrapped with TLS on serverside