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);
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);
上面的附言里面 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!!');
});
1
Mitt 2021-02-08 19:22:43 +08:00 1
128/8 = 16
|
2
lqzhgood OP @Mitt openssl 是怎么把 32 位的 vi `bc1f89d3421a6f097262c348890a9acc` 处理成 16 位的呢。
|
3
Mitt 2021-02-08 19:24:53 +08:00 1
16 字节才是正确的,32 位是因为它是十六进制,你 hex2bin 就好了
|
4
Jirajine 2021-02-08 19:28:06 +08:00 via Android 1
显然 key 接受的类型是二进制数据,而不是字符串。
|
5
lqzhgood OP @Jirajine
Nodejs http://nodejs.cn/api/crypto.html#crypto_crypto_createcipheriv_algorithm_key_iv_options 不是说 key 支持 String 类型么~ |
6
lqzhgood OP @Mitt 能再说清楚点么?
我把 key 写为 Buffer.from([ 0xbc, 0x1f, 0x89, 0xd3, 0x42, 0x1a, 0x6f, 0x09, 0x72, 0x62, 0xc3, 0x48, 0x89, 0x0a, 0x9a, 0xcc, ]) 还是无法解出~~ |
7
EPr2hh6LADQWqRVH 2021-02-08 20:03:35 +08:00 1
@lqzhgood Buffer.from('bc1f89d3421a6f097262c348890a9acc', 'hex'), 他俩类型都统一成 Buffer
|
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')); 只能说文档比较糟糕,缺乏例子且描述较为模糊。 |
10
lqzhgood OP |
11
xxcheng 2021-12-23 23:20:30 +08:00
太感谢了
|