Search code examples
c++encryptionaespacketinitialization-vector

AES encryption in game server client/server


I run a game server, i got main server that handles all packets sent from players (clients).

I want to encrypt my packets via AES so that each packet should be unique (i think i need IV here) and that server should accept each encrypted packet one time only, so that if someone tried to sniff a packet he cannot send it again.

How do I do this?

P.S i code server and client in c++


Solution

  • You could use some kind of SSL API like OpenSSL, but this may be overkill in your scenario as you would need to use certificates etc. There is a open source Rijndael (the algorithm that AES uses) C++ implementation here.

    Here is an example of its usage:

    void testEncryptBlock()
    {
        const int nCharacters = 16;
        char szHex[33];
        char *EncryptedData = new char[nCharacters + 1];
        CRijndael rijndael;
        int result = rijndael.MakeKey("abcdefghabcdefghabcdefghabcdefgh");
    
        // Add some dummy data for the sake of the demo
        EncryptedData = (char*)memset(EncryptedData, 255, nCharacters); // 0xfffff...
    
        result = rijndael.EncryptBlock(EncryptedData);
    
        ... // Do something with the data
    
        delete [] EncryptedData;
    }