chmodseven

Test PlayerPrefs Encryption

Jan 14th, 2021 (edited)
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.62 KB | None | 0 0
  1. using Tesseraction.Functions;
  2. using UnityEngine;
  3. using UnityEngine.Assertions;
  4.  
  5. public class TestEncryptionPlayerPrefs : MonoBehaviour
  6. {
  7.     public string passwordString = "YourAppPasswordHere";
  8.     public string stringToStoreInPlayerPrefs = "Some string with values you want to encrypt and store in PlayerPrefs";
  9.    
  10.     private void Start()
  11.     {
  12.         NormalUsage ();
  13.         EncryptedUsage ();
  14.     }
  15.  
  16.     private void NormalUsage ()
  17.     {
  18.         // Normal usage set
  19.         PlayerPrefs.SetString ("Unencrypted string", stringToStoreInPlayerPrefs);
  20.  
  21.         // Normal usage get
  22.         string fetchUnencryptedString = PlayerPrefs.GetString ("Unencrypted string");
  23.  
  24.         // Test that get is same as original
  25.         // If an assertion fails it will show as an error in the console
  26.         Assert.AreEqual (fetchUnencryptedString, stringToStoreInPlayerPrefs);
  27.  
  28.         // If all assertions pass, it will show this message in console instead
  29.         Debug.Log ("Normal usage assertions passed successfully.");
  30.     }
  31.    
  32.     private void EncryptedUsage ()
  33.     {
  34.         // Creates the encryption key byte array that AES needs for its parameter of the password
  35.         byte [] encryptionKey = EncryptionFunctions.GeneratePasswordEncryptionKey (passwordString);
  36.        
  37.         // Encrypt the string value using that key and store result in PlayerPrefs
  38.         string encryptedString = EncryptionFunctions.EncryptString (stringToStoreInPlayerPrefs, encryptionKey);
  39.         PlayerPrefs.SetString ("Encrypted string", encryptedString);
  40.         Debug.Log ("Stored original value \"" + stringToStoreInPlayerPrefs + "\"\n as encrypted value \"" + encryptedString + "\" in PlayerPrefs");
  41.  
  42.         // Assert that original and encrypted are different
  43.         Assert.AreNotEqual (encryptedString, stringToStoreInPlayerPrefs);
  44.        
  45.         // Encrypted usage get
  46.         string fetchEncryptedString = PlayerPrefs.GetString ("Encrypted string");
  47.         Assert.AreEqual (fetchEncryptedString, encryptedString);
  48.  
  49.         // Decrypt it using the same key as before
  50.         string decryptedString = EncryptionFunctions.DecryptString (fetchEncryptedString, encryptionKey);
  51.         Debug.Log ("Decrypted value is \"" + decryptedString + "\"");
  52.        
  53.         // Test that decrypted value is same as original
  54.         // If an assertion fails it will show as an error in the console
  55.         Assert.AreEqual (decryptedString, stringToStoreInPlayerPrefs);
  56.        
  57.         // If all assertions pass, it will show this message in console instead
  58.         Debug.Log ("Encrypted usage assertions passed successfully.");
  59.     }
  60. }
  61.  
Add Comment
Please, Sign In to add comment