gpt4 book ai didi

node.js - Nodejs 6.10.2 加密 AES key 长度无效

转载 作者:搜寻专家 更新时间:2023-10-31 23:01:27 27 4
gpt4 key购买 nike

我尝试使用 crypto 来加密文件。这是我的代码:

const crypto = require('crypto');
const fs = require('fs');

const input = fs.createReadStream('test.jpg');
const output = fs.createWriteStream('test.enc');

const sharedSecret = crypto.randomBytes(256);
const initializationVector = crypto.randomBytes(16);

const cipher = crypto.createCipheriv('aes-256-cbc', sharedSecret, initializationVector);

input.pipe(cipher).pipe(output);

我得到了错误:

crypto.js:191
this._handle.initiv(cipher, toBuf(key), toBuf(iv));
^

Error: Invalid key length
at Error (native)
at new Cipheriv (crypto.js:191:16)
at Object.Cipheriv (crypto.js:189:12)
at Object.<anonymous> (/Users/lijinyao/Projects/HyperAlbum/Encryption/encrypt.js:10:23)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)

虽然 sharedSecret 长度应该与 aes-length 相同,但事实并非如此。我应该使用什么长度?谢谢:)

最佳答案

您将字节与位混淆了。 aes-256 表示 256 位 = 32 字节。

试试这个:

const crypto = require('crypto');
const fs = require('fs');

const input = fs.createReadStream('test.jpg');
const output = fs.createWriteStream('test.enc');

const sharedSecret = crypto.randomBytes(32);
const initializationVector = crypto.randomBytes(16);

const cipher = crypto.createCipheriv('aes-256-cbc', sharedSecret, initializationVector);

input.pipe(cipher).pipe(output);

如果你看不出区别,变化是:

const sharedSecret = crypto.randomBytes(32);

关于node.js - Nodejs 6.10.2 加密 AES key 长度无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44502637/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com