Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Tesseraction.Functions;
- using UnityEngine;
- using UnityEngine.Assertions;
- public class TestEncryptionPlayerPrefs : MonoBehaviour
- {
- public string passwordString = "YourAppPasswordHere";
- public string stringToStoreInPlayerPrefs = "Some string with values you want to encrypt and store in PlayerPrefs";
- private void Start()
- {
- NormalUsage ();
- EncryptedUsage ();
- }
- private void NormalUsage ()
- {
- // Normal usage set
- PlayerPrefs.SetString ("Unencrypted string", stringToStoreInPlayerPrefs);
- // Normal usage get
- string fetchUnencryptedString = PlayerPrefs.GetString ("Unencrypted string");
- // Test that get is same as original
- // If an assertion fails it will show as an error in the console
- Assert.AreEqual (fetchUnencryptedString, stringToStoreInPlayerPrefs);
- // If all assertions pass, it will show this message in console instead
- Debug.Log ("Normal usage assertions passed successfully.");
- }
- private void EncryptedUsage ()
- {
- // Creates the encryption key byte array that AES needs for its parameter of the password
- byte [] encryptionKey = EncryptionFunctions.GeneratePasswordEncryptionKey (passwordString);
- // Encrypt the string value using that key and store result in PlayerPrefs
- string encryptedString = EncryptionFunctions.EncryptString (stringToStoreInPlayerPrefs, encryptionKey);
- PlayerPrefs.SetString ("Encrypted string", encryptedString);
- Debug.Log ("Stored original value \"" + stringToStoreInPlayerPrefs + "\"\n as encrypted value \"" + encryptedString + "\" in PlayerPrefs");
- // Assert that original and encrypted are different
- Assert.AreNotEqual (encryptedString, stringToStoreInPlayerPrefs);
- // Encrypted usage get
- string fetchEncryptedString = PlayerPrefs.GetString ("Encrypted string");
- Assert.AreEqual (fetchEncryptedString, encryptedString);
- // Decrypt it using the same key as before
- string decryptedString = EncryptionFunctions.DecryptString (fetchEncryptedString, encryptionKey);
- Debug.Log ("Decrypted value is \"" + decryptedString + "\"");
- // Test that decrypted value is same as original
- // If an assertion fails it will show as an error in the console
- Assert.AreEqual (decryptedString, stringToStoreInPlayerPrefs);
- // If all assertions pass, it will show this message in console instead
- Debug.Log ("Encrypted usage assertions passed successfully.");
- }
- }
Add Comment
Please, Sign In to add comment