I'd like to encrypt some private data by every user and I was wondering if I could use the same secret as passphrase for an RSA private key and as password for authentication. How secure is using an encrypted RSA private key compared to blowfish password hashing?

  • 3
    "the same passphrase" - the same as what? Using an RSA key to do what? You appear to be talking about comparing things, but it is not clear to me what you are comparing, or what effect you expect to have. – schroeder Oct 30 '17 at 16:27
  • @schroeder - I think OP is suggesting using a key derivation algorithm to generate an RSA key for the user. Then re-generate the key on login and compare to stored key to auth the user. – Hector Oct 30 '17 at 16:34
  • RSA is used for asymetric encryption or to make signatures (or both, only if two different keys are used). Password hashing does not encrypt, it is used to make it more difficult to retrieve a password for it's stored value in case of a compromise of a database. They are two mechanisms with different purposes, that work on different inputs. The way you formulated your question does not make sense. – A. Hersean Oct 30 '17 at 16:37
  • @Hector Maybe, maybe not. Moreover, I do not know of a PBKDF that can generate a pair of RSA keys. – A. Hersean Oct 30 '17 at 16:39
  • @A.Hersean - good point. It can be done (You use the derived data as a seed for a PRNG) but isn't common / is hard to get right. Most common place i've seen it is Bitcoin clients like electrum. – Hector Oct 30 '17 at 16:43

This is not secure. If you store the key you enable anyone with database access the ability to decrypt the user data.

You should not store the private key. Instead generate it on demand using the password + salt whenever needed. If entering the password every time encryption/decryption is required is not feasible for your use case then cache it in memory and clearing it when the user logs out.

RSA is slow and asymmetric. It also (as pointed out by A. Hersean) doesn't play well with key derivation algorithms. I would suggest you should probably be using a symmetric algorithm like AES for encrypting user data.

  • By RSA you can encrypt the private key (usually with DES/3DES in legacy libs), so only those can decrypt the data who know the passphrase. The public key is for encrypting, so that can be stored too without any risk. – inf3rno Oct 30 '17 at 20:40
  • The only problem that everything is lost if somebody requests a password reset. I have to think about this much more... Thanks for the AES tip, btw. – inf3rno Oct 30 '17 at 20:46
  • Whilst you are technically correct in that you could regenerate and compare public keys after decrypting the private key this is a lot more expensive in terms of CPU cycles and would require the private key to be in memory a lot more of the time. The storage for the password hash would likely be notably cheaper and avoid the pitfalls of rolling your own. – Hector Oct 31 '17 at 8:40

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.