如何解密加密文本
How can I decrypt an encrypted text
我从 https://blog.logrocket.com/node-js-crypto-module-a-tutorial 得到了以下代码。当我 /encrypt
和 /decrypt
结果时,解密时出现以下错误:
Error: error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt
at Decipheriv._flush (internal/crypto/cipher.js:141:29)
at Decipheriv.prefinish (internal/streams/transform.js:147:10)
密码是:
app.post("/encrypt", (req, res) => {
crypto.scrypt(password, "salt", 24, (err, key) => {
if (err) throw err;
crypto.randomFill(new Uint8Array(16), (err, iv) => {
if (err) throw err;
const cipher = crypto.createCipheriv(algorithm, key, iv);
let encrypted = "";
cipher.setEncoding("hex");
cipher.on("data", (chunk) => (encrypted += chunk));
cipher.on("end", () => {
console.log(encrypted);
res.json({ encrypted });
});
cipher.write(req.body.payload);
cipher.end();
});
});
});
app.post("/decrypt", (req, res) => {
const key = crypto.scryptSync(password, "salt", 24);
const iv = Buffer.alloc(16, 0);
const decipher = crypto.createDecipheriv(algorithm, key, iv);
let decrypted = "";
decipher.on("readable", () => {
while (null !== (chunk = decipher.read())) {
decrypted += chunk.toString("utf8");
}
});
decipher.on("end", () => {
console.log(decrypted);
res.json({ decrypted });
});
decipher.write(req.body.payload, "hex");
decipher.end();
});
我在这里做错了什么? password
是从文件中的 const
读取的,因此密码不正确不是原因。
感谢@Topaco,这就是我想出的:
app.post("/encrypt", (req, res) => {
crypto.scrypt(password, "salt", 24, (err, key) => {
if (err) throw err;
const iv = Buffer.alloc(16, 0);
const cipher = crypto.createCipheriv(algorithm, key, iv);
let encrypted = "";
cipher.setEncoding("hex");
cipher.on("data", (chunk) => (encrypted += chunk));
cipher.on("end", () => res.json({ encrypted }));
cipher.write(req.body.payload);
cipher.end();
});
});
app.post("/decrypt", (req, res) => {
const key = crypto.scryptSync(password, "salt", 24);
const iv = Buffer.alloc(16, 0);
const decipher = crypto.createDecipheriv(algorithm, key, iv);
let decrypted = "";
decipher.on("readable", () => {
while (null !== (chunk = decipher.read())) {
decrypted += chunk.toString("utf8");
}
});
decipher.on("end", () => res.json({ decrypted }));
decipher.write(req.body.payload, "hex");
decipher.end();
});
我从 https://blog.logrocket.com/node-js-crypto-module-a-tutorial 得到了以下代码。当我 /encrypt
和 /decrypt
结果时,解密时出现以下错误:
Error: error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt
at Decipheriv._flush (internal/crypto/cipher.js:141:29)
at Decipheriv.prefinish (internal/streams/transform.js:147:10)
密码是:
app.post("/encrypt", (req, res) => {
crypto.scrypt(password, "salt", 24, (err, key) => {
if (err) throw err;
crypto.randomFill(new Uint8Array(16), (err, iv) => {
if (err) throw err;
const cipher = crypto.createCipheriv(algorithm, key, iv);
let encrypted = "";
cipher.setEncoding("hex");
cipher.on("data", (chunk) => (encrypted += chunk));
cipher.on("end", () => {
console.log(encrypted);
res.json({ encrypted });
});
cipher.write(req.body.payload);
cipher.end();
});
});
});
app.post("/decrypt", (req, res) => {
const key = crypto.scryptSync(password, "salt", 24);
const iv = Buffer.alloc(16, 0);
const decipher = crypto.createDecipheriv(algorithm, key, iv);
let decrypted = "";
decipher.on("readable", () => {
while (null !== (chunk = decipher.read())) {
decrypted += chunk.toString("utf8");
}
});
decipher.on("end", () => {
console.log(decrypted);
res.json({ decrypted });
});
decipher.write(req.body.payload, "hex");
decipher.end();
});
我在这里做错了什么? password
是从文件中的 const
读取的,因此密码不正确不是原因。
感谢@Topaco,这就是我想出的:
app.post("/encrypt", (req, res) => {
crypto.scrypt(password, "salt", 24, (err, key) => {
if (err) throw err;
const iv = Buffer.alloc(16, 0);
const cipher = crypto.createCipheriv(algorithm, key, iv);
let encrypted = "";
cipher.setEncoding("hex");
cipher.on("data", (chunk) => (encrypted += chunk));
cipher.on("end", () => res.json({ encrypted }));
cipher.write(req.body.payload);
cipher.end();
});
});
app.post("/decrypt", (req, res) => {
const key = crypto.scryptSync(password, "salt", 24);
const iv = Buffer.alloc(16, 0);
const decipher = crypto.createDecipheriv(algorithm, key, iv);
let decrypted = "";
decipher.on("readable", () => {
while (null !== (chunk = decipher.read())) {
decrypted += chunk.toString("utf8");
}
});
decipher.on("end", () => res.json({ decrypted }));
decipher.write(req.body.payload, "hex");
decipher.end();
});