解密函数无法读取内容哈希抛出:TypeError [ERR_INVALID_ARG_TYPE]:
Decrypt function can't read content hash throws : TypeError [ERR_INVALID_ARG_TYPE]:
当我第一次测试它时,使用 aes-256-ctr 算法加密和解密都工作正常,但在我将它发布到 IPFS 并返回它后,它给了我以下错误。
TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object. Received undefined
这一切看起来都符合我的标准。
这是 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
返回的整个对象传递给 decrypt
。如果您只传入 hash
而没有 iv
,则会出现该错误。
当我第一次测试它时,使用 aes-256-ctr 算法加密和解密都工作正常,但在我将它发布到 IPFS 并返回它后,它给了我以下错误。
TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object. Received undefined
这一切看起来都符合我的标准。 这是 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
返回的整个对象传递给 decrypt
。如果您只传入 hash
而没有 iv
,则会出现该错误。