如何解决解密函数无法读取内容哈希抛出:TypeError [ERR_INVALID_ARG_TYPE]:
当我第一次测试它时,使用 aes-256-ctr 算法的加密和解密工作正常,但在我将它发布到 IPFS 并返回后,它给了我以下错误。
TypeError [ERR_INVALID_ARG_TYPE]:第一个参数必须是字符串类型或 Buffer、ArrayBuffer 或 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,Buffer.from(hash.iv,'hex'));
const decrypted = Buffer.concat([decipher.update(Buffer.from(hash.content,'hex')),decipher.final()]);
return decrypted.toString();
};
解决方法
它似乎对我有用——但前提是您将 encrypt
返回的整个对象传递给 decrypt
。如果您只传入 hash
而没有 iv
,则会出现该错误。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。