Search code examples
c#socketsssltls1.2

How to implement an easy secure socket connection


I'm writing a simple client-server game in c#. my prototype works well with socket programming. i used socket class with SocketAsyncEventArgs for asyncing my code. now for alpha version i like (must) create a secure connection. but i don't have any background about security and SSL and TLS.

I found a class for this purpose: sslstream. but .Net docs are not understandable for me as a beginner. another problem is SocketAsyncEventArgs doesn't work by streams like TCPClient Class.

so can anyone guide me how to implement a simple secure connection with sockets? there is two requirements:
1- it should be fast an low-latency and I appreciate it supports non blocking (async) pattern.
2- it should support very small messages. my game commands are 10 bytes average. I don't like encrypted commands become 128 bytes!

because my server is not public and only my clients connect to it, i don't like to register and buy a certificate.

please provide a step-by-step answer. first how to create some keys (Public- private... i don't know) second how to authenticate client and third how to communicate securely.

sincerely Thanks for reading this question until end and previously thanks for your answers. best regards!


Solution

  • for those how are not familiar with cryptography like me, there is a very good start point for creating a secure scheme in the Microsoft docs. .NET Framework Cryptography Model

    Creating a cryptographic scheme is not a trivial task! (Source)

    but this tutorial is basic material about implementing a basic security system.

    EDIT: Microsoft have deleted the page about creating a cryptographic scheme, so i paste a google chached version for you:

    Creating a Cryptographic Scheme

    The cryptographic components of the .NET Framework can be combined to create different schemes to encrypt and decrypt data.

    A simple cryptographic scheme for encrypting and decrypting data might specify the following steps:

    • Each party generates a public/private key pair.

    • The parties exchange their public keys.

    • Each party generates a secret key for TripleDES encryption, for example, and encrypts the newly created key using the other's public key.

    • Each party sends the data to the other and combines the other's secret key with its own, in a particular order, to create a new secret key.

    • The parties then initiate a conversation using symmetric encryption.

    Creating a cryptographic scheme is not a trivial task.