Skip to content
engineering workbench.by StudyPort Academy
All errors
data · DEBUG LIBRARY

Passwords were encrypted, so the key decrypts every one

Locate the failed assumption, reproduce it, then repair the cause.

What actually caused it

Encryption is reversible on purpose. AES, or any cipher, stores ciphertext that the same key turns back into the password. If the application can decrypt the column to check the password, then anyone who reads the database and the key can decrypt every password. The key is often in the same environment, the same backup, or a debug log that printed the config. Users reuse passwords, so the dump is their other accounts too. Naming the column encrypted, or calling the encryptPassword, does not make the operation one-way. A hash has no path back to the password. Verification hashes the attempt with the stored salt and compares bytes. A wrong guess fails the compare and never yields the original string.

Minimal reproduction

Ciphertext you can open

const decipher = crypto.createDecipheriv('aes-256-gcm', key, iv);
const password = decipher.update(stored) + decipher.final('utf8');

Anyone with the key and the column reads every password.

The fix

Store a unique salt and the output of a memory-hard password hash. Argon2id from a reviewed library is the production choice. Node's scrypt is the memory-hard available when you cannot add that library, and it is still not a cipher. Verify by hashing the attempt and comparing with a timing-safe equal. Delete the decryption function. There should be no code path that returns the original password. If you already stored ciphertext, force a reset or rehash on the next successful login once a real hash exists. Do not invent a hash. A raw SHA-256 of the password has no salt and no memory cost, which is the other way this column goes wrong.

Salt and a one-way hash

const hash = crypto.scryptSync(password, salt, 32);
const ok = crypto.timingSafeEqual(stored, hash);

A wrong password fails the compare. The original string is not recoverable.