【发布时间】:2021-06-17 16:51:39
【问题描述】:
我知道这个主题有几个答案,但我对详细信息密钥长度有疑问。
我想用 CTR 加密 AES 算法中的数据。加密长度为 256 位。
如果我通过密钥长度 256 和 IV 长度 16 对数据进行加密,则会收到错误 Invalid key length。我认为 IV 必须是相同的密钥长度。我将 IV 的长度更改为 256,但出现错误 Invalid iv length。我发现 IV 必须是 16 位长。
只有当我的密钥是 16 位并且我的 IV 是 16 位长时,我的代码才有效。所以我的代码没有用 256 位长度加密。
我的代码
const crypto = require('crypto');
let key = crypto.randomBytes(16).toString('hex'); // Key is static
let iv = crypto.randomBytes(16);
let cipher = crypto.createCipheriv('aes-256-ctr', process.env.KEY, iv);
let encrypte = cipher.update("Example_data", 'utf-8', 'hex');
encrypte += cipher.final('hex');
密钥长度为 256 位的数据如何加密?
【问题讨论】:
标签: node.js encryption cryptography cryptojs