V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
lqzhgood
V2EX  ›  Node.js

openssl aes-128-cbc 用 nodejs 怎么写? nodejs 的 crypto 库 vi 只认 16 位额……

  •  
  •   lqzhgood · 2021-02-08 19:20:59 +08:00 · 2129 次点击
    这是一个创建于 1144 天前的主题,其中的信息可能已经有所发展或是发生改变。
    openssl enc -d -aes-128-cbc -in ./1qaz  -K bc1f89d3421a6f097262c348890a9acc  -out decrypted.jpg -iv bc1f89d3421a6f097262c348890a9acc  
    

    我用 nodejs 这么写,一直报错 Invalid IV length 求教。。。。

    样本文件下载地址 https://netcut.cn/nodejs

    const crypto = require("crypto");
    const key = "bc1f89d3421a6f097262c348890a9acc";
    const iv = key;
    const cipher = crypto.createCipheriv('aes-128-cbc', key, iv);
    
    const encryptedBytes = fs.readFileSync('./1qaz');
    cipher.update(encryptedBytes);
    
    const data = cipher.final('hex');
    
    console.log('data', data);
    
    
    第 1 条附言  ·  2021-02-08 20:26:52 +08:00
    const encryptedBytes = fs.readFileSync('./1qaz');
    
    const decipher = crypto.createCipheriv('aes-128-cbc', Buffer.from(key, 'hex'), Buffer.from(iv, 'hex'));
    const data = Buffer.concat([decipher.update(encryptedBytes), decipher.final()]);
    
    fs.writeFileSync('./test.jpg', data);
    
    
    第 2 条附言  ·  2021-02-08 20:40:45 +08:00

    上面的附言里面 createDecipheriv 写成 createCipheriv 了………………

    const key = "bc1f89d3421a6f097262c348890a9acc";
    const iv = key;
    const encryptedBytes = fs.readFileSync('./1qaz');
    
    const decipher = crypto.createDecipheriv('aes-128-cbc', Buffer.from(key, 'hex'), Buffer.from(iv, 'hex'));
    const data = Buffer.concat([decipher.update(encryptedBytes), decipher.final()]);
    
    fs.writeFileSync('./test.jpg', data);
    

    再加个 stream 形式的, 留给后来人

    const key = "bc1f89d3421a6f097262c348890a9acc";
    const iv = key;
    
    let input = fs.createReadStream('./1qaz');
    let output = fs.createWriteStream('test2.jpg');
    
    const decipher = crypto.createDecipheriv('aes-128-cbc', Buffer.from(key, 'hex'), Buffer.from(iv, 'hex'));
    
    input.pipe(decipher).pipe(output);
    
    output.on('finish', () => {
        console.log('ok!!');
    });
    
    
    11 条回复    2021-12-23 23:20:30 +08:00
    Mitt
        1
    Mitt  
       2021-02-08 19:22:43 +08:00   ❤️ 1
    128/8 = 16
    lqzhgood
        2
    lqzhgood  
    OP
       2021-02-08 19:24:40 +08:00
    @Mitt openssl 是怎么把 32 位的 vi `bc1f89d3421a6f097262c348890a9acc` 处理成 16 位的呢。
    Mitt
        3
    Mitt  
       2021-02-08 19:24:53 +08:00   ❤️ 1
    16 字节才是正确的,32 位是因为它是十六进制,你 hex2bin 就好了
    Jirajine
        4
    Jirajine  
       2021-02-08 19:28:06 +08:00 via Android   ❤️ 1
    显然 key 接受的类型是二进制数据,而不是字符串。
    lqzhgood
        5
    lqzhgood  
    OP
       2021-02-08 19:37:27 +08:00
    lqzhgood
        6
    lqzhgood  
    OP
       2021-02-08 19:49:21 +08:00
    @Mitt 能再说清楚点么?
    我把 key 写为 Buffer.from([
    0xbc, 0x1f, 0x89, 0xd3, 0x42, 0x1a, 0x6f, 0x09, 0x72, 0x62, 0xc3, 0x48, 0x89, 0x0a, 0x9a, 0xcc,
    ])

    还是无法解出~~
    EPr2hh6LADQWqRVH
        7
    EPr2hh6LADQWqRVH  
       2021-02-08 20:03:35 +08:00   ❤️ 1
    @lqzhgood Buffer.from('bc1f89d3421a6f097262c348890a9acc', 'hex'), 他俩类型都统一成 Buffer
    Jirajine
        8
    Jirajine  
       2021-02-08 20:03:42 +08:00   ❤️ 1
    > Both arguments must be 'utf8' encoded strings
    这里的意思应该是指把 string 看作 utf8 encoded bytes, 而你这里显然不是。
    改成 const cipher = crypto.createCipheriv('aes-128-cbc', Buffer.from(key,'hex'), Buffer.from(iv,'hex'));

    只能说文档比较糟糕,缺乏例子且描述较为模糊。
    lqzhgood
        9
    lqzhgood  
    OP
       2021-02-08 20:27:20 +08:00
    @avastms
    @Jirajine
    额, 解出来还是不对…… T.T
    lqzhgood
        10
    lqzhgood  
    OP
       2021-02-08 20:30:13 +08:00
    @avastms
    @Jirajine
    额 是我错了。。。

    createDecipheriv 写成 createCipheriv 了………………
    从中午搞到现在 。。。 想死 。


    还是谢谢各位了 。。。
    xxcheng
        11
    xxcheng  
       2021-12-23 23:20:30 +08:00
    太感谢了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2713 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 00:21 · PVG 08:21 · LAX 17:21 · JFK 20:21
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.