首页 > 解决方案 > 解密函数无法读取内容哈希抛出: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();
};

标签: javascriptnode.jsaescryptojs

解决方案


它似乎对我有用——但前提是你将返回的整个对象传递给encryptinto decrypt。如果你只是传入hash没有iv,你会得到那个错误。


推荐阅读