Encrypt Player Data

About Player Data Encryption / Decryption

To secure your player data for actual deployment of your game, rather than the text format used for development - you can encrypt and decrypt your player maps before transferring them to the server. For example, the following demonstration encrypts and decrypts the player data.

Steps necessary

Add these functions into the SaveLoadMap.cs script (inside the Scripts/Save folder)

Then, for the Save and Load functions, encrypt each line as the script saves it except for the first and last. The exact same for loading, but reversed. (If you instead decide to encrypt the entire file - you will need to update the verification step that checks the first and last lines for their encrypted versions.

Encryption function example

public static string Encrypt (string toEncrypt)
{
 byte[] keyArray = UTF8Encoding.UTF8.GetBytes ("12345678901234567890123456789012");
 // 256-AES key
 byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes (toEncrypt);
 RijndaelManaged rDel = new RijndaelManaged ();
 rDel.Key = keyArray;
 rDel.Mode = CipherMode.ECB;
 // http://msdn.microsoft.com/en-us/library/system.security.cryptography.ciphermode.aspx
 rDel.Padding = PaddingMode.PKCS7;
 // better lang support
 ICryptoTransform cTransform = rDel.CreateEncryptor ();
 byte[] resultArray = cTransform.TransformFinalBlock (toEncryptArray, 0, toEncryptArray.Length);
 return Convert.ToBase64String (resultArray, 0, resultArray.Length);
}

Decryption function example

public static string Decrypt (string toDecrypt)
{
 byte[] keyArray = UTF8Encoding.UTF8.GetBytes ("12345678901234567890123456789012");
 // AES-256 key
 byte[] toEncryptArray = Convert.FromBase64String (toDecrypt);
 RijndaelManaged rDel = new RijndaelManaged ();
 rDel.Key = keyArray;
 rDel.Mode = CipherMode.ECB;
 // http://msdn.microsoft.com/en-us/library/system.security.cryptography.ciphermode.aspx
 rDel.Padding = PaddingMode.PKCS7;
 // better lang support
 ICryptoTransform cTransform = rDel.CreateDecryptor ();
 byte[] resultArray = cTransform.TransformFinalBlock (toEncryptArray, 0, toEncryptArray.Length);
 return UTF8Encoding.UTF8.GetString (resultArray);
}

Many thanks to Rodrigo Barros for the demo code.