1
$\begingroup$

When encrypting data, I want to verify that the correct key was entered without hashing it. Is it safe to decrypt the ciphertext and compare the hash of it with the stored one or can the plaintext (which could be very long) be read from the hash?

$\endgroup$
5
  • 4
    $\begingroup$ Welcome to Cryptography.SE Passwords are not hashed with standard collision-resistant hashes like SHA256 or SHA3-512 they are hashed with special password hashing algorithms like PBKDF2, Scrypt, or better Argon2. While there are really good open-source password manager like Keepass, this question is lack of the basic research of a password manager developer must do; searcing for what others do! $\endgroup$
    – kelalaka
    Nov 20 at 18:10
  • 3
    $\begingroup$ Also, the passwords are not stored in the hash form in the password manager, they are stored encrypted so that the password manager can decrypt them when needed. hashing is one-way process that destroys the original data. Hashing != encryption. you need to create an encryption key from the user's password so that this key can decrypt the actual key that encrypts the passwords. $\endgroup$
    – kelalaka
    Nov 20 at 18:13
  • 1
    $\begingroup$ How do your users enter keys to your program? How do they memorize it? They need to enter a password, usually generated with a good one like dicewire. $\endgroup$
    – kelalaka
    Nov 20 at 18:31
  • $\begingroup$ The passwords are stored encrypted. But when I use my cipher function, I can't tell if it's decrypted to the right plaintext. If I call the function with a wrong master key, it will still give an output. I am hashing the plaintext so that I can tell if the right key was entered. After that, I'll show the UI with the plaintext, else I would tell the user that he entered the wrong master key. $\endgroup$
    – Luqus
    Nov 20 at 19:48
  • $\begingroup$ I am not trying to replicate the best password manager, I want to find a way for me that has no big security flaws. I am by the way not hashing the password, but the plaintext as stated in my question. $\endgroup$
    – Luqus
    Nov 21 at 13:02
1
$\begingroup$

A long plaintext doesn't mean it can not be guessed. If the hash of the plaintext is available to an attacker such an attacker can efficently verify any guess he may have as to the plain text.

In many real world scenarios we encrypt plain text with limited entropy, the text may be large but still have little information not known to the attacker. And with a simple efficient hash, even trying millions of guesses for the plaintext can be very practical.

There are better ways of authenticating encryption. We can do encrypt then mac, we can use combined methods like GCM. Both are better than a plain text hash.

$\endgroup$
1
$\begingroup$

As others indicated, this would not be a scheme that would conform to best practice. Considering that you are asking this question I would strongly suggest to use your code for practice only.

In principle you can hash data and then encrypt it. As the hash remains confidential until the key is found, this is considered when it comes to protecting the hash and the plaintext message. However, please note that hash-then-encrypt itself may not be secure; we generally prefer methods such as encrypt-then-MAC. Personally I would try and use a Password Based Key Derivation Function and an authenticated mode of encryption such as EAX / encrypt-then-HMAC (also taking care of the IV).

It is also a good idea to store some key check value (e.g. a MAC over some known data) next to it. That way you don't have the problem that you need to try and decrypt all of your database before you can check if your password is correct. Note that an adversary can validate if the master password is correct by simply decrypting a few blocks of data, so this won't give much if any advantage to a would-be attacker, while adding to the user experience.

$\endgroup$
3
  • $\begingroup$ I don't try to hash my data and encrypt it then, I am trying to tell if a decryption is correct. That is the only reason I am calculating the hash; to tell if the user entered the right master key. $\endgroup$
    – Luqus
    Nov 20 at 19:51
  • $\begingroup$ I added a graphic to my question that shows, what I would like to achieve. $\endgroup$
    – Luqus
    Nov 20 at 19:58
  • 1
    $\begingroup$ It's still the scheme that I'm describing. If you don't want to verify the data for integrity then you don't need to hash it in the first place. To be honest, I would like to know if somebody had screwed with the database. $\endgroup$
    – Maarten Bodewes
    Nov 20 at 20:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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