Search code examples
python-3.xencryptiondiffie-hellmanman-in-the-middle

Securely exchanging public dsa keys for manual use in python


In a project of mine I am working towards secure TCP communications between a client and a server across an insecure network. Initially I was thinking about just passing this off to openssl, but I've concluded that (because of reasons beyond the scope of this question), I would prefer to deal with the DSA keys manually. While that part is simple enough, there is one remaining problem: How would I send the public key the server while making sure there's no MITM intercepting this key and replacing it with its own?

I have been looking into Diffie-Hellman key exchange algorithm, but I haven't found a way of implementing this in my code without reinventing the wheel, and I am struggling with finding an existing library for it, so I have decided to step back a bit and simply ask:

Is there a library or some other method that allows me to exchange keys securely, while allowing me to apply the keys manually? Basically, I want to do the actual encryption/decryption with keys myself, but I need a reliable way of exchanging the public keys involved.

Using python 3.6.9 on linux. I have access to openssl and the usual linux-tools.


Solution

  • the Diffie-Hellmen algorithm should be easy to implement, the simple example on Wikipedia highlights all the steps that are programmatically easy to execute.

    enter image description here