public class KeyWrapper extends Object implements Serializable
Random.salt()). The salt
adds randomness to the process so that two people using the same password
will still get different keys.
This does means you'll need to store the salt value for each person and use
it each time in order to ensure you can regenerate the key. This is
considered acceptable as the salt is not a particularly sensitive value - all
it does is add a little randomness.
The cryptographic algorithms used in this class are those indicated in the
"Beginning Cryptography with Java" book. See:
http://p2p.wrox.com/book-beginning
-cryptography-java/67710-wrapping-rsa-keys.html| Modifier and Type | Field and Description |
|---|---|
static int |
PBKD_ITERATIONS
The number of iterations to perform when doing the password-based key
derivation to generate the wrapping key: 1024.
|
| Constructor and Description |
|---|
KeyWrapper(SecretKey wrapKey)
This constructor sets the wrap key directly, rather than generating it
from a password and salt as
KeyWrapper(String, String) does. |
KeyWrapper(String password,
String salt)
This is the constructor you should typically use.
|
| Modifier and Type | Method and Description |
|---|---|
static PublicKey |
decodePublicKey(String encodedKey)
Decodes the given encoded
PublicKey. |
static String |
encodePublicKey(PublicKey key)
Encodes the given
PublicKey without wrapping. |
PrivateKey |
unwrapPrivateKey(String wrappedKey)
Unwraps the given encoded
PrivateKey, using
WRAP_ALGORITHM_ASYMMETRIC. |
SecretKey |
unwrapSecretKey(String wrappedKey)
Unwraps the given encoded
SecretKey, using
WRAP_ALGORITHM_SYMMETRIC. |
String |
wrapPrivateKey(PrivateKey key)
Wraps the given
SecretKey using
. |
String |
wrapSecretKey(SecretKey key)
Wraps the given
SecretKey using
. |
public static final int PBKD_ITERATIONS
public KeyWrapper(String password, String salt)
password - The password to use as the basis for wrapping keys.salt - A value for this can be obtained from
Random.salt(). You need to store a salt value
for each password and ensure the matching one is passed in
each time this constructor is invoked.public KeyWrapper(SecretKey wrapKey)
KeyWrapper(String, String) does.
The wrap key must be an WRAP_KEY_ALGORITHM key. Both
Keys.newSecretKey() and
Keys.generateSecretKey(String, String) can be used to generate
the right type of key.wrapKey - The key which will be used for wrapping other keys.public String wrapSecretKey(SecretKey key)
SecretKey using
.key - The SecretKey to be wrapped. This method internally
just calls wrap(Key, String), but this provides a
clear naming match with unwrapSecretKey(String).SecretKey, for ease of storage.public String wrapPrivateKey(PrivateKey key)
SecretKey using
.key - The PrivateKey to be wrapped. This method internally
just calls wrap(Key, String), but this provides a
clear naming match with unwrapPrivateKey(String).PrivateKey, for ease of storage.public static String encodePublicKey(PublicKey key)
PublicKey without wrapping. Since a
public key is public, this is a convenience method provided to convert it
to a String for unprotected storage.
This method internally calls ByteArray.toBase64String(byte[]),
passing the value of Key.getEncoded().public SecretKey unwrapSecretKey(String wrappedKey)
SecretKey, using
WRAP_ALGORITHM_SYMMETRIC.wrappedKey - The wrapped key, base-64 encoded, as returned by
wrapSecretKey(SecretKey) .SecretKey.public PrivateKey unwrapPrivateKey(String wrappedKey)
PrivateKey, using
WRAP_ALGORITHM_ASYMMETRIC.wrappedKey - The wrapped key, base-64 encoded, as returned by
wrapPrivateKey(PrivateKey) .PrivateKey.public static PublicKey decodePublicKey(String encodedKey)
PublicKey.
See:
http://stackoverflow.com/questions/2411096/how-to-recover-a-rsa-public
-key-from-a-byte- arrayencodedKey - The public key, base-64 encoded, as returned by
encodePublicKey(PublicKey) .PublicKey.Copyright © 2015 Carboni. All rights reserved.