0
0
Unityframework~20 mins

Data encryption basics in Unity - Practice Problems & Coding Challenges

Choose your learning style9 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.