| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using SteamAuth;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
- using System.IO;
- namespace Steam_Desktop_Authenticator
- {
- public partial class ImportAccountForm : Form
- {
- private Manifest mManifest;
- public ImportAccountForm()
- {
- InitializeComponent();
- this.mManifest = Manifest.GetManifest();
- }
- 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 == null || maFile.Session.SteamID == 0 || maFile.Session.IsAccessTokenExpired())
- {
- // Have the user to relogin to steam to get a new session
- LoginForm loginForm = new LoginForm(LoginForm.LoginType.Import, maFile);
- loginForm.ShowDialog();
- if (loginForm.Session == null || loginForm.Session.SteamID == 0)
- {
- MessageBox.Show("Login failed. Try to import this account again.", "Account Import", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- // Save new session to the maFile
- maFile.Session = loginForm.Session;
- }
- // Save account
- mManifest.SaveAccount(maFile, false);
- MessageBox.Show("Account Imported!", "Account Import", MessageBoxButtons.OK);
- #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 == null || maFile.Session.SteamID == 0 || maFile.Session.IsAccessTokenExpired())
- {
- // Have the user to relogin to steam to get a new session
- LoginForm loginForm = new LoginForm(LoginForm.LoginType.Import, maFile);
- loginForm.ShowDialog();
- if (loginForm.Session == null || loginForm.Session.SteamID == 0)
- {
- MessageBox.Show("Login failed. Try to import this account again.", "Account Import", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- // Save new session to the maFile
- maFile.Session = loginForm.Session;
- }
- // Save account
- mManifest.SaveAccount(maFile, false);
- MessageBox.Show("Account Imported!\nYour Account in now Decrypted!", "Account Import", MessageBoxButtons.OK);
- }
- }
- 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 btnCancel_Click(object sender, EventArgs e)
- {
- this.Close();
- }
- private void Import_maFile_Form_FormClosing(object sender, FormClosingEventArgs e)
- {
- }
- }
- public class AppManifest
- {
- [JsonProperty("encrypted")]
- public bool Encrypted { get; set; }
- }
- public class ImportManifest
- {
- [JsonProperty("encrypted")]
- public bool Encrypted { get; set; }
- [JsonProperty("entries")]
- public List<ImportManifestEntry> Entries { get; set; }
- }
- public class ImportManifestEntry
- {
- [JsonProperty("encryption_iv")]
- public string IV { get; set; }
- [JsonProperty("encryption_salt")]
- public string Salt { get; set; }
- [JsonProperty("filename")]
- public string Filename { get; set; }
- [JsonProperty("steamid")]
- public ulong SteamID { get; set; }
- }
- }
|