public class RSAOaepPrivateKey extends RSAPrivateKey
iaik.pkcs.pkcs8.PrivateKeyInfo
for supporting the PKCS#8 Private
Key Information Standard for RSA-OAEP private keys. This class
implements the java.security.interfaces.RSAPrivateKey
interface for providing the functionality of a private key, as used
for decryption based on the RSAES-OAEP encryption scheme as specified
by PKCS#1v2.1.
The ASN.1 object identifier for RSAES-OAEP key derived from PKCS-1:
pkcs-1 OBJECT IDENTIFIER ::= { iso(1) member-body(2) us(840) rsadsi(113549) pkcs(1) 1 } id-RSAES-OAEP OBJECT IDENTIFIER ::= { pkcs-1 7 }This corresponds to the OID string "1.2.840.113549.1.1.7".
A RSAES-OAEP private key differs from an ordinary PKCS#1 RSA private key
only in that a RSAES-OAEP key may
contain OAEP parameters
;
modulus
, privateExponent
and CRT (Chinese
Remainder Theorem) fields are encoded in the same way as ASN.1
SEQUENCE according to PKCS#1:
If a private RSAES-OAEP key does not contain parameters it may be used for OAEP based decryption with any OAEP parameters. However, if a RSAES-OAEP key contains OAEP parameters it must be only used with the hash algorithm and mask generation function that are specified by its parameters (see RFC 4055):RSAPrivateKey ::= SEQUENCE { version Version, -- a INTEGER version number; 0 for this standard modulus INTEGER, -- n publicExponent INTEGER, -- e privateExponent INTEGER, -- d prime1 INTEGER, -- primeP (p) (first prime factor of n) prime2 INTEGER, -- primeQ (q) (second prime factor of n) exponent1 INTEGER, -- primeExponentP: d mod (p - 1) exponent2 INTEGER, -- primeExponentQ: d mod (q - 1) crtCoefficient INTEGER -- Chinese Remainder Theorem ((inverse of q) mod p) }
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 RSAOaepPrivateKey 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
RSAOaepKeyPairGenerator
for performing an algorithm-specific initialization with proper RSAES-OAEP parameters. If
an algorithm-specific initialization is not required, the cast to RSAOaepKeyPairGenerator
can be omitted (in this case no parameters will be included in the RSA-OAEP generated keys and
they maybe used with any OAEP parameters).
Generally four steps have to be performed for creating a RSAOaepPrivateKey by using a proper KeyPairGenerator:
KeyPairGenerator
has to be instantiated thereby specifying
"RSAES-OAEP" as algorithm name:
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSAES-OAEP", "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
RSAOaepKeyPairGenerator
to be initialized
with the desired RSA-OAEP parameters which have to be supplied as
RSAOaepParameterSpec
object, e.g.:
RSAOaepKeyPairGenerator rsaOaepkeyGen = (RSAOaepKeyPairGenerator)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
encryption scheme. For using a RSAOaepPublicKey
with a Cipher
engine, "OAEP" has to be specified
as padding scheme when instantiating the Cipher
object:
Cipher rsaOaep = Cipher.getInstance("RSA/ECB/OAEP", "IAIK");When now initializing the Cipher with an RSAES-OAEP 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);
PrivateKeyInfo
,
RSAPrivateKey
,
RSAOaepPublicKey
,
RSAOaepKeyPairGenerator
,
RSAOaepKeyFactory
,
RSAOaepParameterSpec
,
RSACipher
,
RSAPrivateKey
,
KeyPairGenerator
,
KeyPair
,
Serialized Formprivate_key_algorithm
Constructor and Description |
---|
RSAOaepPrivateKey(ASN1Object obj)
Creates a new RSAOaepPrivateKey key from an ASN1Object.
|
RSAOaepPrivateKey(java.math.BigInteger modulus,
java.math.BigInteger privateExponent)
Creates a new RSAOaepPrivate key from given modulus and private exponent.
|
RSAOaepPrivateKey(java.math.BigInteger modulus,
java.math.BigInteger privateExponent,
java.security.spec.AlgorithmParameterSpec oaepParams)
Creates a new RSAOaepPrivate key from given modulus and private exponent
and OAEP parameters.
|
RSAOaepPrivateKey(java.math.BigInteger modulus,
java.math.BigInteger publicExponent,
java.math.BigInteger privateExponent,
java.math.BigInteger primeP,
java.math.BigInteger primeQ,
java.math.BigInteger primeExponentP,
java.math.BigInteger primeExponentQ,
java.math.BigInteger crtCoefficient)
Creates a RSAOaepPrivateKey from the given values.
|
RSAOaepPrivateKey(java.math.BigInteger modulus,
java.math.BigInteger publicExponent,
java.math.BigInteger privateExponent,
java.math.BigInteger primeP,
java.math.BigInteger primeQ,
java.math.BigInteger primeExponentP,
java.math.BigInteger primeExponentQ,
java.math.BigInteger crtCoefficient,
java.security.spec.AlgorithmParameterSpec oaepParams)
Creates a RSAOaepPrivateKey from the given values and
OAEP parameters.
|
RSAOaepPrivateKey(byte[] pk)
Creates a new RSAOaepPrivateKey from a DER encoded ASN.1 data structure.
|
RSAOaepPrivateKey(java.io.InputStream is)
Creates a new RSAOaepPrivateKey from an InputStream.
|
Modifier and Type | Method and Description |
---|---|
protected void |
decode(byte[] privateKey)
Decodes a DER encoded RSAOaepPrivateKey (PKCS#1).
|
boolean |
equals(java.lang.Object obj)
Compares this RSAOaepPrivateKey object with the supplied object.
|
java.lang.String |
getAlgorithm()
Returns the name of the key algorithm.
|
java.security.spec.AlgorithmParameterSpec |
getParams()
Gets the OAEP parameters from this RSA-OAEP key (if included).
|
java.security.PublicKey |
getPublicKey()
Returns the public parts (modulus
n and public exponent e
of this private key. |
int |
hashCode()
Returns a hash code for this object.
|
java.lang.String |
toString()
Returns a string that represents the contents of this RSA OAEP private key.
|
boolean |
validateParameters(java.security.spec.AlgorithmParameterSpec rsaOaepParamSpec)
Validates the given parameters for this OAEP key.
|
crypt, encode, getCrtCoefficient, getModulus, getPrimeExponentP, getPrimeExponentQ, getPrimeP, getPrimeQ, getPrivateExponent, getPublicExponent, parse
clone, createPrivateKeyInfo, decode, getAlgorithmID, getAttributes, getEncoded, getFormat, getPrivateKey, getPrivateKey, getPrivateKey, getPrivateKey, getPrivateKey, getPrivateKey, getPubKey, setAttributes, setPubKey, toASN1Object, writeTo
public RSAOaepPrivateKey(java.math.BigInteger modulus, java.math.BigInteger privateExponent)
privateExponent
- the private exponent d
modulus
- the modulus n
public RSAOaepPrivateKey(java.math.BigInteger modulus, java.math.BigInteger privateExponent, java.security.spec.AlgorithmParameterSpec oaepParams) throws java.security.spec.InvalidParameterSpecException
privateExponent
- the private exponent d
modulus
- the modulus n
oaepParams
- the OAEP parameters (hash-, mgf- and psource-algorithm)java.security.spec.InvalidParameterSpecException
- if the parameters are not specified
as RSAOaepParameterSpec
object or are invalidpublic RSAOaepPrivateKey(java.math.BigInteger modulus, java.math.BigInteger publicExponent, java.math.BigInteger privateExponent, java.math.BigInteger primeP, java.math.BigInteger primeQ, java.math.BigInteger primeExponentP, java.math.BigInteger primeExponentQ, java.math.BigInteger crtCoefficient)
modulus
- the modulus n
publicExponent
- the public exponent e
privateExponent
- the private exponent d
primeP
- first prime factor of the modulusprimeQ
- second prime factor of the modulusprimeExponentP
- privateExponent mod (primeP-1)primeExponentQ
- privateExponent mod (primeQ-1)crtCoefficient
- the Chinese Remainder Theorem coefficient
(multiplic inverse of primeP mod primeQ)public RSAOaepPrivateKey(java.math.BigInteger modulus, java.math.BigInteger publicExponent, java.math.BigInteger privateExponent, java.math.BigInteger primeP, java.math.BigInteger primeQ, java.math.BigInteger primeExponentP, java.math.BigInteger primeExponentQ, java.math.BigInteger crtCoefficient, java.security.spec.AlgorithmParameterSpec oaepParams) throws java.security.spec.InvalidParameterSpecException
modulus
- the modulus n
publicExponent
- the public exponent e
privateExponent
- the private exponent d
primeP
- first prime factor of the modulusprimeQ
- second prime factor of the modulusprimeExponentP
- privateExponent mod (primeP-1)primeExponentQ
- privateExponent mod (primeQ-1)crtCoefficient
- the Chinese Remainder Theorem coefficient
(multiplic inverse of primeP mod primeQ)oaepParams
- the OAEP parameters (hash-, mgf- and psource-algorithm)java.security.spec.InvalidParameterSpecException
- if the parameters are not specified
as RSAOaepParameterSpec
object or are invalidpublic RSAOaepPrivateKey(byte[] pk) throws java.security.InvalidKeyException
This constructor may be used for parsing an already existing
RSAES-OAEP private key, wrapped into a PKCS#8 PrivateKeyInfo
that is supplied as DER encoded byte array.
pk
- the byte array holding the DER encoded private key infojava.security.InvalidKeyException
- if something is wrong with the key encodingpublic RSAOaepPrivateKey(ASN1Object obj) throws java.security.InvalidKeyException
PrivateKeyInfo
holding the RSAES-OAEP private key.obj
- the private key as ASN1Objectjava.security.InvalidKeyException
- if something is wrong with the key encodingpublic RSAOaepPrivateKey(java.io.InputStream is) throws java.io.IOException, java.security.InvalidKeyException
This constructor may be used for parsing an already existing
RSAES-OAEP private key, wrapped into a PKCS#8 PrivateKeyInfo
that is supplied as DER encoded byte array.
is
- the input stream with the data to be read to initialize the private keyjava.io.IOException
- if an I/O error occursjava.security.InvalidKeyException
- if something is wrong with the key encodingprotected void decode(byte[] privateKey) throws java.security.InvalidKeyException
From the given DER encoded byte array an ASN.1 object is created and parsed for
the RSAPrivateKey fields according to PKCS#1: version, modulus n
,
public and private exponent (e
and d
),
prime factor primeP
of n, prime factor primeQ
of n,
primeExponentP
(d mod(p-1)), primeExponentQ
(d mod(q-1)),
and crtCoefficient
, the Chinese Remainder Thereom coefficient q-1
mod p. If OAEP parameters included they are parsed from the algorithm id.
This method is protected and typically will not be used by an application. Rather
it is used by the parent PKCS#8 PrivateKeyInfo
class for decoding the inherent RSAES-OAEP private key.
decode
in class RSAPrivateKey
privateKey
- the RSAES-OAEP private key as DER encoded byte arrayjava.security.InvalidKeyException
- if something is wrong with the encoding of the keypublic java.security.PublicKey getPublicKey()
n
and public exponent e
of this private key.getPublicKey
in class RSAPrivateKey
public java.lang.String getAlgorithm()
getAlgorithm
in interface java.security.Key
getAlgorithm
in class RSAPrivateKey
public java.security.spec.AlgorithmParameterSpec getParams()
null
if there are no parameters
included. In this case the key may be used with any OAEP parameters.null
if there are
no parameters are includedpublic boolean validateParameters(java.security.spec.AlgorithmParameterSpec rsaOaepParamSpec) throws java.security.spec.InvalidParameterSpecException
rsaOaepParamSpec
- the OAEP parameters to be validatedtrue
if the given parameters can be used with this
key (i.e. if the key does not contain any parameters at all,
or if it contains parameters that specify the same hash algorithm
and mask generation function as the given parameters);
false
if notjava.security.spec.InvalidParameterSpecException
public int hashCode()
hashCode
in class RSAPrivateKey
public boolean equals(java.lang.Object obj)
equals
in class PrivateKeyInfo
obj
- the object to be comparedtrue
if the two objects are RSAOaepPrivateKey objects
with same values and parameters, false
otherwisepublic java.lang.String toString()
toString
in class RSAPrivateKey