Ejemplo n.º 1
0
        public SteamGuardAccount GetAccount(SecureString passkey = null, int index = 0)
        {
            SteamGuardAccount account = new SteamGuardAccount();

            if (passkey == null && Encrypted)
            {
                return(account);
            }
            if (!UseMaFiles)
            {
                credMan = CredManifest.GetManifest();
                string fileText = credMan.Entries[index].Contents;

                fileText = Encryptor.DPAPIUnprotect(fileText, Encryptor.AccountEntropy);

                if (fileText.StartsWith("Encrypted"))
                {
                    string decryptedText = Encryptor.DecryptData(passkey, credMan.Entries[index].Salt, credMan.Entries[index].IV, fileText.Remove(0, 9)); if (decryptedText == null)
                    {
                        return(account);
                    }
                    fileText = decryptedText;
                }

                var acc = JsonConvert.DeserializeObject <SteamGuardAccount>(fileText);
                if (acc == null)
                {
                    return(account);
                }
                return(acc);
            }
            else
            {
                if (passkey == null && Encrypted)
                {
                    return(new SteamGuardAccount());
                }
                string maDir = GetExecutableDir() + "/maFiles/";

                string fileText = File.ReadAllText(maDir + Entries[index].FileName);

                fileText = Encryptor.DPAPIUnprotect(fileText, Encryptor.AccountEntropy);

                if (fileText.StartsWith("Encrypted"))
                {
                    string decryptedText = Encryptor.DecryptData(passkey, Entries[index].Salt, Entries[index].IV, fileText.Remove(0, 9)); if (decryptedText == null)
                    {
                        return(account);
                    }
                    fileText = decryptedText;
                }

                var acc = JsonConvert.DeserializeObject <SteamGuardAccount>(fileText);
                if (acc == null)
                {
                    return(account);
                }
                return(acc);
            }
        }
Ejemplo n.º 2
0
        public bool ChangeEncryptionKey(SecureString oldKey, SecureString newKey)
        {
            if (Encrypted)
            {
                if (!VerifyPasskey(oldKey))
                {
                    return(false);
                }
            }
            bool toEncrypt = newKey != null;

            credMan = CredManifest.GetManifest();
            if (UseMaFiles)
            {
                string maDir = GetExecutableDir() + "/maFiles/";
                for (int i = 0; i < Entries.Count; i++)
                {
                    ManifestEntry entry    = Entries[i];
                    string        filename = maDir + entry.FileName;
                    if (!File.Exists(filename))
                    {
                        continue;
                    }

                    string fileContents = File.ReadAllText(filename);

                    fileContents = Encryptor.DPAPIUnprotect(fileContents, Encryptor.AccountEntropy);

                    if (fileContents.StartsWith("Encrypted"))
                    {
                        fileContents = Encryptor.DecryptData(oldKey, entry.Salt, entry.IV, fileContents.Remove(0, 9));
                    }

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

                    if (toEncrypt)
                    {
                        newSalt             = Encryptor.GetRandomSalt();
                        newIV               = Encryptor.GetInitializationVector();
                        toWriteFileContents = "Encrypted" + Encryptor.EncryptData(newKey, newSalt, newIV, fileContents);
                    }

                    if (UseDPAPI)
                    {
                        toWriteFileContents = Encryptor.DPAPIProtect(toWriteFileContents, Encryptor.AccountEntropy);
                    }


                    File.WriteAllText(filename, toWriteFileContents);
                    if (UseWindowsFileEncryption)
                    {
                        File.Encrypt(filename);
                    }
                    else
                    {
                        File.Decrypt(filename);
                    }
                    entry.IV   = newIV;
                    entry.Salt = newSalt;
                }
            }
            else
            {
                foreach (CredManifestEntry entry in credMan.Entries)
                {
                    string fileContents = entry.Contents;

                    fileContents = Encryptor.DPAPIUnprotect(fileContents, Encryptor.AccountEntropy);

                    if (fileContents.StartsWith("Encrypted"))
                    {
                        fileContents = Encryptor.DecryptData(oldKey, entry.Salt, entry.IV, fileContents.Remove(0, 9));
                    }

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

                    if (toEncrypt)
                    {
                        newSalt             = Encryptor.GetRandomSalt();
                        newIV               = Encryptor.GetInitializationVector();
                        toWriteFileContents = "Encrypted" + Encryptor.EncryptData(newKey, newSalt, newIV, fileContents);
                    }
                    else
                    {
                        entry.Encrypted = false;
                    }

                    if (UseDPAPI)
                    {
                        toWriteFileContents = Encryptor.DPAPIProtect(toWriteFileContents, Encryptor.AccountEntropy);
                    }

                    entry.Contents = toWriteFileContents;
                    entry.IV       = newIV;
                    entry.Salt     = newSalt;
                }
                credMan.Key = newKey;
                credMan.Save();
            }

            Encrypted = toEncrypt;

            Save();
            return(true);
        }