From 374fe6c7930d565307e4d286fde525e81e35d3ff Mon Sep 17 00:00:00 2001 From: Carson McManus Date: Mon, 22 Aug 2016 15:51:37 -0400 Subject: [PATCH] added encrypted maFile reading, writing added passkey prompt --- Manifest.cs | 55 ++++++++++++++++++++++++++++++++++++++++------------- Program.cs | 10 +++++++++- 2 files changed, 51 insertions(+), 14 deletions(-) diff --git a/Manifest.cs b/Manifest.cs index c1c8459..8651ac8 100644 --- a/Manifest.cs +++ b/Manifest.cs @@ -83,11 +83,6 @@ public class Manifest _manifest.Save(); } - if (_manifest.Encrypted) - { - throw new NotSupportedException("Encrypted maFiles are not supported at this time."); - } - _manifest.RecomputeExistingEntries(); return _manifest; @@ -233,10 +228,22 @@ public class Manifest string fileText = ""; Stream stream = null; FileStream fileStream = File.OpenRead(Path.Combine(Program.SteamGuardPath, entry.Filename)); + RijndaelManaged aes256; if (this.Encrypted) { - //string decryptedText = FileEncryptor.DecryptData(passKey, entry.Salt, entry.IV, fileText); + byte[] key = GetEncryptionKey(passKey, entry.Salt); + + aes256 = new RijndaelManaged + { + IV = Convert.FromBase64String(entry.IV), + Key = key, + Padding = PaddingMode.PKCS7, + Mode = CipherMode.CBC + }; + + ICryptoTransform decryptor = aes256.CreateDecryptor(aes256.Key, aes256.IV); + stream = new CryptoStream(fileStream, decryptor, CryptoStreamMode.Read); } else { @@ -314,12 +321,6 @@ public class Manifest string iV = null; string jsonAccount = JsonConvert.SerializeObject(account); - if (encrypt) - { - throw new NotSupportedException("Encrypted maFiles are not supported at this time."); - } - - string filename = account.Session.SteamID.ToString() + ".maFile"; ManifestEntry newEntry = new ManifestEntry() @@ -357,7 +358,35 @@ public class Manifest try { - File.WriteAllText(Program.SteamGuardPath + filename, jsonAccount); + Stream stream = null; + FileStream fileStream = File.OpenWrite(Path.Combine(Program.SteamGuardPath, newEntry.Filename)); + RijndaelManaged aes256; + + if (this.Encrypted) + { + byte[] key = GetEncryptionKey(passKey, newEntry.Salt); + + aes256 = new RijndaelManaged + { + IV = Convert.FromBase64String(newEntry.IV), + Key = key, + Padding = PaddingMode.PKCS7, + Mode = CipherMode.CBC + }; + + ICryptoTransform decryptor = aes256.CreateDecryptor(aes256.Key, aes256.IV); + stream = new CryptoStream(fileStream, decryptor, CryptoStreamMode.Write); + } + else + { + stream = fileStream; + } + + using (StreamWriter writer = new StreamWriter(stream)) + { + writer.Write(jsonAccount); + } + stream.Close(); return true; } catch (Exception) diff --git a/Program.cs b/Program.cs index 05f5159..e688719 100644 --- a/Program.cs +++ b/Program.cs @@ -78,7 +78,15 @@ public static class Program if (Verbose) Console.WriteLine("Opening manifest..."); Manifest = Manifest.GetManifest(true); if (Verbose) Console.WriteLine("Reading accounts from manifest..."); - SteamGuardAccounts = Manifest.GetAllAccounts(); + if (Manifest.Encrypted) + { + string passkey = Manifest.PromptForPassKey(); + SteamGuardAccounts = Manifest.GetAllAccounts(passkey); + } + else + { + SteamGuardAccounts = Manifest.GetAllAccounts(); + } if (SteamGuardAccounts.Length == 0) { Console.WriteLine("error: No accounts read.");