This is the error I get:
Error: Invalid IV length
at Decipheriv.createCipherBase (internal/crypto/cipher.js:103:19)
at Decipheriv.createCipherWithIV (internal/crypto/cipher.js:121:20)
at new Decipheriv (internal/crypto/cipher.js:264:22)
at Object.createDecipheriv (crypto.js:132:10)
This is my code;
const crypto = require(“crypto”);
const secret = “pppppppppppppppppppppppppppppppp”;
const encrypt = (password) => {
const iv = Buffer.from(crypto.randomBytes(16));
const cipher = crypto.createCipheriv(“aes-256-ctr”, Buffer.from(secret), iv);
const encryptedPassword = Buffer.concat([
cipher.update(password),
cipher.final(),
]);
return {
iv: iv.toString(“hex”),
password: encryptedPassword.toString(“hex”),
};
};
const decrypt = (encryption) => {
const decipher = crypto.createDecipheriv(
“aes-256-ctr”,
Buffer.from(secret),
Buffer.from(encryption.iv, “hex”)
);
const decryptedPassword = Buffer.concat([
decipher.update(Buffer.from(encryption.password, “hex”)),
decipher.final(),
]);
return decryptedPassword.toString();
};
module.exports = { encrypt, decrypt };
submitted by /u/thenovastar17
[link] [comments]