public SteamAuth.SteamGuardAccount[] GetAllAccounts(string passKey = null)
        {
            if (passKey == null && this.Encrypted)
            {
                return(new SteamGuardAccount[0]);
            }
            string maDir = Manifest.GetExecutableDir() + "/maFiles/";

            List <SteamAuth.SteamGuardAccount> accounts = new List <SteamAuth.SteamGuardAccount>();

            foreach (var entry in this.Entries)
            {
                string fileText = File.ReadAllText(maDir + entry.Filename);
                if (this.Encrypted)
                {
                    string decryptedText = FileEncryptor.DecryptData(passKey, entry.Salt, entry.IV, fileText);
                    if (decryptedText == null)
                    {
                        return(new SteamGuardAccount[0]);
                    }
                    fileText = decryptedText;
                }

                var account = JsonConvert.DeserializeObject <SteamAuth.SteamGuardAccount>(fileText);
                if (account == null)
                {
                    continue;
                }
                accounts.Add(account);
            }

            return(accounts.ToArray());
        }
        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 SteamAuth.SteamGuardAccount[] GetAllAccounts(string passKey = null, int limit = -1)
        {
            if (passKey == null && this.Encrypted)
            {
                return(new SteamGuardAccount[0]);
            }

            List <SteamAuth.SteamGuardAccount> accounts = new List <SteamAuth.SteamGuardAccount>();

            foreach (var entry in this.Entries)
            {
                string fileText = File.ReadAllText(MaDir + entry.Filename);
                if (this.Encrypted)
                {
                    string decryptedText = FileEncryptor.DecryptData(passKey, entry.Salt, entry.IV, fileText);
                    if (decryptedText == null)
                    {
                        return(new SteamGuardAccount[0]);
                    }
                    fileText = decryptedText;
                }

                var account = JsonConvert.DeserializeObject <SteamAuth.SteamGuardAccount>(fileText);
                if (account == null)
                {
                    continue;
                }
                accounts.Add(account);

                if (limit != -1 && limit >= accounts.Count)
                {
                    break;
                }
            }

            return(accounts.ToArray());
        }
        private void btnImport_Click(object sender, EventArgs e)
        {
            // check if data already added is encripted
            #region check if data already added is encripted
            string ContiuneImport = "0";

            string ManifestFile = "maFiles/manifest.json";
            if (File.Exists(ManifestFile))
            {
                string      AppManifestContents       = File.ReadAllText(ManifestFile);
                AppManifest AppManifestData           = JsonConvert.DeserializeObject <AppManifest>(AppManifestContents);
                bool        AppManifestData_encrypted = AppManifestData.Encrypted;
                if (AppManifestData_encrypted == true)
                {
                    MessageBox.Show("You can't import an .maFile because the existing account in the app is encrypted.\nDecrypt it and try again.");
                    this.Close();
                }
                else if (AppManifestData_encrypted == false)
                {
                    ContiuneImport = "1";
                }
                else
                {
                    MessageBox.Show("invalid value for variable 'encrypted' inside manifest.json");
                    this.Close();
                }
            }
            else
            {
                MessageBox.Show("An Error occurred, Restart the program!");
            }
            #endregion

            // Continue
            #region Continue
            if (ContiuneImport == "1")
            {
                this.Close();

                // read EncriptionKey from imput box
                string ImportUsingEncriptionKey = txtBox.Text;

                // Open file browser > to select the file
                OpenFileDialog openFileDialog1 = new OpenFileDialog();

                // Set filter options and filter index.
                openFileDialog1.Filter      = "maFiles (.maFile)|*.maFile|All Files (*.*)|*.*";
                openFileDialog1.FilterIndex = 1;
                openFileDialog1.Multiselect = false;

                // Call the ShowDialog method to show the dialog box.
                DialogResult userClickedOK = openFileDialog1.ShowDialog();

                // Process input if the user clicked OK.
                if (userClickedOK == DialogResult.OK)
                {
                    // Open the selected file to read.
                    System.IO.Stream fileStream   = openFileDialog1.OpenFile();
                    string           fileContents = null;

                    using (System.IO.StreamReader reader = new System.IO.StreamReader(fileStream))
                    {
                        fileContents = reader.ReadToEnd();
                    }
                    fileStream.Close();

                    try
                    {
                        if (ImportUsingEncriptionKey == "")
                        {
                            // Import maFile
                            //-------------------------------------------
                            #region Import maFile
                            SteamGuardAccount maFile = JsonConvert.DeserializeObject <SteamGuardAccount>(fileContents);
                            if (maFile.Session.SteamID != 0)
                            {
                                mManifest.SaveAccount(maFile, false);
                                MessageBox.Show("Account Imported!");
                            }
                            else
                            {
                                throw new Exception("Invalid SteamID");
                            }
                            #endregion
                        }
                        else
                        {
                            // Import Encripted maFile
                            //-------------------------------------------
                            #region Import Encripted maFile
                            //Read manifest.json encryption_iv encryption_salt
                            string ImportFileName_Found = "0";
                            string Salt_Found           = null;
                            string IV_Found             = null;
                            string ReadManifestEx       = "0";

                            //No directory means no manifest file anyways.
                            ImportManifest newImportManifest = new ImportManifest();
                            newImportManifest.Encrypted = false;
                            newImportManifest.Entries   = new List <ImportManifestEntry>();

                            // extract folder path
                            string fullPath = openFileDialog1.FileName;
                            string fileName = openFileDialog1.SafeFileName;
                            string path     = fullPath.Replace(fileName, "");

                            // extract fileName
                            string ImportFileName = fullPath.Replace(path, "");

                            string ImportManifestFile = path + "manifest.json";


                            if (File.Exists(ImportManifestFile))
                            {
                                string ImportManifestContents = File.ReadAllText(ImportManifestFile);


                                try
                                {
                                    ImportManifest account = JsonConvert.DeserializeObject <ImportManifest>(ImportManifestContents);
                                    //bool Import_encrypted = account.Encrypted;

                                    List <ImportManifest> newEntries = new List <ImportManifest>();

                                    foreach (var entry in account.Entries)
                                    {
                                        string FileName        = entry.Filename;
                                        string encryption_iv   = entry.IV;
                                        string encryption_salt = entry.Salt;

                                        if (ImportFileName == FileName)
                                        {
                                            ImportFileName_Found = "1";
                                            IV_Found             = entry.IV;
                                            Salt_Found           = entry.Salt;
                                        }
                                    }
                                }
                                catch (Exception)
                                {
                                    ReadManifestEx = "1";
                                    MessageBox.Show("Invalid content inside manifest.json!\nImport Failed.");
                                }


                                // DECRIPT & Import
                                //--------------------
                                #region DECRIPT & Import
                                if (ReadManifestEx == "0")
                                {
                                    if (ImportFileName_Found == "1" && Salt_Found != null && IV_Found != null)
                                    {
                                        string decryptedText = FileEncryptor.DecryptData(ImportUsingEncriptionKey, Salt_Found, IV_Found, fileContents);

                                        if (decryptedText == null)
                                        {
                                            MessageBox.Show("Decryption Failed.\nImport Failed.");
                                        }
                                        else
                                        {
                                            string fileText = decryptedText;

                                            SteamGuardAccount maFile = JsonConvert.DeserializeObject <SteamGuardAccount>(fileText);
                                            if (maFile.Session.SteamID != 0)
                                            {
                                                mManifest.SaveAccount(maFile, false);
                                                MessageBox.Show("Account Imported!\nYour Account in now Decrypted!");
                                                //MainForm.loadAccountsList();
                                            }
                                            else
                                            {
                                                MessageBox.Show("Invalid SteamID.\nImport Failed.");
                                            }
                                        }
                                    }
                                    else
                                    {
                                        if (ImportFileName_Found == "0")
                                        {
                                            MessageBox.Show("Account not found inside manifest.json.\nImport Failed.");
                                        }
                                        else if (Salt_Found == null && IV_Found == null)
                                        {
                                            MessageBox.Show("manifest.json does not contain encrypted data.\nYour account may be unencrypted!\nImport Failed.");
                                        }
                                        else
                                        {
                                            if (IV_Found == null)
                                            {
                                                MessageBox.Show("manifest.json does not contain: encryption_iv\nImport Failed.");
                                            }
                                            else if (IV_Found == null)
                                            {
                                                MessageBox.Show("manifest.json does not contain: encryption_salt\nImport Failed.");
                                            }
                                        }
                                    }
                                }
                                #endregion     //DECRIPT & Import END
                            }
                            else
                            {
                                MessageBox.Show("manifest.json is missing!\nImport Failed.");
                            }
                            #endregion //Import Encripted maFile END
                        }
                    }
                    catch (Exception)
                    {
                        MessageBox.Show("This file is not a valid SteamAuth maFile.\nImport Failed.");
                    }
                }
            }
            #endregion // Continue End
        }
        private void btnImport_Click(object sender, EventArgs e)
        {
            // check if data already added is encripted
            #region check if data already added is encripted
            string ContiuneImport = "0";

            string ManifestFile = "maFiles/manifest.json";
            if (File.Exists(ManifestFile))
            {
                string      AppManifestContents       = File.ReadAllText(ManifestFile);
                AppManifest AppManifestData           = JsonConvert.DeserializeObject <AppManifest>(AppManifestContents);
                bool        AppManifestData_encrypted = AppManifestData.Encrypted;
                if (AppManifestData_encrypted == true)
                {
                    MessageBox.Show("您无法导入.maFile文件,因为应用程序中的现有帐户是加密的。解密.maFile文件,然后重试。");
                    this.Close();
                }
                else if (AppManifestData_encrypted == false)
                {
                    ContiuneImport = "1";
                }
                else
                {
                    MessageBox.Show("manifest.json文件中变量'encrypted'的值无效");
                    this.Close();
                }
            }
            else
            {
                MessageBox.Show("出现错误,请重新启动程序!");
            }
            #endregion

            // Continue
            #region Continue
            if (ContiuneImport == "1")
            {
                this.Close();

                // read EncriptionKey from imput box
                string ImportUsingEncriptionKey = txtBox.Text;

                // Open file browser > to select the file
                OpenFileDialog openFileDialog1 = new OpenFileDialog();

                // Set filter options and filter index.
                openFileDialog1.Filter      = "maFiles (.maFile)|*.maFile|All Files (*.*)|*.*";
                openFileDialog1.FilterIndex = 1;
                openFileDialog1.Multiselect = false;

                // Call the ShowDialog method to show the dialog box.
                DialogResult userClickedOK = openFileDialog1.ShowDialog();

                // Process input if the user clicked OK.
                if (userClickedOK == DialogResult.OK)
                {
                    // Open the selected file to read.
                    System.IO.Stream fileStream   = openFileDialog1.OpenFile();
                    string           fileContents = null;

                    using (System.IO.StreamReader reader = new System.IO.StreamReader(fileStream))
                    {
                        fileContents = reader.ReadToEnd();
                    }
                    fileStream.Close();

                    try
                    {
                        if (ImportUsingEncriptionKey == "")
                        {
                            // Import maFile
                            //-------------------------------------------
                            #region Import maFile
                            SteamGuardAccount maFile = JsonConvert.DeserializeObject <SteamGuardAccount>(fileContents);
                            if (maFile.Session.SteamID != 0)
                            {
                                mManifest.SaveAccount(maFile, false);
                                MessageBox.Show("账号已导入!");
                            }
                            else
                            {
                                throw new Exception("Invalid SteamID");
                            }
                            #endregion
                        }
                        else
                        {
                            // Import Encripted maFile
                            //-------------------------------------------
                            #region Import Encripted maFile
                            //Read manifest.json encryption_iv encryption_salt
                            string ImportFileName_Found = "0";
                            string Salt_Found           = null;
                            string IV_Found             = null;
                            string ReadManifestEx       = "0";

                            //No directory means no manifest file anyways.
                            ImportManifest newImportManifest = new ImportManifest();
                            newImportManifest.Encrypted = false;
                            newImportManifest.Entries   = new List <ImportManifestEntry>();

                            // extract folder path
                            string fullPath = openFileDialog1.FileName;
                            string fileName = openFileDialog1.SafeFileName;
                            string path     = fullPath.Replace(fileName, "");

                            // extract fileName
                            string ImportFileName = fullPath.Replace(path, "");

                            string ImportManifestFile = path + "manifest.json";


                            if (File.Exists(ImportManifestFile))
                            {
                                string ImportManifestContents = File.ReadAllText(ImportManifestFile);


                                try
                                {
                                    ImportManifest account = JsonConvert.DeserializeObject <ImportManifest>(ImportManifestContents);
                                    //bool Import_encrypted = account.Encrypted;

                                    List <ImportManifest> newEntries = new List <ImportManifest>();

                                    foreach (var entry in account.Entries)
                                    {
                                        string FileName        = entry.Filename;
                                        string encryption_iv   = entry.IV;
                                        string encryption_salt = entry.Salt;

                                        if (ImportFileName == FileName)
                                        {
                                            ImportFileName_Found = "1";
                                            IV_Found             = entry.IV;
                                            Salt_Found           = entry.Salt;
                                        }
                                    }
                                }
                                catch (Exception)
                                {
                                    ReadManifestEx = "1";
                                    MessageBox.Show("manifest.json文件中的内容无效!导入失败。");
                                }


                                // DECRIPT & Import
                                //--------------------
                                #region DECRIPT & Import
                                if (ReadManifestEx == "0")
                                {
                                    if (ImportFileName_Found == "1" && Salt_Found != null && IV_Found != null)
                                    {
                                        string decryptedText = FileEncryptor.DecryptData(ImportUsingEncriptionKey, Salt_Found, IV_Found, fileContents);

                                        if (decryptedText == null)
                                        {
                                            MessageBox.Show("解密失败.\n导入失败.");
                                        }
                                        else
                                        {
                                            string fileText = decryptedText;

                                            SteamGuardAccount maFile = JsonConvert.DeserializeObject <SteamGuardAccount>(fileText);
                                            if (maFile.Session.SteamID != 0)
                                            {
                                                mManifest.SaveAccount(maFile, false);
                                                MessageBox.Show("账号已导入!\n正在解密中!");
                                                //MainForm.loadAccountsList();
                                            }
                                            else
                                            {
                                                MessageBox.Show("无效的Steam ID.\n导入失败.");
                                            }
                                        }
                                    }
                                    else
                                    {
                                        if (ImportFileName_Found == "0")
                                        {
                                            MessageBox.Show("manifest.json 中未找到账号信息.\n导入失败.");
                                        }
                                        else if (Salt_Found == null && IV_Found == null)
                                        {
                                            MessageBox.Show("manifest.json中未包含加密数据.\n您的账号可能被破解!\n导入失败.");
                                        }
                                        else
                                        {
                                            if (IV_Found == null)
                                            {
                                                MessageBox.Show("manifest.json 未包含数据段: encryption_iv\n导入失败.");
                                            }
                                            else if (IV_Found == null)
                                            {
                                                MessageBox.Show("manifest.json 未包含数据段: encryption_salt\nn导入失败.");
                                            }
                                        }
                                    }
                                }
                                #endregion //DECRIPT & Import END
                            }
                            else
                            {
                                MessageBox.Show("manifest.json is json文件丢失!导入失败。");
                            }
                            #endregion //Import Encripted maFile END
                        }
                    }
                    catch (Exception)
                    {
                        MessageBox.Show("此文件不是有效的SteamAuth Mafile。导入失败。");
                    }
                }
            }
            #endregion // Continue End
        }