C# Org.BouncyCastle.Crypto.InvalidCipherTextException: 'mac 检查 GCM 失败'

时间:2021-01-25 17:06:01

标签: c# encryption cookies cryptography bouncycastle

我正在尝试用 C# 编写 Chrome Cookie 解密器。我使用 System.Data.Sqlite 读取数据库,我使用 Org.Bouncycastle 解密 %AppData%\Local\Google\Chrome\User Data\Default\Cookies(Chorme 的 Cookie DB)中的 encrypted_value,密钥存储在 C 中: \Users\User\AppData\Local\Google\Chrome\User Data\Local State

我的代码是:

public static string Decrypt_AES_256_GCM(byte[] msg, byte[] key)
{
    if (key == null || key.Length != 256 / 8)
        throw new ArgumentException($"Key needs to be 256 bit!", "key");
    if (msg == null || msg.Length == 0)
        throw new ArgumentException("Message required!", "message");

    using (var cipherStream = new MemoryStream(msg))
    using (var cipherReader = new BinaryReader(cipherStream))
    {
        cipherReader.ReadBytes(3);
        var cipher = new GcmBlockCipher(new AesEngine());
        cipher.Init(false, new AeadParameters(new KeyParameter(key), 128, cipherReader.ReadBytes(96 / 8), msg));
        var cipherText = cipherReader.ReadBytes(msg.Length);
        var plainText = new byte[cipher.GetOutputSize(cipherText.Length)];
        try
        {
            int len = cipher.ProcessBytes(cipherText, 0, cipherText.Length, plainText, 0);
            cipher.DoFinal(plainText, len);
        }
        catch (InvalidCipherTextException)
        {
            return null;
        }
        return Encoding.Default.GetString(plainText);
    }
}

这是密钥的代码:

public byte[] GetKey()
{
    string keyFileText = File.ReadAllText(ChromeKeyPath); // reads the file (string)

    var jsonReader = JsonReaderWriterFactory.CreateJsonReader(Encoding.UTF8.GetBytes(keyFileText), new System.Xml.XmlDictionaryReaderQuotas());
    var root = XElement.Load(jsonReader);
    string encryptedKey = root.XPathSelectElement("//os_crypt/encrypted_key").Value;

    return ProtectedData.Unprotect(Convert.FromBase64String(encryptedKey).Skip(5).ToArray(), null, DataProtectionScope.LocalMachine); // decrypts the key and returns a byte Array
}

(我从 https://stackoverflow.com/a/60611673/12955591 那里得到了代码)

但是我收到此错误: enter image description here

这里抛出了错误: cipher.DoFinal(plainText, len);

有人可以帮我理解错误吗?

0 个答案:

没有答案
相关问题