/// <summary> /// Encrypted version of PlayerPrefs.SetString(), stored key and value is encrypted in player prefs /// </summary> public static void SetEncryptedString(string key, string value) { string encryptedKey = SimpleEncryption.EncryptString(key); string encryptedValue = SimpleEncryption.EncryptString(value); // Store the encrypted key and value (with relevant identifying prefixes) in PlayerPrefs PlayerPrefs.SetString(KEY_PREFIX + encryptedKey, VALUE_STRING_PREFIX + encryptedValue); }
/// <summary> /// Encrypts the specified int value and returns an encrypted string /// </summary> public static string EncryptInt(int value) { // Convert the int value into its 4 bytes byte[] bytes = BitConverter.GetBytes(value); // Represent those bytes as a base 64 string string base64 = Convert.ToBase64String(bytes); // Return the encrypted version of that base 64 string return(SimpleEncryption.EncryptString(base64)); }
/// <summary> /// Encrypted version of PlayerPrefs.GetString(), an unencrypted key is passed and the value is returned decrypted /// </summary> public static string GetEncryptedString(string key, string defaultValue = "") { // Encrypt and prefix the key so we can look it up from player prefs string encryptedKey = KEY_PREFIX + SimpleEncryption.EncryptString(key); // Look up the encrypted value string fetchedString = PlayerPrefs.GetString(encryptedKey); if (!string.IsNullOrEmpty(fetchedString)) { // Strip out the type identifier character fetchedString = fetchedString.Remove(0, 1); // Decrypt and return the string value return(SimpleEncryption.DecryptString(fetchedString)); } else { // No existing player pref value, so return defaultValue instead return(defaultValue); } }