我正在尝试从我的 Chrome 浏览器获取会话。我可以在开发人员工具中看到 2 个 cookie 文件。但这对于用户从浏览器获取 cookie 值来说很不方便,我想用代码来做。所以我使用此代码来获取 Chrome 默认配置文件 cookie sqlite DB:
string local = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
string path = @"Google\Chrome\User Data\Default\Cookies";
path = Path.Combine(local, path);
Run Code Online (Sandbox Code Playgroud)
接下来我创建 SQLite 连接和请求
var cmd = new SQLiteCommand("SELECT encrypted_value, name FROM cookies WHERE host_key = 'my_host_ip'", con);
Run Code Online (Sandbox Code Playgroud)
然后我读了结果
byte[] encryptedCookie = (byte[])r.GetValue(r.GetOrdinal("encrypted_value"));
Run Code Online (Sandbox Code Playgroud)
并尝试解密它:
var decodedData = ProtectedData.Unprotect(encryptedCookie, null, DataProtectionScope.CurrentUser);
var plainText = Encoding.ASCII.GetString(decodedData);
Run Code Online (Sandbox Code Playgroud)
在这里我有例外
System.Security.Cryptography.CryptographicException
我知道我必须在启动浏览器的同一用户帐户下(在同一台机器上)解密 cookie 内容,并且参数DataProtectionScope.CurrentUser用于
我在调试器中看到 63 个字节(在encryptedCookie数组中),我也在 SQLite DB BLOB 字段中看到了这个字节。但Unprotect方法抛出System.Security.Cryptography.CryptographicException: Invalid …