【发布时间】:2018-06-21 07:50:26
【问题描述】:
我在 NodeJS v8.11.0 中使用此代码生成了一个 base64 编码的密钥:
const secret = 'shezhuansauce';
const key = crypto.createHash('sha256').update(String(secret)).digest('base64');
//output is REtgV24bDB7xQYoMuypiBASMEaJbc59nJWChoXbbmsA=
使用密钥,我尝试加密一个字符串:
var tobeEncrypted = 'some secret string';
const iv = crypto.randomBytes(16).toString('hex').slice(0, 16);
const cipher = crypto.createCipheriv('aes-256-ctr', key, iv);
const encrypted = cipher.update(String(tobeEncrypted), 'utf8', 'hex') + cipher.final('hex');
console.log(encrypted);
但是,我收到一个错误:
crypto.js:219
this._handle.initiv(cipher, toBuf(key), toBuf(iv));
^
Error: Invalid key length
密钥必须是base64字符串,因为我将它存储在云服务中,它只接收base64字符串。
感谢任何帮助。
【问题讨论】:
-
我如何在你的过程中解密?
标签: javascript node.js cryptography