iaik.security.rsa
Class RSAPrivateKey

java.lang.Object
  |
  +--iaik.pkcs.pkcs8.PrivateKeyInfo
        |
        +--iaik.security.rsa.RSAPrivateKey
All Implemented Interfaces:
ASN1Type, Cloneable, Key, PrivateKey, RSAKey, RSAPrivateCrtKey, RSAPrivateKey, Serializable

public class RSAPrivateKey
extends PrivateKeyInfo
implements RSAPrivateCrtKey, Serializable

This class implements an RSA private key and supports ASN.1 encoding.

This class extends iaik.pkcs.pkcs8.PrivateKeyInfo for supporting the PKCS#8 Private Key Information Standard for RSA private keys. This class implements the java.security.interfaces.RSAPrivateKeyCrt interface for providing the functionality of a private key, as used for data decrypting or digital signing based on the RSA algorithm. This class implements the RSAPrivateKeyCrt interface for using the Chinese Remainder Theorem to speed up private key operations by extending the standard RSA private key components modulus n and private exponent d according to PKCS#1:

 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) }
 

An application wishing to create a RSAPrivateKey to be used for, e.g. data decryption or digital signing with the RSA algorithm, uses a proper getInstance method of the java.security.KeyPairGenerator class, which subsequently maybe casted to RSAKeyPairGenerator for performing an algorithm-specific initialization with proper RSA parameters. If an algorithm-specific initialization is not required, the cast to RSAKeyPairGenerator can be omitted.

Generally four steps have to be performed for creating a RSAPrivateKey by using a proper KeyPairGenerator:

For performing an algorithm-specific initialization with particular RSA parameters (e.g. using a particular public exponent e), an explicit cast of the KeyPairGenerator will be necessary for obtaining a specific RSAKeyPairGenerator to be initialized with the desired RSA parameters:

 RSAKeyPairGenerator rsa_key_gen = (RSAKeyPairGenerator)key_gen;
 rsa_key_gen.initialize(512, pub_exponent, sec_random);
 
(where sec_random denotes some random seed)

Guidelines on how to create some key using a KeyPairGenerator can be found in http://java.sun.com/products/JDK/1.2/docs/guide/security/CryptoSpec.html.

Version:
File Revision 28
See Also:
PrivateKeyInfo, RSAPrivateCrtKey, KeyPairGenerator, KeyPair, RSACipher, RSAKey, RSAPublicKey, RSAKeyPairGenerator, RSAKeyFactory, Serialized Form

Fields inherited from class iaik.pkcs.pkcs8.PrivateKeyInfo
private_key_algorithm
 
Fields inherited from interface java.security.PrivateKey
serialVersionUID
 
Constructor Summary
protected RSAPrivateKey()
          Default constructor.
  RSAPrivateKey(ASN1Object obj)
          Creates a new private key from an ASN1Object.
  RSAPrivateKey(BigInteger modulus, BigInteger privateExponent)
          Creates a new RSAPrivate key from given modulus and private exponent.
  RSAPrivateKey(BigInteger modulus, BigInteger publicExponent, BigInteger privateExponent, BigInteger primeP, BigInteger primeQ, BigInteger primeExponentP, BigInteger primeExponentQ, BigInteger crtCoefficient)
          Creates a RSAPrivateKey from the given values.
  RSAPrivateKey(byte[] pk)
          Creates a new RSAPrivateKey from a DER encoded ASN.1 data structure.
  RSAPrivateKey(InputStream is)
          Creates a new RSAPrivateKey from an InputStream.
  RSAPrivateKey(RSAPrivateKey key)
          Creates a new RSAPrivateKey from a RSAPrivateKey or RSAPrivateKeyCrt.
  RSAPrivateKey(RSAPrivateKeySpec keySpec)
          Creates a new RSAPrivateKey from a RSAPrivateKeySpec or RSAPrivateKeyCrtSpec.
 
Method Summary
 BigInteger crypt(BigInteger message)
          Deprecated. Use iaik.pkcs.pkcs1.RSACipher#rawPrivateRSA instead.
