Пример #1
0
        public static string DecryptFile(string filename, string encryptedContent)
        {
            if (encryptedContent == null || encryptedContent.Length == 0)
            {
                return(null);
            }
            string encryptedString = EncryptedPlayerPrefs.GetEncryptedString("__shhh__" + filename);

            if (encryptedString != null && encryptedString.Length > 0)
            {
                return(AesCipher.Decrypt(encryptedContent, encryptedString));
            }
            return(null);
        }
        protected virtual void LoadOrGenerateSecondLevel(string secondaryPrefix, int maxSecondLevelKeys)
        {
            string masterKeyName = GetMasterKeyName();

            m_internalKeys = new Dictionary <string, Type>();
            m_internalKeys[masterKeyName] = s_intType;
            if (PlayerPrefs.HasKey(masterKeyName))
            {
                int            @int      = PlayerPrefs.GetInt(masterKeyName);
                string         masterKey = GetMasterKey(@int);
                IList <string> list      = new List <string>();
                try
                {
                    for (int i = 0; i < maxSecondLevelKeys; i++)
                    {
                        string encryptedKey = GetEncryptedKey(secondaryPrefix + (i + 1));
                        m_internalKeys[encryptedKey] = s_stringType;
                        string @string = PlayerPrefs.GetString(encryptedKey);
                        string item    = AesCipher.Decrypt(@string, masterKey);
                        list.Add(item);
                    }
                    m_secondLevel = new string[list.Count];
                    list.CopyTo(m_secondLevel, 0);
                }
                catch (ArgumentNullException)
                {
                    m_secondLevel = null;
                }
            }
            if (m_secondLevel == null)
            {
                System.Random random = new System.Random();
                int           @int   = random.Next();
                PlayerPrefs.SetInt(masterKeyName, @int);
                string masterKey = GetMasterKey(@int);
                m_secondLevel = new string[maxSecondLevelKeys];
                for (int i = 0; i < m_secondLevel.Length; i++)
                {
                    string encryptedKey = GetEncryptedKey(secondaryPrefix + (i + 1));
                    m_internalKeys[encryptedKey] = s_stringType;
                    m_secondLevel[i]             = Guid.NewGuid().ToString();
                    string @string = AesCipher.Encrypt(m_secondLevel[i], masterKey);
                    PlayerPrefs.SetString(encryptedKey, @string);
                }
                PlayerPrefs.Save();
            }
        }
        protected string GetDecryptedValue(string prefsKey)
        {
            byte[] keyHash       = GetKeyHash(prefsKey);
            string encryptionKey = GetEncryptionKey(keyHash);
            string encryptedKey  = GetEncryptedKey(keyHash);

            if (!PlayerPrefs.HasKey(encryptedKey))
            {
                return(null);
            }
            string @string = PlayerPrefs.GetString(encryptedKey);

            if (@string == null || @string.Length == 0)
            {
                return(@string);
            }
            return(AesCipher.Decrypt(@string, encryptionKey));
        }
Пример #4
0
        public static string EncryptFile(string filename, string fileContent)
        {
            string text   = Guid.NewGuid().ToString();
            string result = ((fileContent == null || fileContent.Length == 0) ? fileContent : AesCipher.Encrypt(fileContent, text));

            EncryptedPlayerPrefs.SetEncryptedString("__shhh__" + filename, text);
            EncryptedPlayerPrefs.Save();
            return(result);
        }
        protected void SaveValueEncrypted(string prefsKey, string unencryptedValue)
        {
            if (prefsKey == null || prefsKey.Length == 0)
            {
                throw new PlayerPrefsException("Not a valid key");
            }
            byte[] keyHash       = GetKeyHash(prefsKey);
            string encryptedKey  = GetEncryptedKey(keyHash);
            string encryptionKey = GetEncryptionKey(keyHash);
            string value         = ((unencryptedValue == null || unencryptedValue.Length == 0) ? "" : AesCipher.Encrypt(unencryptedValue, encryptionKey));

            if (m_fallbackUnencrypted)
            {
                PlayerPrefs.DeleteKey(prefsKey);
            }
            PlayerPrefs.SetString(encryptedKey, value);
        }