Beispiel #1
0
 /// <summary>
 /// Helper method that can handle any of the encrypted player pref types, returning a float, int or string based
 /// on what type of value has been stored.
 /// </summary>
 public static object GetEncryptedValue(string encryptedKey, string encryptedValue)
 {
     // See what type identifier the encrypted value starts
     if (encryptedValue.StartsWith(VALUE_FLOAT_PREFIX))
     {
         // It's a float, so decrypt it as a float and return the value
         return(GetEncryptedFloat(SimpleEncryption.DecryptString(encryptedKey.Substring(KEY_PREFIX.Length))));
     }
     else if (encryptedValue.StartsWith(VALUE_INT_PREFIX))
     {
         // It's an int, so decrypt it as an int and return the value
         return(GetEncryptedInt(SimpleEncryption.DecryptString(encryptedKey.Substring(KEY_PREFIX.Length))));
     }
     else if (encryptedValue.StartsWith(VALUE_STRING_PREFIX))
     {
         // It's a string, so decrypt it as a string and return the value
         return(GetEncryptedString(SimpleEncryption.DecryptString(encryptedKey.Substring(KEY_PREFIX.Length))));
     }
     else if (encryptedValue.StartsWith(VALUE_BOOL_PREFIX))
     {
         // It's a string, so decrypt it as a string and return the value
         return(GetEncryptedBool(SimpleEncryption.DecryptString(encryptedKey.Substring(KEY_PREFIX.Length))));
     }
     else
     {
         throw new InvalidOperationException("Could not decrypt item, no match found in known encrypted key prefixes");
     }
 }
Beispiel #2
0
        /// <summary>
        /// Encrypted version of EditorPrefs.SetBool(), stored key and value is encrypted in player prefs
        /// </summary>
        public static void SetEncryptedBool(string key, bool value)
        {
            string encryptedKey   = SimpleEncryption.EncryptString(key);
            string encryptedValue = SimpleEncryption.EncryptBool(value);

            // Store the encrypted key and value (with relevant identifying prefixes) in PlayerPrefs
            PlayerPrefs.SetString(KEY_PREFIX + encryptedKey, VALUE_BOOL_PREFIX + encryptedValue);
        }
        /// <summary>
        /// Decrypts the encrypted string representing a bool into the decrypted bool
        /// </summary>
        public static bool DecryptBool(string sourceString)
        {
            // Decrypt the encrypted string
            string decryptedString = SimpleEncryption.DecryptString(sourceString);

            // Convert the decrypted Base 64 representation back into bytes
            byte[] bytes = Convert.FromBase64String(decryptedString);

            // Turn the bytes back into a bool and return it
            return(BitConverter.ToBoolean(bytes, 0));
        }
        /// <summary>
        /// Decrypts the encrypted string representing a float into the decrypted float
        /// </summary>
        public static float DecryptFloat(string sourceString)
        {
            // Decrypt the encrypted string
            string decryptedString = SimpleEncryption.DecryptString(sourceString);

            // Convert the decrypted Base 64 representation back into bytes
            byte[] bytes = Convert.FromBase64String(decryptedString);

            // Turn the bytes back into a float and return it
            return(BitConverter.ToSingle(bytes, 0));
        }
        /// Encrypts the specified bool value and returns an encrypted string
        /// </summary>
        public static string EncryptBool(bool value)
        {
            // Convert the bool 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));
        }
Beispiel #6
0
 /// <summary>
 /// Decrypts the specified key
 /// </summary>
 public static string DecryptKey(string encryptedKey)
 {
     if (encryptedKey.StartsWith(KEY_PREFIX))
     {
         // Remove the key prefix from the encrypted key
         string strippedKey = encryptedKey.Substring(KEY_PREFIX.Length);
         // Return the decrypted key
         return(SimpleEncryption.DecryptString(strippedKey));
     }
     else
     {
         throw new InvalidOperationException("Could not decrypt item, no match found in known encrypted key prefixes");
     }
 }
Beispiel #7
0
        /// <summary>
        /// Encrypted version of EditorPrefs.GetBool(), an unencrypted key is passed and the value is returned decrypted
        /// </summary>
        public static bool GetEncryptedBool(string key, bool defaultValue = false)
        {
            // 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 int value
                return(SimpleEncryption.DecryptBool(fetchedString));
            }
            else
            {
                // No existing player pref value, so return defaultValue instead
                return(defaultValue);
            }
        }