protected  void decode(byte[] privateKey)
          Decodes a DER encoded RSAPrivateKey (PKCS#1).
protected  byte[] encode()
          Returns this RSA private key as DER encoded byte array (PKCS#1).
 String getAlgorithm()
          Returns the name of the appertaining algorithm.
 BigInteger getCrtCoefficient()
          Returns the Chinese Remainder Theorem coefficient of this private key.
 BigInteger getModulus()
          Returns the modulus of this private key.
 BigInteger getPrimeExponentP()
          Returns the first exponent of this private key.
 BigInteger getPrimeExponentQ()
          Returns the second exponent of this private key.
 BigInteger getPrimeP()
          Returns the first prime of this private key.
 BigInteger getPrimeQ()
          Returns the second prime of this private key.
 BigInteger getPrivateExponent()
          Returns the private exponent of this private key.
 BigInteger getPublicExponent()
          Returns the public exponent of this private key.
 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.
static RSAPrivateKey parse(byte[] privateKey)
          This method parses a RSA private key.
 String toString()
          Returns a string that represents the contents of this private key.
 
Methods inherited from class iaik.pkcs.pkcs8.PrivateKeyInfo
clone, createPrivateKeyInfo, decode, equals, getEncoded, getFormat, getPrivateKey, getPrivateKey, toASN1Object, writeTo
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.security.Key
getEncoded, getFormat
 

Constructor Detail

RSAPrivateKey

protected RSAPrivateKey()
Default constructor.

RSAPrivateKey

public RSAPrivateKey(BigInteger modulus,
                     BigInteger privateExponent)
Creates a new RSAPrivate key from given modulus and private exponent. Public exponent, prime factors p and q, primeExponentP and primeExponentQ, and Chinese Remainder Theorem Coefficient are set to ZERO (0).
Parameters:
privateExponent - the private exponent e
modulus - the modulus n

RSAPrivateKey

public RSAPrivateKey(BigInteger modulus,
                     BigInteger publicExponent,
                     BigInteger privateExponent,
                     BigInteger primeP,
                     BigInteger primeQ,
                     BigInteger primeExponentP,
                     BigInteger primeExponentQ,
                     BigInteger crtCoefficient)
Creates a RSAPrivateKey from the given values.
Parameters:
modulus - the modulus n
publicExponent - the public exponent e
privateExponent - the private exponent d
primeP - first prime factor of the modulus
primeQ - second prime factor of the modulus
primeExponentP - privateExponent mod (primeP-1)
primeExponentQ - privateExponent mod (primeQ-1)
crtCoefficient - the Chinese Remainder Theorem coefficient (multiplic inverse of primeP mod primeQ)

RSAPrivateKey

public RSAPrivateKey(RSAPrivateKeySpec keySpec)
Creates a new RSAPrivateKey from a RSAPrivateKeySpec or RSAPrivateKeyCrtSpec. If the given key specification is an instance of RSAPrivateKeySpec the Chinese Remainder Theorem would not be considered by only parsing modulus and private exponent from the given specification and setting the other parameters to ZERO (0).
If the given key specification is an instance of RSAPrivateKeyCrtSpec, all parameters (modulus, public and private exponent, prime factors p and q, primeExponentP and primeExponentQ, and Chinese Remainder Theorem coefficient) are parsed from the given specification.
Parameters:
keySpec - the key spec

RSAPrivateKey

public RSAPrivateKey(RSAPrivateKey key)
Creates a new RSAPrivateKey from a RSAPrivateKey or RSAPrivateKeyCrt. If the given key specification is an instance of RSAPrivateKey the Chinese Remainder Theorem would not be considered by only parsing modulus and private exponent from the given specification and setting the other parameters to ZERO (0).
If the given key specification is an instance of RSAPrivateKeyCrt, all parameters (modulus, public and private exponent, prime factors p and q, primeExponentP and primeExponentQ, and Chinese Remainder Theorem coefficient) are parsed from the given specification.
Parameters:
key - the key

RSAPrivateKey

public RSAPrivateKey(byte[] pk)
              throws InvalidKeyException
Creates a new RSAPrivateKey from a DER encoded ASN.1 data structure.

This constructor may be used for parsing an already exisiting RSA private key, wrapped into a PKCS#8 PrivateKeyInfo that is supplied as DER encoded byte array.

Parameters:
the - byte array holding the DER encoded private key info
Throws:
InvalidKeyException - if something is wrong with the key encoding

RSAPrivateKey

public RSAPrivateKey(ASN1Object obj)
              throws InvalidKeyException
Creates a new private key from an ASN1Object. The supplied ASN1Object represents a PKCS#8 PrivateKeyInfo holding the RSA private key.
Parameters:
obj - the private key as ASN1Object
Throws:
InvalidKeyException - if something is wrong with the key encoding

RSAPrivateKey

public RSAPrivateKey(InputStream is)
              throws IOException,
                     InvalidKeyException
Creates a new RSAPrivateKey from an InputStream.

This constructor may be used for parsing an already exisiting RSA private key, wrapped into a PKCS#8 PrivateKeyInfo that is supplied as DER encoded byte array.

Parameters:
is - the input stream with the data to be read to initialize the private key
Throws:
IOException - if an I/O error occurs
InvalidKeyException - if something is wrong with the key encoding
Method Detail

decode

protected void decode(byte[] privateKey)
               throws InvalidKeyException
Decodes a DER encoded RSAPrivateKey (PKCS#1).

From the given DER encoded byte array an ASN.1 object is created and parsed for the RSAPrivateKey parameters 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.

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 RSA private key.

Overrides:
decode in class PrivateKeyInfo
Parameters:
privateKey - the RSA private key as DER encoded byte array
Throws:
InvalidKeyException - if the given key is not a RSA private key

parse

public static RSAPrivateKey parse(byte[] privateKey)
                           throws InvalidKeyException
This method parses a RSA private key. The format must be RSAPrivateKey as defined in PKCS#1.
Parameters:
privateKey - a "RAW" RSA private key
Throws:
InvalidKeyException - if the given key is not a RSA private key

crypt

public BigInteger crypt(BigInteger message)
Deprecated. Use iaik.pkcs.pkcs1.RSACipher#rawPrivateRSA instead.

Encrypts or decrypts a BigInteger using the private key. This method uses a fast algorithm based on the chinese remainder theorem.
Parameters:
message - the BigInteger message to encrypt or encrypt
Returns:
the encrypted or decrypted message

encode

protected byte[] encode()
Returns this RSA private key as DER encoded byte array (PKCS#1).

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 encoding the inherent RSA private key.

Overrides:
encode in class PrivateKeyInfo
Returns:
the RSA private key as a DER encoded ASN.1 datastructure

getPublicKey

public PublicKey getPublicKey()
Returns the public parts (modulus n and public exponent e of this private key.
Returns:
the public key part of this private key

getPrivateExponent

public BigInteger getPrivateExponent()
Returns the private exponent of this private key.
Specified by:
getPrivateExponent in interface RSAPrivateKey
Returns:
the private exponent of the private key.

getPublicExponent

public BigInteger getPublicExponent()
Returns the public exponent of this private key.
Specified by:
getPublicExponent in interface RSAPrivateCrtKey
Returns:
the public exponent of the private key.

getModulus

public BigInteger getModulus()
Returns the modulus of this private key.
Specified by:
getModulus in interface RSAKey
Returns:
the public modulus of the private key.

getPrimeP

public BigInteger getPrimeP()
Returns the first prime of this private key.
Specified by:
getPrimeP in interface RSAPrivateCrtKey
Returns:
the prime 1 of the private key.

getPrimeQ

public BigInteger getPrimeQ()
Returns the second prime of this private key.
Specified by:
getPrimeQ in interface RSAPrivateCrtKey
Returns:
the prime 2 of the private key.

getPrimeExponentP

public BigInteger getPrimeExponentP()
Returns the first exponent of this private key.

exponent = privateExponent mod (prime_p-1);

Specified by:
getPrimeExponentP in interface RSAPrivateCrtKey
Returns:
the exponent 1 of the private key.

getPrimeExponentQ

public BigInteger getPrimeExponentQ()
Returns the second exponent of this private key.

exponent = privateExponent mod (prime_q-1);

Specified by:
getPrimeExponentQ in interface RSAPrivateCrtKey
Returns:
the exponent 2 of the private key.

getCrtCoefficient

public BigInteger getCrtCoefficient()
Returns the Chinese Remainder Theorem coefficient of this private key.

crtCoefficient = multiplic inverse of prime_p mod prime_q;

Specified by:
getCrtCoefficient in interface RSAPrivateCrtKey
Returns:
the Chinese Remainder Theorem coefficient of the private key.

getAlgorithm

public String getAlgorithm()
Returns the name of the appertaining algorithm.
Specified by:
getAlgorithm in interface Key
Overrides:
getAlgorithm in class PrivateKeyInfo
Returns:
the string "RSA"

hashCode

public int hashCode()
Returns a hash code for this object.
Overrides:
hashCode in class PrivateKeyInfo
Returns:
the hash code

toString

public String toString()
Returns a string that represents the contents of this private key.
Overrides:
toString in class PrivateKeyInfo
Returns:
the string representation

This Javadoc may contain text parts from Internet Standard specifications (RFC 2459, 3280, 3039, 2560, 1521, 821, 822, 2253, 1319, 1321, ,2630, 2631, 2268, 3058, 2984, 2104, 2144, 2040, 2311, 2279, see copyright note) and RSA Data Security Public-Key Cryptography Standards (PKCS#1,3,5,7,8,9,10,12, see copyright note).

IAIK-JCE 3.1 with IAIK-JCE CC Core 3.1, (c) 1997-2004 IAIK