【问题标题】:Encrypt Cookie and save to Chrome加密 Cookie 并保存到 Chrome
【发布时间】:2021-01-08 01:24:39
【问题描述】:

我目前正在编写一个 Node.js 脚本,该脚本将我登录到一个网站并在我登录的情况下自动打开浏览器。 我一直在尝试将 cookie(从登录 POST 请求中收集)添加到 chrome 的“cookie”数据库中,但我未能正确加密。 Stackoverflow 线程,关于 Cookie 的加密,我遇到的是这个:Chrome 80 how to decode cookies 但那个帖子是关于解密 cookie 而不是加密它。

function encryptCookie(cookie, key){
    const iv = crypto.randomBytes(16);
    let cipher = crypto.createCipheriv('aes-256-gcm', key, iv);
    let encrypted = cipher.update(cookie);
    encrypted = Buffer.concat([Buffer.from('v10'), encrypted, cipher.final()]);
    console.log(`Encrypted: ${encrypted}`);
    return encrypted; 
}

至于“密钥”,我使用的是本地状态的解密密钥。 (我使用 DPAPI 模块解密了密钥。)我知道加密的 cookie 总是以 v10 开头,所以我将它添加为前缀,但加密的 Cookie 仍然太短。

我知道我可以使用 webscraper 或者只是在页面上保护我的密码,但我想与 cookie 数据库和 HTTP 请求进行交互。

【问题讨论】:

    标签: node.js google-chrome encryption cookies cryptography


    【解决方案1】:

    linked answer中的数据结构描述如下:

    加密数据以 v10 的 ASCII 编码开头(即 0x763130),后跟 12 个字节的 nonce,实际密文和 最后是 16 字节的认证标签。

    我。 e.数据结构如下:

    • 3 字节前缀 v10
    • 12 字节 IV
    • 密文
    • 16字节认证标签

    长度为:3 + 12 + ciphertextlength + 16。另外,由于GCM模式没有使用padding,所以密文的长度与明文的长度相同。

    除其他外,发布的代码使用了错误的 IV 长度,没有连接 IV,也没有考虑身份验证标签。

    加密/解密的一种可能实现是:

    var crypto = require('crypto');
    
    function encryptCookie(cookie, key){
        const iv = crypto.randomBytes(12);
        const cipher = crypto.createCipheriv('aes-256-gcm', key, iv);
        const encryptedCookie = Buffer.concat(
          [
            Buffer.from('v10'),         // prefix
            iv,                         // 12 bytes nonce
            cipher.update(cookie),      // cookie data
            cipher.final(), 
            cipher.getAuthTag()         // 16 bytes authentication
          ]);
        return encryptedCookie; 
    }
    
    function decryptCookie(encryptedCookie, key){
        const prefix = encryptedCookie.slice(0, 3);                                     // prefix
        const iv = encryptedCookie.slice(3, 3 + 12);                                    // 12 bytes nonce
        const ciphertext = encryptedCookie.slice(3 + 12, encryptedCookie.length - 16);  // encrypted cookie
        const authTag = encryptedCookie.slice(encryptedCookie.length - 16);             // 12 bytes authentication tag
        const decipher = crypto.createDecipheriv('aes-256-gcm', key, iv);
        decipher.setAuthTag(authTag);
        const decryptedCookie = Buffer.concat(
          [
            decipher.update(ciphertext),      // encrypted cookie
            decipher.final(), 
          ]);
        return decryptedCookie; 
    }
    
    // Encryption
    const cookie = Buffer.from('The test cookie with some data');
    const key = Buffer.from('01234567890123456789012345678901');
    const encryptedCookie = encryptCookie(cookie, key);
    console.log(encryptedCookie);
    console.log(encryptedCookie.length);
    
    // Decryption
    const decryptedCookie = decryptCookie(encryptedCookie, key);
    console.log(decryptedCookie.toString('utf8'));
    

    对于长度为 30 字节的示例 cookie,加密数据的长度为 3 + 12 + 30 + 16 = 61 字节。

    【讨论】:

    • 哇,感谢您快速详细的回答!我一定会按照您推荐的方式尝试。我以前从未加密/解密,所以再次感谢您的帮助。一旦我有时间处理这个项目(很可能在大约 10 小时内),我会再次评论这篇文章。
    猜你喜欢
    • 1970-01-01
    • 2021-07-13
    • 1970-01-01
    • 2014-04-24
    • 2017-11-03
    • 2015-10-06
    • 1970-01-01
    • 2021-08-27
    相关资源
    最近更新 更多