I'm using AES-256-CBC to encrypt password for a set of users, and for each user in the database we gotta generate and store the password in the database. The database has constraint that the name of the user must be unique, so I was wondering if using the name of the person as the initialization vector was okay, because names are sufficiently random for a cryptographic perspective, if I encode it to hex and use the 16 bytes for the IV would it be okay. This would give me one less thing to store in the database.


1 Answer 1


encrypt password for a set of users

You should hash the password for users instead of encrypting them, rarely does an application need to recover the password for any purpose.

PHP has password_hash and password_verify functions that replaces the crypt function that provides password hashing functionality, and it's broadly interoperable with other implementations of SHA-256/512, BCrypt, Argon password hashing algorithms.

Your specific environment may ship with its own implementations. Python has PassLib.

  • $\begingroup$ for my usecase, I need to encrypt the password, and because i need to decrypt it later, please do tell me if using the unique username is okay $\endgroup$ Mar 14, 2023 at 6:23
  • $\begingroup$ @troubleddev You do realize AES has a blocksize of 128 bits do you? Then how are you suppose to encrypt passwords shorter than 16 octets using the CBC mode? $\endgroup$
    – DannyNiu
    Mar 14, 2023 at 8:21
  • $\begingroup$ would concatenate by repeatition of the same value e.g. appleappleapple for a sufficiently long key not suitable? $\endgroup$ Mar 14, 2023 at 8:26
  • 3
    $\begingroup$ @troubleddev All these just show that you're not fully informed to make security-related decisions. Let your employer hire a specialist if you haven't already, and involve him in the entire process. $\endgroup$
    – DannyNiu
    Mar 14, 2023 at 8:35
  • $\begingroup$ @troubleddev -- you may think you need to decrypt the password for example because you need to check the password history. Many systems don't allow changing a password to one that's too similar. There are ways however to accomplish this without storing the password itself. $\endgroup$
    – Amit
    Mar 14, 2023 at 17:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.