Data encryption helps keep your information safe by turning it into a secret code. This way, only people with the right key can read it.
0
0
Data encryption basics in Unity
Introduction
When you want to protect user passwords in your game.
When sending player data over the internet to prevent hackers from reading it.
When saving sensitive information like scores or personal details on a device.
When you want to make sure saved game files cannot be easily changed or read by others.
Syntax
Unity
using System.Security.Cryptography;
using System.Text;
public static string Encrypt(string plainText, string key)
{
byte[] keyBytes = Encoding.UTF8.GetBytes(key);
using Aes aes = Aes.Create();
aes.Key = keyBytes;
aes.GenerateIV();
using var encryptor = aes.CreateEncryptor(aes.Key, aes.IV);
byte[] plainBytes = Encoding.UTF8.GetBytes(plainText);
byte[] encryptedBytes = encryptor.TransformFinalBlock(plainBytes, 0, plainBytes.Length);
byte[] result = new byte[aes.IV.Length + encryptedBytes.Length];
aes.IV.CopyTo(result, 0);
encryptedBytes.CopyTo(result, aes.IV.Length);
return Convert.ToBase64String(result);
}This example uses AES encryption, a common and strong method.
The key must be a specific length (like 16, 24, or 32 bytes) for AES to work properly.
Examples
This example encrypts the message "Hello World!" using a 16-character key.
Unity
string secretKey = "1234567890123456"; string message = "Hello World!"; string encrypted = Encrypt(message, secretKey); Console.WriteLine(encrypted);
Encrypting a player's score before saving it to keep it safe.
Unity
string secretKey = "mysecretkey12345"; string message = "PlayerScore=1000"; string encrypted = Encrypt(message, secretKey); Console.WriteLine(encrypted);
Sample Program
This program encrypts a simple save data string using AES encryption and prints the encrypted text.
Unity
using System; using System.Security.Cryptography; using System.Text; class Program { public static string Encrypt(string plainText, string key) { byte[] keyBytes = Encoding.UTF8.GetBytes(key); using Aes aes = Aes.Create(); aes.Key = keyBytes; aes.GenerateIV(); using var encryptor = aes.CreateEncryptor(aes.Key, aes.IV); byte[] plainBytes = Encoding.UTF8.GetBytes(plainText); byte[] encryptedBytes = encryptor.TransformFinalBlock(plainBytes, 0, plainBytes.Length); byte[] result = new byte[aes.IV.Length + encryptedBytes.Length]; aes.IV.CopyTo(result, 0); encryptedBytes.CopyTo(result, aes.IV.Length); return Convert.ToBase64String(result); } static void Main() { string secretKey = "1234567890123456"; // 16 chars for AES-128 string message = "SaveData=Player1;Score=500"; string encrypted = Encrypt(message, secretKey); Console.WriteLine("Encrypted data:"); Console.WriteLine(encrypted); } }
OutputSuccess
Important Notes
The encrypted output changes each time because a new random IV (initialization vector) is created.
Always keep your encryption key secret and never hard-code it in real apps.
Decryption requires the same key and the IV used during encryption.
Summary
Encryption turns readable data into secret code to protect it.
AES is a common way to encrypt data in Unity using C#.
Keep your keys safe and use encryption when handling sensitive information.