public class RSAOaepKeyPairGeneratorFIPS extends RSAKeyPairGeneratorFIPS implements OaepKeyPairGenerator
The modulus of the generated KeyPair always has either 1024, 2048 or 3072
bits. If no initialize()
method of this key pair generator is
called, it defaults to initialize(2048).
The algorithm used for key generation was published in FIPS 186-3.
RSAES-OAEP keys (RSAOaepPublicKey
,
RSAOaepPrivateKey
) differ from
ordinary PKCS#1 RSA keys (RSAPublicKey
, RSAPrivateKey
) only in that
RSAES-OAEP keys may contain OAEP parameters
. If RSAES-OAEP keys do not contain parameters they may be used
for OAEP based en/decryption with any OAEP parameters. However, if RSAES-OAEP
keys contain OAEP parameters they must be only used with the hash algorithm
and mask generation function that are specified by their parameters (see RFC
4055):
RSAES-OAEP-params ::= SEQUENCE { hashFunc [0] AlgorithmIdentifier DEFAULT sha1Identifier, maskGenFunc [1] AlgorithmIdentifier DEFAULT mgf1SHA1Identifier, pSourceFunc [2] AlgorithmIdentifier DEFAULT pSpecifiedEmptyIdentifier } pSpecifiedEmptyIdentifier AlgorithmIdentifier ::= { id-pSpecified, nullOctetString } nullOctetString OCTET STRING (SIZE (0)) ::= { ''H }
An application wishing to create a RSAES-OAEP key pair to be used for OAEP
based encryption with the RSA algorithm, uses a proper
getInstance
method of the
java.security.KeyPairGenerator
class, which subsequently maybe
casted to RSAOaepKeyPairGeneratorFIPS
for performing an algorithm-specific
initialization with proper RSAES-OAEP parameters. If an algorithm-specific
initialization is not required, the cast to
RSAOaepKeyPairGeneratorFIPS
can be omitted (in this case no
parameters will be included in the RSA-OAEP-FIPS-186-3 generated keys and
they maybe used with any OAEP parameters).
Generally four steps have to be performed for creating a RSAES-OAEP-FIPS-186-3 KeyPair by using a proper KeyPairGenerator:
KeyPairGenerator
has to be instantiated thereby
specifying "RSA-OAEP-FIPS-186-3" as algorithm name: KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSAES-OAEP-FIPS-186-3", "IAIK");
initialize
method. For initializing the generator to
create keys with a modulus length of, e.g., 2048 bits, this can be explicitly
specified (when not initializing the generator explicitly, per default the
modulus length is set to 2048 bits): keyGen.initialize(2048);
generateKeyPair()
:
KeyPair keyPair = keyGen.generateKeyPair();
RSAOaepPrivateKey privateKey = (RSAOaepPrivateKey) keyPair.getPrivate(); RSAOaepPublicKey publicKey = (RSAOaepPublicKey) keyPair.getPublic();
For performing an algorithm-specific initialization with particular RSA OAEP
parameters an explicit cast of the KeyPairGenerator may be necessary for
obtaining a specific RSAOaepKeyPairGeneratorFIPS
to be initialized with the desired RSA-OAEP
parameters which have to be supplied as
RSAOaepParameterSpec
object,
e.g.:
RSAOaepKeyPairGeneratorFIPS rsaOaepkeyGen = (RSAOaepKeyPairGeneratorFIPS) keyGen; // create OAEP parameters for specifying hash, mgf and pSource algorithms: // hash, mgf and pSource algorithm ids AlgorithmID hashID = (AlgorithmID) AlgorithmID.sha512.clone(); AlgorithmID mgfID = (AlgorithmID) AlgorithmID.mgf1.clone(); mgfID.setParameter(hashID.toASN1Object()); AlgorithmID pSourceID = (AlgorithmID) AlgorithmID.pSpecified.clone(); pSourceID.setParameter(new OCTET_STRING()); // hash and mgf engines MessageDigest hashEngine = hashID.getMessageDigestInstance(); MaskGenerationAlgorithm mgfEngine = mgfID.getMaskGenerationAlgorithmInstance(); MGF1ParameterSpec mgf1ParamSpec = new MGF1ParameterSpec(hashID); mgf1ParamSpec.setHashEngine(hashEngine); mgfEngine.setParameters(mgf1ParamSpec); // create the RSAOaepParameterSpec RSAOaepParameterSpec oaepParamSpec = new RSAOaepParameterSpec(hashID, mgfID, pSourceID); // set engines oaepParamSpec.setHashEngine(hashEngine); oaepParamSpec.setMGFEngine(mgfEngine); // initialize key pair generator rsaOaepkeyGen.initialize(2048, oaepParamSpec); KeyPair keyPair = rsaOaepkeyGen.generateKeyPair(); RSAOaepPublicKey publicKey = (RSAOaepPublicKey) keyPair.getPublic(); RSAOaepPrivateKey privateKey = (RSAOaepPrivateKey) keyPair.getPrivate();
RSAES-OAEP keys must be only used for en/decryption purposes with the
RSAES-OAEP-FIPS-186-3 encryption scheme. For using a
RSAOaepPublicKey
with a
Cipher
engine, "OAEP-FIPS-186-3" has to be specified as padding
scheme when instantiating the Cipher
object:
Cipher rsaOaep = Cipher.getInstance("RSA/ECB/OAEP-FIPS-186-3", "IAIK");When now initializing the Cipher with an RSAES-OAEP-FIPS-186-3 key that contains OAEP parameters, the hash algorithm, mask generation function and pSource algorithm are taken from the public key parameters:
rsaOaep.init(Cipher.ENCRYPT_MODE, publicKey); // the data to be encrypted (e.g. secret key material): byte[] data = ...; // encrypt data byte[] encrypted = rsaOaep.doFinal(data);For decrypting the encrypted data you will have to use the right RSAES-OAEP
RSAOaepPrivateKey
:
Cipher rsaOaep = Cipher.getInstance("RSA/ECB/OAEP", "IAIK"); rsaOaep.init(Cipher.DECRYPT_MODE, privateKey); // decrypt data byte[] decrypted = rsaOaep.doFinal(encrypted);
RSAOaepPublicKey
,
RSAOaepPrivateKey
,
RSAOaepKeyFactory
,
RSAOaepParameterSpec
,
KeyPairGenerator
,
KeyPair
KEYLENGTH_1024, KEYLENGTH_2048, KEYLENGTH_3072, LOWER_PUBLIC_EXPONENT_BOUND, UPPER_PUBLIC_EXPONENT_BOUND
initialized, keylen, public_exponent, random
Constructor and Description |
---|
RSAOaepKeyPairGeneratorFIPS()
Default constructor for creating a RSAOaepKeyPairGeneratorFIPS object.
|
Modifier and Type | Method and Description |
---|---|
void |
initialize(java.security.spec.AlgorithmParameterSpec params)
Initializes this RSAOaepKeyPairGeneratorFIPS with the given
RSAOaepParameterSpec.
|
void |
initialize(java.security.spec.AlgorithmParameterSpec params,
java.security.SecureRandom random)
Initializes this RSAOaepKeyPairGeneratorFIPS with given
RSAOaepParameterSpec and random number generator.
|
void |
initialize(int strength,
java.security.spec.AlgorithmParameterSpec params)
Initializes the RSAOaepKeyPairGeneratorFIPS for generating keys with the
given length and OAEP parameters.
|
void |
initialize(int strength,
java.security.spec.AlgorithmParameterSpec params,
java.security.SecureRandom secureRandom)
Initializes the RSAOaepKeyPairGeneratorFIPS for a certain key length with
the given random number generator and OAEP parameters.
|
void |
initialize(int strength,
java.math.BigInteger publicExponent,
java.security.spec.AlgorithmParameterSpec params,
java.security.SecureRandom secureRandom)
Initializes the key pair generator using the specified "strength" (desired
key length in bits), public exponent, source of random bits, and OAEP
parameters.
|
generateKeyPair, initialize, initialize, initialize
public RSAOaepKeyPairGeneratorFIPS()
KeyPairGenerator.getInstance
method:
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSAES-OAEP-FIPS-186-3", "IAIK");
KeyPairGenerator
public void initialize(java.security.spec.AlgorithmParameterSpec params) throws java.security.InvalidAlgorithmParameterException
initialize
methods may be called during the initialization
process, e.g.:
int strength = ...; RSAOaepParameterSpec oaepParamSpec = ...; KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSAES-OAEP-FIPS-186-3", "IAIK"); keyGen.initialize(strength); keyGen.initialize(oaepParamSpec); ...
initialize
in interface OaepKeyPairGenerator
initialize
in class RSAKeyPairGenerator
params
- the RSAOaepParameterSpec for initializing this generatorjava.security.InvalidAlgorithmParameterException
- if the given parameter specification is not a
RSAOaepParameterSpecpublic void initialize(java.security.spec.AlgorithmParameterSpec params, java.security.SecureRandom random) throws java.security.InvalidAlgorithmParameterException
initialize
methods
may be called during the initialization process, e.g.:
int strength = ...; RSAOaepParameterSpec oaepParamSpec = ...; SecureRandom secureRandom = ...; KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSAES-OAEP-FIPS-186-3", "IAIK"); keyGen.initialize(strength); keyGen.initialize(oaepParamSpec, secureRandom); ...
initialize
in interface OaepKeyPairGenerator
initialize
in class RSAKeyPairGenerator
params
- the RSAOaepParameterSpec for initializing this generatorrandom
- the SecureRandom for generating random numbersjava.security.InvalidAlgorithmParameterException
- if the given parameter specification is not a
RSAOaepParameterSpecpublic void initialize(int strength, java.security.spec.AlgorithmParameterSpec params) throws java.security.InvalidParameterException
KeyPairGenerator
to
RSAOaepKeyPairGeneratorFIPS
is required:
RSAOaepKeyPairGeneratorFIPS keyGen = (RSAOaepKeyPairGeneratorFIPS)KeyPairGenerator.getInstance("RSAES-OAEP-FIPS-186-3", "IAIK"); keyGen.initialize(strength, oaepParamSpec); ...
initialize
in interface OaepKeyPairGenerator
strength
- the length of the key in bits.params
- the OAEP parameters to be set for the RSAES-OAEP keysjava.security.InvalidParameterException
- if the supplied parameters do not represent OAEP
parameters
public void initialize(int strength, java.security.spec.AlgorithmParameterSpec params, java.security.SecureRandom secureRandom) throws java.security.InvalidParameterException
KeyPairGenerator
to RSAOaepKeyPairGeneratorFIPS
is required:
RSAOaepKeyPairGeneratorFIPS keyGen = (RSAOaepKeyPairGeneratorFIPS)KeyPairGenerator.getInstance("RSAES-OAEP-FIPS-186-3", "IAIK"); keyGen.initialize(strength, oaepParamSpec, secureRandom); ...
initialize
in interface OaepKeyPairGenerator
strength
- the length of the key in bits.params
- the OAEP parameters to be set for the RSAES-OAEP keyssecureRandom
- the random number generatorjava.security.InvalidParameterException
- if the supplied parameters do not represent OAEP
parameters
public void initialize(int strength, java.math.BigInteger publicExponent, java.security.spec.AlgorithmParameterSpec params, java.security.SecureRandom secureRandom) throws java.security.InvalidParameterException
KeyPairGenerator
to RSAOaepKeyPairGeneratorFIPS
is required:
RSAOaepKeyPairGeneratorFIPS keyGen = (RSAOaepKeyPairGeneratorFIPS)KeyPairGenerator.getInstance("RSAES-OAEP-FIPS-186-3", "IAIK"); keyGen.initialize(strength, publicExponent, oaepParamSpec, secureRandom); ...
initialize
in interface OaepKeyPairGenerator
strength
- keyLength the length of the key in bits.publicExponent
- the public exponentparams
- the OAEP parameters to be set for the RSAES-OAEP keyssecureRandom
- the random seedjava.security.InvalidParameterException
- if the supplied parameters do not represent OAEP
parameters