public class RSAOaepPublicKey extends RSAPublicKey
iaik.x509.PublicKeyInfo
for supporting RSA-OAEP public keys to be used within X.509 certificates. This
class implements the java.security.interfaces.RSAPublicKey interface
for providing the functionality of a public key, as used for encryption 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 public key differs from an ordinary PKCS#1 RSA public key only in that a RSAES-OAEP key may
contain OAEP parameters;
modulus and publicExponent fields are encoded
in the same way as ASN.1 SEQUENCE according to
PKCS#1:
RSAPublicKey ::= SEQUENCE {
modulus INTEGER, -- n
publicExponent INTEGER -- e }
If a RSAES-OAEP key does not contain parameters it may be used for OAEP
based encryption 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):
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 RSAOaepPublicKey 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 RSAOaepPublicKey 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
1024 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();
An RSAES-OAEP key must be only used for encryption 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
private key:
Cipher rsaOaep = Cipher.getInstance("RSA/ECB/OAEP", "IAIK");
rsaOaep.init(Cipher.DECRYPT_MODE, privateKey);
// decrypt data
byte[] decrypted = rsaOaep.doFinal(encrypted);
PublicKeyInfo,
RSAPublicKey,
RSAOaepPrivateKey,
RSAOaepKeyPairGenerator,
RSAOaepKeyFactory,
RSAOaepParameterSpec,
RSACipher,
RSAPublicKey,
KeyPairGenerator,
KeyPair,
Serialized Formpublic_key_algorithm| Constructor and Description |
|---|
RSAOaepPublicKey(ASN1Object obj)
Creates a new RSAOaepPublicKey from the given ASN.1 object.
|
RSAOaepPublicKey(java.math.BigInteger modulus,
java.math.BigInteger publicExponent)
Creates a new RSAOaepPublicKey with given values for modulus
n and
public exponent e. |
RSAOaepPublicKey(java.math.BigInteger modulus,
java.math.BigInteger publicExponent,
java.security.spec.AlgorithmParameterSpec oaepParams)
Creates a new RSAOaepPublicKey with given values for modulus
n and
public exponent e and given OAEP parameters. |
RSAOaepPublicKey(byte[] pk)
Creates a new RSAOaepPublicKey from the given DER encoded byte array.
|
RSAOaepPublicKey(java.io.InputStream is)
Creates a new RSAOaepPublicKey from an InputStream.
|
| Modifier and Type | Method and Description |
|---|---|
protected void |
decode(byte[] publicKey)
Decodes a RSAOaepPublicKey, encoded in DER format.
|
boolean |
equals(java.lang.Object obj)
Compares this RSAOaepPublicKey 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).
|
int |
hashCode()
Returns a hash code for this object.
|
java.lang.String |
toString()
Returns a string that represents the contents of this RSA OAEP public key.
|
boolean |
validateParameters(java.security.spec.AlgorithmParameterSpec rsaOaepParamSpec)
Validates the given parameters for this OAEP key.
|
crypt, encode, getFingerprint, getModulus, getPublicExponent, parseclone, createPublicKeyInfo, decode, getAlgorithmID, getEncoded, getFormat, getPublicKey, getPublicKey, getPublicKey, getPublicKey, toASN1Object, writeTopublic RSAOaepPublicKey(java.math.BigInteger modulus,
java.math.BigInteger publicExponent)
n and
public exponent e. When using this constructor for creating
an OAEP key, no parameters are explicitly specified. This means that no
parameters will be encoded with the algorithm id of this OAEP key and
the key may be used with any OAEP parameters.modulus - the modulus of the keypublicExponent - the public exponent of the keypublic RSAOaepPublicKey(java.math.BigInteger modulus,
java.math.BigInteger publicExponent,
java.security.spec.AlgorithmParameterSpec oaepParams)
throws java.security.spec.InvalidParameterSpecException
n and
public exponent e and given OAEP parameters. If no parameters
are specified no parameters will be encoded with the algorithm id of this
OAEP key and the key may be used with any OAEP parameters.modulus - the modulus of the keypublicExponent - the public exponent of the keyoaepParams - the OAEP parameters (hash-, mgf- and psource-algorithm)java.security.spec.InvalidParameterSpecException - if the parameters are not specified
as RSAOaepParameterSpec
object or are invalidpublic RSAOaepPublicKey(byte[] pk)
throws java.security.InvalidKeyException
This constructor may be used for parsing an already existing
RSA-OAEP public key, wrapped into a X.509 PublicKeyInfo that is supplied as DER encoded byte array.
pk - the byte array holding the DER encoded public key infojava.security.InvalidKeyException - if something is wrong with the key encodingpublic RSAOaepPublicKey(ASN1Object obj) throws java.security.InvalidKeyException
PublicKeyInfo holding the RSA-OAEP public key.obj - the public key ASN.1 structurejava.security.InvalidKeyException - if something is wrong with the key encodingpublic RSAOaepPublicKey(java.io.InputStream is)
throws java.io.IOException,
java.security.InvalidKeyException
This constructor may be used for parsing an already existing
RSA public key, wrapped into a X.509 PublicKeyInfo that is supplied as DER encoded byte array.
is - an input stream with the data to be read to initialize the public keyjava.io.IOException - if an I/O error occursjava.security.InvalidKeyException - if something is wrong with the key encodingprotected void decode(byte[] publicKey)
throws java.security.InvalidKeyException
From the given DER encoded byte array an ASN.1 object is created and parsed for
modulus n and public exponent e and OAEP parameters,
if included.
This method is protected and typically will not be used by an application. Rather
it is used by the parent X.509 PublicKeyInfo class for decoding the public key.
decode in class RSAPublicKeypublicKey - the public key as DER encoded ASN.1 object (PKCS#1)java.security.InvalidKeyException - if something is wrong with the encoding of the keypublic java.lang.String getAlgorithm()
getAlgorithm in interface java.security.KeygetAlgorithm in class RSAPublicKeypublic 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.InvalidParameterSpecExceptionpublic int hashCode()
hashCode in class RSAPublicKeypublic boolean equals(java.lang.Object obj)
equals in class RSAPublicKeyobj - the object to be comparedtrue if the two objects are RSAOaepPublicKey objects
with same modulus and exponent and parameters, false otherwisepublic java.lang.String toString()
toString in class RSAPublicKey