Created a simple app that encrypts text, but how do i show error when wrong password or salt is given. Hosted it on replit. But when i give wrong password or salt it just decrypts it. There isn't a callback or function in crypto for crypto.createDecipheriv()
const app = {
encrypt(text, password, salt) {
password = password.repeat(32).substr(0, 32);
salt = salt.repeat(16).substr(0, 16);
crypto.pbkdf2(password, salt, 10, 16, 'sha512', (err, key) => {
if (err) {
console.log(err);
} else {
key = key.toString('hex');
const cipher = crypto.createCipheriv('aes-256-gcm', key, salt);
let encrypted = cipher.update(text, 'utf8', 'hex');
console.log(encrypted);
}
});
},
decrypt(text, password, salt) {
password = password.repeat(32).substr(0, 32);
salt = salt.repeat(16).substr(0, 16);
crypto.pbkdf2(password, salt, 10, 16, 'sha512', (err, key) => {
if (err) {
console.log(err);
} else {
key = key.toString('hex');
const cipher = crypto.createDecipheriv('aes-256-gcm', key, salt);
let decrypted = cipher.update(text, 'hex', 'utf8');
console.log(decrypted);
}
});
}
}
const message = 'Hello World';
app.encrypt(message, 'password', 'salt');
const cipherText = 'a0a4e0ad97133494856502';
app.decrypt(cipherText, 'password', 'salt');