javascript - 解密函数无法读取内容哈希抛出:TypeError [ERR_INVALID_ARG_TYPE]:
问题描述
当我第一次测试它时,使用 aes-256-ctr 算法的加密和解密都运行良好,但是在我将它发布到 IPFS 并返回它之后,它给了我以下错误。
TypeError [ERR_INVALID_ARG_TYPE]:第一个参数必须是字符串类型或 Buffer、ArrayBuffer 或 Array 的实例或类似 Array 的对象。收到未定义
这一切都符合我的标准。这是encryption.js 文件。
const crypto = require('crypto');
const algorithm = 'aes-256-ctr';
const secretKey = 'vOH6sdmpNWjRRIqCc7rdxs01lwHzfr33';
const iv = crypto.randomBytes(16);
//const file = fs.readFileSync("test.txt");
//console.log(secretKey.length);
const encrypt = (text) => {
const cipher = crypto.createCipheriv(algorithm, secretKey, iv);
const encrypted = Buffer.concat([cipher.update(text), cipher.final()]);
return {
iv: iv.toString('hex'),
content: encrypted.toString('hex')
};
};
const decrypt = (hash) => {
const decipher = crypto.createDecipheriv(algorithm, secretKey, Buffer.from(hash.iv, 'hex'));
const decrypted = Buffer.concat([decipher.update(Buffer.from(hash.content, 'hex')),
decipher.final()]);
return decrypted.toString();
};
解决方案
它似乎对我有用——但前提是你将返回的整个对象传递给encrypt
into decrypt
。如果你只是传入hash
没有iv
,你会得到那个错误。
推荐阅读
- c - 如何获得程序的完整输入流?
- python - Django form.is_valid() 失败的基于类的视图 - Form、SingleObject、DetailMixins
- matlab - IFFT 找到 sin(x^2)?
- sugarcrm - 将产品下拉列表添加到 suitecrm 中的 oppurtunity 模块
- c# - 如何在 C# 中以编程方式创建桌面快捷方式(链接),但使用自己的快捷方式文件扩展名(例如 .appfolder)而不是 .lnk?
- python - kivy 中的嵌入式网络浏览器 | android.config 出错
- jquery - 为什么我的下拉列表中没有列表未定义的数据?
- c - 当串行设备的路径改变时,C 程序停止
- python - 地址簿练习我希望它拆分列表的每一行,但它只需要最后一行
- c# - Selenium Webdriver 存储 cookie 并使用它们 c#