📖 Scenario: You are building a simple Unity game that needs to save player scores securely. To keep scores safe, you will encrypt the score data before saving it.
🎯 Goal: Create a Unity script that encrypts and decrypts a player's score using a basic encryption method.
📋 What You'll Learn
Create an integer variable called playerScore with the value 100
Create a string variable called encryptionKey with the value "key123"
Write a method called EncryptScore that takes playerScore and encryptionKey and returns an encrypted string
Write a method called DecryptScore that takes the encrypted string and encryptionKey and returns the original score as an integer
💡 Why This Matters
🌍 Real World
Encrypting player data helps protect game progress and scores from cheating or tampering.
💼 Career
Understanding basic encryption is useful for game developers and software engineers to secure sensitive data.
Progress0 / 4 steps
1
Set up player score variable
Create an integer variable called playerScore and set it to 100 inside a Unity MonoBehaviour class.
Unity
Hint
Use int playerScore = 100; inside the class but outside any method.
2
Add encryption key variable
Add a string variable called encryptionKey and set it to "key123" inside the ScoreEncryption class.
Unity
Hint
Use string encryptionKey = "key123"; inside the class but outside any method.
3
Create EncryptScore method
Write a method called EncryptScore that takes an integer score and a string key, and returns a string. Use a simple encryption by converting the score to a string and appending the key.
Unity
Hint
Define EncryptScore to return score.ToString() + key.
4
Create DecryptScore method
Write a method called DecryptScore that takes a string encrypted and a string key, removes the key from the end of encrypted, and returns the original score as an integer.
Unity
Hint
Use Substring to remove the key and int.Parse to convert back to integer.
Practice
(1/5)
1. What is the main purpose of encryption in Unity programming?
easy
A. To create graphics effects
B. To convert readable data into a secret code to protect it
C. To make the game run faster
D. To store data in plain text files
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 B
Quick Check:
Encryption = data protection [OK]
Hint: Encryption means hiding data by coding it [OK]
Common Mistakes:
Thinking encryption makes the game faster
Confusing encryption with graphics or speed
Believing encryption stores data as plain text
2. Which of the following is the correct way to declare an AES encryption object in Unity C#?
easy
A. Aes aes = Create.Aes();
B. aes = new AES();
C. Aes aes = Aes.Create();
D. Aes aes = new Aes();
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 C
Quick Check:
Aes.Create() creates AES object [OK]
Hint: Use Aes.Create() to make AES object [OK]
Common Mistakes:
Trying to use 'new AES()' which is invalid
Using wrong capitalization like 'AES' instead of 'Aes'
Calling non-existent methods like Create.Aes()
3. Consider this Unity C# code snippet for AES encryption:
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?
medium
A. An error because of wrong key size
B. The original string 'HelloWorld'
C. A byte array of the encrypted data
D. A base64 encoded string representing encrypted data
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 D
Quick Check:
Encrypted bytes -> base64 string [OK]
Hint: Encrypted bytes are shown as base64 strings [OK]
Common Mistakes:
Thinking output is original text
Expecting byte array instead of string
Assuming key size error without checking length
4. This Unity C# code tries to decrypt data but throws an exception: