I am creating a Password Manager and I wanted to create a JavaScript function to encrypt/decrypt data in the client-side, then I will be saving the only the encrypted data in MySQL Server. To encrypt/decrypt I am using Crypto-JS. This is what I have created so far:
HTML <head>
:
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/crypto-js.min.js"></script>
Code:
function EncryptData(data, password){
var iterations = 10000;
var salt = CryptoJS.lib.WordArray.random(128 / 8).toString();
try {
var password_hash = CryptoJS.SHA512(password + salt);
var key = CryptoJS.PBKDF2(password_hash, salt, { keySize: 512 / 32, iterations: iterations }).toString();
var ciphertext = CryptoJS.AES.encrypt(data, key, { mode: CryptoJS.mode.CFB, padding: CryptoJS.pad.Pkcs7 });
var params = iterations + ':' + salt + ':' + ciphertext;
var encoded = CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(params));
console.log(encoded);
} catch(error) {
console.log("Unable to encrypt data.");
}
}
function DecryptData(data, password){
var decoded = CryptoJS.enc.Base64.parse(data).toString(CryptoJS.enc.Utf8);
var iterations = decoded.split(':')[0];
var salt = decoded.split(':')[1];
var ciphertext = decoded.split(':')[2];
try {
var password_hash = CryptoJS.SHA512(password + salt);
var key = CryptoJS.PBKDF2(password_hash, salt, { keySize: 512 / 32, iterations: iterations }).toString();
var cleartext = CryptoJS.AES.decrypt(ciphertext, key, { mode: CryptoJS.mode.CFB, padding: CryptoJS.pad.Pkcs7 }).toString(CryptoJS.enc.Utf8);
console.log(cleartext);
} catch(error) {
console.log("Unable to decrypt data.");
}
}
Encrypt data
EncryptData("This is a simple demo", "abc123");
Decrypt data
DecryptData("MTAwMDA6MDg3NjNiZjEwNDM1YzhkZTJkMGU2NjM5M2I1M2YxNDc6VTJGc2RHVmtYMStxZWZZSmJ2eTJHak85cTdxOS9kK2dYaCt6UHRBcjJXMEt3bE1uakNaaTY5QmVmKzA3Y1lkaw==", "abc123");
The code works fine. I would like to read comments to know if this is secure enough (of course abc123 should be replaced by a mega secure password).