【发布时间】:2021-04-14 20:06:28
【问题描述】:
我正在使用 Node 的加密原生 api 对 id 进行编码,因为它将显示在 URL 参数中,问题是,它以一种可以猜到的方式进行编码,我无法理解为什么,这是我的代码.
const encrypt = (key, value) => {
const iv = crypto.randomBytes(16);
const cipher = crypto.createCipheriv('aes-256-ctr', Buffer.from(key), iv);
let encrypted = cipher.update(value);
encrypted = Buffer.concat([encrypted, cipher.final()]);
return `${iv.toString('hex')}-${encrypted.toString('hex')}`;
}
发生的情况是 AES 编码的 id 以可预测的方式输出,例如,数字 5 输出代码 160f20bea36be22f90b092f876f1abdd-55 并注意如果我将最后两个数字更改为 56,则输出变为 6 或 54 , id 被发现,因为它输出 7。
我想要它做的是给我一个安全编码的 id。
【问题讨论】:
标签: javascript cryptography aes