I have a feature request to connect my PHP web-app to the users mail server. I have usually strayed away from this type of work as it will mean storing the users email passwords.

My question: is it secure to store a users third party email password, if the email password is encrypted using PHP's openssl functionality, of which the private key will be some hashed version of the users login password to the PHP application. (Users login passwords will be hashed using a different algorithm for authentication purposes.) The plaintext password to the web-app will not be stored in the server.

Upon user login, we can use the plain text password to access the private key to the users encrypted email password. This could then be re-encrypted using openSSL, with a daily refreshed server key.

The result (or part thereof) of that encryption, could be stored as a cookie in the users browser, only accessible every time the user loads a page in the web-app.

Is this method effective for ensuring the security of the third party email passwords?

1 Answer 1


It's hard to tell from how you worded it, but it sounds like there may be a few areas that can be improved. You don't want they key to be derived from some hashed version of the password, but rather want to use a secure key derivation function to derive an encryption key for the user's keyring.

Derived keys have limited security, so you want to limit how much you encrypt with the derived key. My recommendation is to produce a truly random encryption key to use for the user's e-mail credentials. That key (symmetric) is then encrypted with the derived key from the user's password and stored in the database. When the user logs in, their password should be used to determine their derived key and the data key should be decrypted and stored in the user's session. When the user needs to access the e-mail configuration, the key is available to be used for decryption, however as soon as the session ends, the credentials will be cleared from memory and be unavailable until the user re-enters their password.

The use of a separate key for encrypting the e-mail credentials also means that you can use asymmetric cryptography to encrypt the configuration key so that it can be used by a separate server for sending e-mails when the user is offline (if you end up needing that feature). Since this e-mail server could be isolated from input from the outside world, it would have a much higher degree of security and the private key wouldn't be available to the outside world so it would be very difficult to compromise the account details.

  • Thanks for your answer! Very clear and concise. I never was good at wording questions, but seeing as you understand where I'm going from, please edit the question if you feel it needs it so others will understand. Thanks again for your detailed answer. It is much appreciated. Commented Feb 18, 2014 at 20:45

You must log in to answer this question.

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