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.
Data encryption basics in 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.
string secretKey = "1234567890123456"; string message = "Hello World!"; string encrypted = Encrypt(message, secretKey); Console.WriteLine(encrypted);
string secretKey = "mysecretkey12345"; string message = "PlayerScore=1000"; string encrypted = Encrypt(message, secretKey); Console.WriteLine(encrypted);
This program encrypts a simple save data string using AES encryption and prints the encrypted text.
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); } }
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.
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.
Practice
Solution
Step 1: Understand what encryption does
Encryption changes readable data into a secret code so others cannot understand it easily.Step 2: Identify the purpose in Unity
In Unity, encryption is used to protect sensitive information like passwords or personal data.Final Answer:
To convert readable data into a secret code to protect it -> Option BQuick Check:
Encryption = data protection [OK]
- Thinking encryption makes the game faster
- Confusing encryption with graphics or speed
- Believing encryption stores data as plain text
Solution
Step 1: Recall AES object creation in C#
In C#, AES encryption objects are created using the static method Aes.Create().Step 2: Check syntax correctness
Aes aes = Aes.Create(); uses correct syntax: Aes aes = Aes.Create(); Options A, B, and C use incorrect constructors or methods.Final Answer:
Aes aes = Aes.Create(); -> Option CQuick Check:
Aes.Create() creates AES object [OK]
- Trying to use 'new AES()' which is invalid
- Using wrong capitalization like 'AES' instead of 'Aes'
- Calling non-existent methods like Create.Aes()
using System.Security.Cryptography;
using System.Text;
Aes aes = Aes.Create();
aes.Key = Encoding.UTF8.GetBytes("1234567890123456");
aes.IV = Encoding.UTF8.GetBytes("abcdefghijklmnop");
byte[] encrypted;
using (var encryptor = aes.CreateEncryptor())
{
byte[] input = Encoding.UTF8.GetBytes("HelloWorld");
encrypted = encryptor.TransformFinalBlock(input, 0, input.Length);
}
string result = Convert.ToBase64String(encrypted);
Console.WriteLine(result);
What will be the output type of result?Solution
Step 1: Understand encryption output
The encrypted data is a byte array, which is then converted to a base64 string for easy display.Step 2: Analyze the code's final output
The code uses Convert.ToBase64String(encrypted) to get a string representation of encrypted bytes, so result is a base64 string.Final Answer:
A base64 encoded string representing encrypted data -> Option DQuick Check:
Encrypted bytes -> base64 string [OK]
- Thinking output is original text
- Expecting byte array instead of string
- Assuming key size error without checking length
byte[] encryptedData = ...;
Aes aes = Aes.Create();
aes.Key = Encoding.UTF8.GetBytes("shortkey");
aes.IV = Encoding.UTF8.GetBytes("shortiv");
using (var decryptor = aes.CreateDecryptor())
{
byte[] decrypted = decryptor.TransformFinalBlock(encryptedData, 0, encryptedData.Length);
}
What is the main problem causing the error?Solution
Step 1: Check key and IV sizes for AES
AES requires keys and IVs of specific lengths (e.g., 16 bytes for AES-128). The given strings "shortkey" and "shortiv" are too short.Step 2: Understand error cause
Using keys or IVs with incorrect length causes exceptions during encryption/decryption.Final Answer:
The key and IV sizes are too short for AES -> Option AQuick Check:
Key/IV length must match AES requirements [OK]
- Assuming encryptedData is empty without checking
- Thinking CreateDecryptor needs password parameter
- Believing TransformFinalBlock accepts strings directly
Solution
Step 1: Understand secure password storage
Passwords should never be stored as plain text. Encryption with AES is good, but IV must be unique per encryption to prevent attacks.Step 2: Evaluate options for key and IV usage
Using a unique IV per user with AES encryption improves security. Reusing IV or fixed keys weakens protection.Final Answer:
Encrypt passwords with AES and store the encrypted string with a unique IV per user -> Option AQuick Check:
Unique IV + AES = secure password storage [OK]
- Storing passwords as plain text
- Reusing IVs for all encryptions
- Using fixed keys for all users
Ready to prove it?
Pass the test and this pattern moves unknown
