NetworkComms.Net makes it really easy to add pre-shared key (PSK) encryption to secure your data transmissions.  In this tutorial we will demonstrate how just 4 extra lines of code are required  to enable PSK encryption on a connection. Some considerations that you should also take when using PSK encryption will also be discussed.

Server Side

If you want to be able to decrypt traffic on a specific connection the only line that you have to add, after you’ve got the connection is:

If a packet is then received on this connection which was sent with PSK encryption, NetworkComms.Net will automatically decrypt the packet using the provided key!

To include the decryption key for all incoming connections you can add the PSK to the global options:

Client Side

On the client side you need to decide whether you want all packets sent over this connection to be encrypted or just for a single send. To send all packets encrypted simply use:

All future packets that are sent will have the payload encrypted from this point.  To use encryption on a per send basis

Passing these options as an argument to connection.SendObject() or connection.SendRecieveObject() will now result in the packet payload being encrypted.

A few notes of caution should be taken when using PSK in NetworkComms.Net.  At the time of writing certain information in the packet header will not be encrypted.  This includes the packet type, the size of the packet and the serializer and data processors used to generate the packet.  To get around this it is good practise  to set the PacketType string to something that does not reveal the context of the data being sent for example “EncryptedPacket”.