public class RSAPrivateKey extends PrivateKeyInfo implements java.security.interfaces.RSAPrivateCrtKey
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:
KeyPairGenerator
has to be instantiated thereby
specifying the application's intention to create keys for use within the RSA
algorithm: KeyPairGenerator key_gen = KeyPairGenerator.getInstance("RSA");
initialize
method. For initializing the generator to
create keys with a modulus length of, e.g., 512 bits, this can be explicitly
specified (when not initializing the generator explicitly, per default the
modulus length is set to 2048 bits): key_gen.initialize(512);
generateKeyPair()
:
KeyPair key_pair = key_gen.generateKeyPair();
RSAPrivateKey rsa_priv_key = (RSAPrivateKey) key_pair.getPrivate();
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:
(whereRSAKeyPairGenerator rsa_key_gen = (RSAKeyPairGenerator) key_gen; rsa_key_gen.initialize(512, pub_exponent, sec_random);
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.
PrivateKeyInfo
,
RSAPrivateCrtKey
,
KeyPairGenerator
,
KeyPair
,
RSACipher
,
RSAPublicKey
,
RSAKeyPairGenerator
,
RSAKeyFactory
,
Serialized Formprivate_key_algorithm
Modifier | Constructor and Description |
---|---|
protected |
RSAPrivateKey()
Default constructor.
|
|
RSAPrivateKey(ASN1Object obj)
Creates a new private key from an ASN1Object.
|
|
RSAPrivateKey(java.math.BigInteger modulus,
java.math.BigInteger privateExponent)
Creates a new RSAPrivate key from given modulus and private exponent.
|
|
RSAPrivateKey(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 RSAPrivateKey from the given values.
|
|
RSAPrivateKey(byte[] pk)
Creates a new RSAPrivateKey from a DER encoded ASN.1 data structure.
|
|
RSAPrivateKey(java.io.InputStream is)
Creates a new RSAPrivateKey from an InputStream.
|
|
RSAPrivateKey(java.security.interfaces.RSAPrivateKey key)
Creates a new RSAPrivateKey from a RSAPrivateKey or RSAPrivateKeyCrt.
|
|
RSAPrivateKey(java.security.spec.RSAPrivateKeySpec keySpec)
Creates a new RSAPrivateKey from a RSAPrivateKeySpec or
RSAPrivateKeyCrtSpec.
|
Modifier and Type | Method and Description |
---|---|
java.math.BigInteger |
crypt(java.math.BigInteger message)
Deprecated.
Use iaik.pkcs.pkcs1.RSACipher#rawPrivateRSA instead.
|
protected void |
decode(byte[] privateKey)
Decodes a DER encoded RSAPrivateKey (PKCS#1).
|
byte[] |
encode()
Returns the raw (PKCS#1) RSA private key (not wrapped in a PKCS#8
PrivateKeyInfo) as DER encoded byte array.
|
java.lang.String |
getAlgorithm()
Returns the name of the appertaining algorithm.
|
java.math.BigInteger |
getCrtCoefficient()
Returns the Chinese Remainder Theorem coefficient of this private key.
|
java.math.BigInteger |
getModulus()
Returns the modulus of this private key.
|
java.math.BigInteger |
getPrimeExponentP()
Returns the first exponent of this private key.
|
java.math.BigInteger |
getPrimeExponentQ()
Returns the second exponent of this private key.
|
java.math.BigInteger |
getPrimeP()
Returns the first prime of this private key.
|
java.math.BigInteger |
getPrimeQ()
Returns the second prime of this private key.
|
java.math.BigInteger |
getPrivateExponent()
Returns the private exponent of this private key.
|
java.math.BigInteger |
getPublicExponent()
Returns the public exponent of this private key.
|
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.
|
static RSAPrivateKey |
parse(byte[] privateKey)
This method parses a RSA private key.
|
java.lang.String |
toString()
Returns a string that represents the contents of this private key.
|
clone, createPrivateKeyInfo, decode, equals, getAlgorithmID, getAttributes, getEncoded, getFormat, getPrivateKey, getPrivateKey, getPrivateKey, getPrivateKey, getPrivateKey, getPrivateKey, getPubKey, setAttributes, setPubKey, toASN1Object, writeTo
protected RSAPrivateKey()
public RSAPrivateKey(java.math.BigInteger modulus, java.math.BigInteger privateExponent)
privateExponent
- the private exponent e
modulus
- the modulus n
public RSAPrivateKey(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 RSAPrivateKey(java.security.spec.RSAPrivateKeySpec keySpec)
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). 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.keySpec
- the key specpublic RSAPrivateKey(java.security.interfaces.RSAPrivateKey key)
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). 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.key
- the keypublic RSAPrivateKey(byte[] pk) throws java.security.InvalidKeyException
This constructor may be used for parsing an already existing RSA 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 RSAPrivateKey(ASN1Object obj) throws java.security.InvalidKeyException
PrivateKeyInfo
holding the RSA
private key.obj
- the private key as ASN1Objectjava.security.InvalidKeyException
- if something is wrong with the key encodingpublic RSAPrivateKey(java.io.InputStream is) throws java.io.IOException, java.security.InvalidKeyException
This constructor may be used for parsing an already existing RSA 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 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.
decode
in class PrivateKeyInfo
privateKey
- the RSA private key as DER encoded byte arrayjava.security.InvalidKeyException
- if the given key is not a RSA private keypublic static RSAPrivateKey parse(byte[] privateKey) throws java.security.InvalidKeyException
privateKey
- a "RAW" RSA private keyjava.security.InvalidKeyException
- if the given key is not a RSA private keypublic java.math.BigInteger crypt(java.math.BigInteger message)
message
- the BigInteger message to encrypt or encryptpublic byte[] encode()
This method typically may not be used by an application. Rather it is used
by the parent PKCS#8 PrivateKeyInfo
class for encoding the inherent RSA private key.
encode
in class PrivateKeyInfo
public java.security.PublicKey getPublicKey()
n
and public exponent
e
of this private key.public java.math.BigInteger getPrivateExponent()
getPrivateExponent
in interface java.security.interfaces.RSAPrivateKey
public java.math.BigInteger getPublicExponent()
getPublicExponent
in interface java.security.interfaces.RSAPrivateCrtKey
public java.math.BigInteger getModulus()
getModulus
in interface java.security.interfaces.RSAKey
public java.math.BigInteger getPrimeP()
getPrimeP
in interface java.security.interfaces.RSAPrivateCrtKey
public java.math.BigInteger getPrimeQ()
getPrimeQ
in interface java.security.interfaces.RSAPrivateCrtKey
public java.math.BigInteger getPrimeExponentP()
exponent = privateExponent mod (prime_p-1);
getPrimeExponentP
in interface java.security.interfaces.RSAPrivateCrtKey
public java.math.BigInteger getPrimeExponentQ()
exponent = privateExponent mod (prime_q-1);
getPrimeExponentQ
in interface java.security.interfaces.RSAPrivateCrtKey
public java.math.BigInteger getCrtCoefficient()
crtCoefficient = multiplic inverse of prime_p mod prime_q;
getCrtCoefficient
in interface java.security.interfaces.RSAPrivateCrtKey
public java.lang.String getAlgorithm()
getAlgorithm
in interface java.security.Key
getAlgorithm
in class PrivateKeyInfo
public int hashCode()
hashCode
in class PrivateKeyInfo
public java.lang.String toString()
toString
in class PrivateKeyInfo