public class Keys extends Object
symmetricKeySize, based on a passwordsymmetricKeySizePassword.hash(String)) the key can only be regenerated using the correct password. Bear
in mind however that if the user changes (or resets) their password this will result in a
different key, so you'll need a plan for recovering data encrypted with the old key and
re-encrypting it with the new one.
Random keys: these are simple to generate, but need to be stored because it's
effectively impossible to regenerate the key. To store a key you should use
KeyWrapper.wrapSecretKey(SecretKey). This produces an encrypted version of the key which
can safely be stored in, for example, a database or properties file. The benefit of the
KeyWrapper approach is that when a user changes their password you'll only need to
re-encrypt the stored keys using a KeyWrapper initialised with the new password, rather
than have to re-encrypt all data encrypted with the key.
In both cases when a user changes their password you will have the old and the new plaintext
passwords, meaning you can decrypt with the old an re-encrypt with the new. The difficulty comes
when you need to reset a password, because it's not possible to recover the old password. In this
case you either need a secondary password, such as a security question, or you need to be clear
that data cannot be recovered. Whatever your solution, remember that storing someone's password
in any recoverable form is a clear security no-no, so you'll need to put some thought into the
recovery process.| Modifier and Type | Field and Description |
|---|---|
static String |
ASYMMETRIC_ALGORITHM
The asymmetric encryption algorithm: "RSA".
|
static int |
ASYMMETRIC_KEY_SIZE
The key size for asymmetric keys: 3072.
|
static String |
SYMMETRIC_ALGORITHM
The symmetric encryption algorithm: "AES".
|
static int |
SYMMETRIC_KEY_SIZE_STANDARD
By default, the JVM will only allow "AES" up to
128 bit keys.
|
static int |
SYMMETRIC_KEY_SIZE_UNLIMITED
If the "Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files" are
correctly installed for your JVM, it's possible to use 256
bit keys.
|
static String |
SYMMETRIC_PASSWORD_ALGORITHM
The algorithm to use to generate password-based secret keys:
"PBKDF2WithHmacSHA1".
|
static int |
SYMMETRIC_PASSWORD_ITERATIONS
The number of iterations to use for password-based key derivation:
1024.
|
| Constructor and Description |
|---|
Keys() |
| Modifier and Type | Method and Description |
|---|---|
static boolean |
canUseStrongKeys()
Tests whether the
"Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files" are
correctly installed.
|
static SecretKey |
generateSecretKey(String password,
String salt)
This method generates a new secret (or symmetric) key for the "AES"
algorithm, using the given password and salt values.
|
static int |
getSymmetricKeySize() |
static KeyPair |
newKeyPair()
|
static SecretKey |
newSecretKey()
This method generates a new secret (or symmetric) key for the "AES"
algorithm with a key size of
symmetricKeySize bits. |
static void |
setSymmetricKeySize(int newSymmetricKeySize)
Sets the key size for symmetric keys.
|
public static final String SYMMETRIC_ALGORITHM
public static final int SYMMETRIC_KEY_SIZE_STANDARD
public static final int SYMMETRIC_KEY_SIZE_UNLIMITED
setSymmetricKeySize(int) method to enable
unlimited-strength cryptography.public static final String SYMMETRIC_PASSWORD_ALGORITHM
public static final int SYMMETRIC_PASSWORD_ITERATIONS
public static final String ASYMMETRIC_ALGORITHM
public static final int ASYMMETRIC_KEY_SIZE
public static SecretKey newSecretKey()
symmetricKeySize bits.SecretKey.public static SecretKey generateSecretKey(String password, String salt)
password - The starting point to use in generating the key. This can be a password, or any
suitably secret string. It's worth noting that, if a user's plaintext password is
used, this makes key derivation secure, but means the key can never be recovered
if a user forgets their password. If a different value, such as a password hash is
used, this is not really secure, but does mean the key can be recovered if a user
forgets their password. It's a trade-off, right?salt - A value for this parameter can be generated by calling
Random.salt(). You'll need to store the salt value (this is ok to
do because salt isn't particularly sensitive) and use the same salt each time in
order to always generate the same key. Using salt is good practice as it ensures
that keys generated from the same password will be different - i.e. if two users
use the password "password", having a salt value avoids the generated keys being
identical which, for example, might give away someone's password.SecretKey, defined by the given password and saltpublic static KeyPair newKeyPair()
KeyPair.public static int getSymmetricKeySize()
public static void setSymmetricKeySize(int newSymmetricKeySize)
SYMMETRIC_KEY_SIZE_STANDARD) but can be changed to
256 bit by setting this field using the
SYMMETRIC_KEY_SIZE_UNLIMITED constant.
Note that whilst it's possible to generate a 256 bit key
in any environment, you will need the
"Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files" installed in
your JVM in order to use it. To test this, you can use the canUseStrongKeys()
method.newSymmetricKeySize - the symmetricKeySize to setpublic static boolean canUseStrongKeys()
Copyright © 2015 Carboni. All rights reserved.