public bool ChangeEncryptionKey(string oldKey, string newKey)
        {
            if (this.Encrypted)
            {
                if (!this.VerifyPasskey(oldKey))
                {
                    return(false);
                }
            }
            bool toEncrypt = newKey != null;

            string maDir = Manifest.GetExecutableDir() + "/maFiles/";

            for (int i = 0; i < this.Entries.Count; i++)
            {
                ManifestEntry entry    = this.Entries[i];
                string        filename = maDir + entry.Filename;
                if (!File.Exists(filename))
                {
                    continue;
                }

                string fileContents = File.ReadAllText(filename);
                if (this.Encrypted)
                {
                    fileContents = FileEncryptor.DecryptData(oldKey, entry.Salt, entry.IV, fileContents);
                }

                string newSalt             = null;
                string newIV               = null;
                string toWriteFileContents = fileContents;

                if (toEncrypt)
                {
                    newSalt             = FileEncryptor.GetRandomSalt();
                    newIV               = FileEncryptor.GetInitializationVector();
                    toWriteFileContents = FileEncryptor.EncryptData(newKey, newSalt, newIV, fileContents);
                }

                File.WriteAllText(filename, toWriteFileContents);
                entry.IV   = newIV;
                entry.Salt = newSalt;
            }

            this.Encrypted = toEncrypt;

            this.Save();
            return(true);
        }
        public bool SaveAccount(SteamGuardAccount account, bool encrypt, string passKey = null)
        {
            if (encrypt && String.IsNullOrEmpty(passKey))
            {
                return(false);
            }
            if (!encrypt && this.Encrypted)
            {
                return(false);
            }

            string salt        = null;
            string iV          = null;
            string jsonAccount = JsonConvert.SerializeObject(account);

            if (encrypt)
            {
                salt = FileEncryptor.GetRandomSalt();
                iV   = FileEncryptor.GetInitializationVector();
                string encrypted = FileEncryptor.EncryptData(passKey, salt, iV, jsonAccount);
                if (encrypted == null)
                {
                    return(false);
                }
                jsonAccount = encrypted;
            }

            string maDir    = Manifest.GetExecutableDir() + "/maFiles/";
            string filename = account.Session.SteamID.ToString() + ".maFile";

            ManifestEntry newEntry = new ManifestEntry()
            {
                SteamID  = account.Session.SteamID,
                IV       = iV,
                Salt     = salt,
                Filename = filename
            };

            bool foundExistingEntry = false;

            for (int i = 0; i < this.Entries.Count; i++)
            {
                if (this.Entries[i].SteamID == account.Session.SteamID)
                {
                    this.Entries[i]    = newEntry;
                    foundExistingEntry = true;
                    break;
                }
            }

            if (!foundExistingEntry)
            {
                this.Entries.Add(newEntry);
            }

            bool wasEncrypted = this.Encrypted;

            this.Encrypted = encrypt || this.Encrypted;

            if (!this.Save())
            {
                this.Encrypted = wasEncrypted;
                return(false);
            }

            try
            {
                File.WriteAllText(maDir + filename, jsonAccount);
                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }