AndreaF
AndreaF

Reputation: 12395

How to generate a javax.crypto.SecretKey from a String

The follow line allow me to generate a SecretKey

 SecretKey key = KeyGenerator.getInstance("DES").generateKey();

But I want to generate a SecretKey related to a specific String.

For example

String myKeyStr="abcde";

SecretKey mykey2=keyGeneratedFrom(myKeyStr);

Obviously the SecretKey generation should have a 1:1 link to avoid issue during the decryption.

I have no idea of how to solve this problem.

Could you help me?

Upvotes: 0

Views: 2560

Answers (1)

Maarten Bodewes
Maarten Bodewes

Reputation: 94038

Passwords are not keys. You have to use something like PBKDF2 to derive a key from a password. Even then, it pays to have secure passwords. There is a lot of information about PBKDF2 when used in Java, and the Bouncy Castle library can help too.

Etcetera.

Don't forget to use a random salt.

Upvotes: 1

Related Questions