0

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).

1
  • This library was useful some years ago, because there was no myna alternatives at that time. Now all major browsers support Web Crypto API/SubtleCrypto. I'd recommend you to use this API, not libraries that implement it from scratch. Usually, browser developers have more resources to properly test their functionality. That's why you can expect it is more reliable than solutions developed by a small community behind particular library. This is crucial for cryptographic functionality.
    – mentallurg
    Commented May 19, 2023 at 20:46

1 Answer 1

2

This is not secure for several reasons.

The crypto-js library you're using is almost two years old and seems to have been abandoned completely. The issues keep piling up, and there doesn't seem to be anyone to take care of them. I would stay away from dead software.

Cryptography in the browser also has conceptual issues and a large attack surface. First, there's no clear separation between “client-side” and “server-side”, because all the code which the client runs is dynamically provided by the server. If the server made a small change to completely break the encryption, the client likely wouldn't notice that. An attacker can also use cross-site scripting or similar attacks to mess with the client-side encryption procedure (e. g. grab the plaintext password before it's hashed).

Last but not least, coming up with your own ad-hoc security solution is always error-prone. A small mistake anywhere in the code (not necessarily in the cryptography itself) can expose all plaintext passwords and render the entire project useless.

The proper solution here is to use a well-tested, well-maintained password manager like KeePassXC, possibly together with browser plugins.

19
  • 1
    1) If you cannot even see the problem with using software that has been abandoned for two(!) years, I'm afraid I cannot help you here. 2) Look up supply chain security 3) Look up the OWASP Top 10 3) The entire crypto code is provided by the server, so any attack against (or misbehavior of) the server directly affects the client. 5) When the attack goes against the cryptography-related code, then it's an attack against the encryption.
    – Ja1024
    Commented May 19, 2023 at 20:34
  • 1
    I have no idea why you're so angry, but I think you should sleep this over and think about it for a while.
    – Ja1024
    Commented May 19, 2023 at 20:35
  • 1
    Sorry, but you're completely misguided in this case, and recommending an approach that is demonstrably insecure shouldn't happen in a security community. As I've said, I have no idea what your problem is, but if you want a proper discussion, you should create a question and not flood people with your rants.
    – Ja1024
    Commented May 19, 2023 at 20:42
  • 2
    It seems your main issue is that you think everything not directly related to cryptography is out of scope for the question and must not be considered. You see standard algorithms like AES, you see a library which you think is fine (even though it's abandoned), so you conclude the code in question is fine. I strongly disagree with this rather naive approach. Attacks against cryptography can exploit any weakness, not just problems with the crypto algorithms themselves. So any serious assessment must address JavaScript-related issues and the roll-your-own problem, whether you like it or not.
    – Ja1024
    Commented May 20, 2023 at 0:38
  • 2
    Interesting discussion. WRT 'If the server made a small change to completely break the encryption, the client likely wouldn't notice that' - this is the infamous 'browser crypto chicken and egg problem'. In other words, if the user can't trust the server with his/her secrets, then how can the user trust the server to serve secure client-side crypto code? See security.stackexchange.com/questions/238441/… for some interesting reading on this subject.
    – mti2935
    Commented May 20, 2023 at 1:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .