Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

key based on password #161

Closed
robstoll opened this issue Jan 30, 2016 · 11 comments
Closed

key based on password #161

robstoll opened this issue Jan 30, 2016 · 11 comments
Labels

Comments

@robstoll
Copy link
Contributor

Hi,

I have the following scenario:

  • data is saved encrypted in DB
  • key for encryption must not be saved directly on the system (in clear text)
  • yet, application needs the key to decrypt the data
  • hence key shall be saved encrypted based on the users password

therefore I would like to know, how can I generate a key passed on a users password?

Cheers,
Robert

@sarciszewski
Copy link
Contributor

hash_pbkdf2().

@robstoll robstoll changed the title key passed on password key based on password Jan 30, 2016
@robstoll
Copy link
Contributor Author

thank you, and is it possible to use the generated key (with hash_pdkdf2) in Crypto::encrypt() somehow?

@sarciszewski
Copy link
Contributor

$key = hash_pbkdf2(
    'sha256',
    $YOUR_USERS_PASSWORD_HERE,
    $A_HARD_CODED_PASSWORD_SALT_MADE_OF_16_OR_MORE_RANDOM_BYTES_UNIQUE_PER_USER,
    100000, // minimum 86000
    16,
    true
);

$cipher = Crypto::encrypt($message, $key);
$plain = Crypto::decrypt($cipher, $key);

var_dump($message === $cipher); // bool(true)

@sarciszewski
Copy link
Contributor

Also, you want to use a CSPRNG for the salt. Crypto::createNewRandomKey() returns 16 bytes. That should be acceptable. Store that long-term per user.

@robstoll
Copy link
Contributor Author

Thanks for the quick reply. I should have mentioned that I am using version 2.x of your library. Crypto::encrypt incorporates the following check:

if (!is_a($key, "\Defuse\Crypto\Key")) {
            throw new Ex\CannotPerformOperationException(
                "The given key is not a valid Key object."
            );
        }

Hence I need to transform the key generated by hash_pbkdf2 into a \Defuse\Crypto\Key somehow.
Any idea?

@sstok
Copy link
Contributor

sstok commented Jan 31, 2016

Use Key::saveToAsciiSafeString to convert to the Key object to a string for storing, then use Key::LoadFromAsciiSafeString to convert the stored string back to a Key object.

@robstoll
Copy link
Contributor Author

@sstok I saw this functionality but it does not help me since I generate the key with hash_pbkdf2 which returns a string (or raw binary data).

robstoll added a commit to robstoll/php-encryption that referenced this issue Jan 31, 2016
added the method CreateKeyBasedOnPassword which uses hash_pbkdf2 to generate a key.
For a use case, see defuse#161
@robstoll
Copy link
Contributor Author

For now I am using the method I added in pull request #163
Please let me know if there is a better way to do this without breaking backward compatibility

@pnowosie
Copy link

pnowosie commented Feb 1, 2016

What you need is a long planed feature #5, not implemented yet in v2.0. Maybe your PR will push things forward a little.

@defuse defuse added the support label Feb 13, 2016
@SkynetHome
Copy link

I am looking to use this library in a project I am working on but am stuck on the proper storage and usage of keys as well. I have scripts receiving strings of data from users on Android and iOS and I need to encrypt that data and store it in mysql. The data submitted by one user needs to be available to certain other users so I don't think encrypting with a users password will work. Most importantly the data needs to be absolutely secure and absolutely unreadable by me the only DBA. What would be the best approach for storing a key so that PHP can encrypt and decrypt POST and GET requests as needed without giving me any possible access to the data stored on my server.

@defuse
Copy link
Owner

defuse commented Apr 3, 2016

Revisit this when rewriting documentation #52

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants