Bird
Raised Fist0
Unityframework~20 mins

Data encryption basics in Unity - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Data Encryption Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of AES encryption snippet
What is the output of this Unity C# code snippet that encrypts a string using AES and prints the Base64 result?
Unity
using System;
using System.Security.Cryptography;
using System.Text;

public class EncryptTest {
    public static void Main() {
        string original = "hello";
        byte[] key = Encoding.UTF8.GetBytes("1234567890123456");
        byte[] iv = Encoding.UTF8.GetBytes("abcdefghijklmnop");

        using (Aes aes = Aes.Create()) {
            aes.Key = key;
            aes.IV = iv;
            ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV);

            byte[] originalBytes = Encoding.UTF8.GetBytes(original);
            byte[] encrypted = encryptor.TransformFinalBlock(originalBytes, 0, originalBytes.Length);

            Console.WriteLine(Convert.ToBase64String(encrypted));
        }
    }
}
AU2FsdGVkX1+q7Q9r6XzQ7v6v6Zq5zqX1w==
Bq7Q9r6XzQ7v6v6Zq5zqX1w==
CSyntaxError: missing semicolon
Dhello
Attempts:
2 left
💡 Hint
Look at how AES encryption transforms the input bytes and outputs a Base64 string.
🧠 Conceptual
intermediate
1:30remaining
Understanding symmetric vs asymmetric encryption
Which statement correctly describes the difference between symmetric and asymmetric encryption?
ASymmetric encryption uses one key for both encrypting and decrypting, while asymmetric uses a pair of keys.
BSymmetric encryption uses two keys, asymmetric uses one key only.
CSymmetric encryption is slower than asymmetric encryption.
DAsymmetric encryption cannot be used for data encryption, only for hashing.
Attempts:
2 left
💡 Hint
Think about how keys are used in each encryption type.
🔧 Debug
advanced
2:00remaining
Identify the error in this encryption code
What error will this Unity C# code produce when run?
Unity
using System;
using System.Security.Cryptography;
using System.Text;

public class EncryptDebug {
    public static void Main() {
        string text = "data";
        byte[] key = Encoding.UTF8.GetBytes("shortkey");
        byte[] iv = Encoding.UTF8.GetBytes("shortiv");

        using (Aes aes = Aes.Create()) {
            aes.Key = key;
            aes.IV = iv;
            ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV);

            byte[] bytes = Encoding.UTF8.GetBytes(text);
            byte[] encrypted = encryptor.TransformFinalBlock(bytes, 0, bytes.Length);

            Console.WriteLine(Convert.ToBase64String(encrypted));
        }
    }
}
AOutput: "ZGF0YQ=="
BNullReferenceException because aes is null
CCryptographicException due to invalid key size
DIndexOutOfRangeException during TransformFinalBlock
Attempts:
2 left
💡 Hint
Check the length of the key and IV for AES requirements.
📝 Syntax
advanced
2:00remaining
Which code snippet correctly decrypts AES data?
Which of the following Unity C# code snippets correctly decrypts AES encrypted data stored in a byte array named encryptedData?
A
using (Aes aes = Aes.Create()) {
    aes.Key = key;
    aes.IV = iv;
    ICryptoTransform decryptor = aes.CreateDecryptor(aes.IV, aes.Key);
    byte[] decrypted = decryptor.TransformFinalBlock(encryptedData, 0, encryptedData.Length);
    string result = Encoding.UTF8.GetString(decrypted);
    Console.WriteLine(result);
}
B
using (Aes aes = Aes.Create()) {
    aes.Key = key;
    aes.IV = iv;
    ICryptoTransform decryptor = aes.CreateEncryptor(aes.Key, aes.IV);
    byte[] decrypted = decryptor.TransformFinalBlock(encryptedData, 0, encryptedData.Length);
    string result = Encoding.UTF8.GetString(decrypted);
    Console.WriteLine(result);
}
C
using (Aes aes = Aes.Create()) {
    aes.Key = key;
    aes.IV = iv;
    ICryptoTransform decryptor = aes.CreateDecryptor();
    byte[] decrypted = decryptor.TransformFinalBlock(encryptedData, 0, encryptedData.Length);
    string result = Encoding.UTF8.GetString(decrypted);
    Console.WriteLine(result);
}
D
using (Aes aes = Aes.Create()) {
    aes.Key = key;
    aes.IV = iv;
    ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);
    byte[] decrypted = decryptor.TransformFinalBlock(encryptedData, 0, encryptedData.Length);
    string result = Encoding.UTF8.GetString(decrypted);
    Console.WriteLine(result);
}
Attempts:
2 left
💡 Hint
Check the method used to create the decryptor and the order of parameters.
🚀 Application
expert
2:30remaining
Calculate the length of encrypted data
Given a plaintext string of length 30 bytes encrypted with AES in CBC mode with PKCS7 padding, what is the length in bytes of the encrypted output?
A32 bytes
B30 bytes
C48 bytes
D16 bytes
Attempts:
2 left
💡 Hint
AES block size is 16 bytes. PKCS7 padding adds bytes to fill the last block.

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

  1. Step 1: Understand what encryption does

    Encryption changes readable data into a secret code so others cannot understand it easily.
  2. Step 2: Identify the purpose in Unity

    In Unity, encryption is used to protect sensitive information like passwords or personal data.
  3. Final Answer:

    To convert readable data into a secret code to protect it -> Option B
  4. 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

  1. Step 1: Recall AES object creation in C#

    In C#, AES encryption objects are created using the static method Aes.Create().
  2. 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.
  3. Final Answer:

    Aes aes = Aes.Create(); -> Option C
  4. 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

  1. Step 1: Understand encryption output

    The encrypted data is a byte array, which is then converted to a base64 string for easy display.
  2. 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.
  3. Final Answer:

    A base64 encoded string representing encrypted data -> Option D
  4. 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:
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?
medium
A. The key and IV sizes are too short for AES
B. The encryptedData array is empty
C. CreateDecryptor() cannot be used without a password
D. TransformFinalBlock requires a string input

Solution

  1. 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.
  2. Step 2: Understand error cause

    Using keys or IVs with incorrect length causes exceptions during encryption/decryption.
  3. Final Answer:

    The key and IV sizes are too short for AES -> Option A
  4. Quick Check:

    Key/IV length must match AES requirements [OK]
Hint: AES keys and IVs must be exact length [OK]
Common Mistakes:
  • Assuming encryptedData is empty without checking
  • Thinking CreateDecryptor needs password parameter
  • Believing TransformFinalBlock accepts strings directly
5. You want to securely store user passwords in Unity using AES encryption. Which approach is best practice?
hard
A. Encrypt passwords with AES and store the encrypted string with a unique IV per user
B. Store passwords as plain text for easy retrieval
C. Use a fixed key and IV for all users to simplify encryption
D. Encrypt passwords with AES but reuse the same IV for all users

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    Encrypt passwords with AES and store the encrypted string with a unique IV per user -> Option A
  4. Quick Check:

    Unique IV + AES = secure password storage [OK]
Hint: Always use unique IVs with AES encryption [OK]
Common Mistakes:
  • Storing passwords as plain text
  • Reusing IVs for all encryptions
  • Using fixed keys for all users
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard