public class RSAPublicKey extends PublicKeyInfo implements java.security.interfaces.RSAPublicKey
iaik.x509.PublicKeyInfo
for supporting RSA 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 data encrypting or signature
verifying based on the RSA algorithm.
PKCS#1
describes a X.509/PEM compatible syntax for RSA public keys to be used in
certificates. The corresponding ASN.1 type RSAPublicKey
is
defined as ASN.1 SEQUENCE:
RSAPublicKey ::= SEQUENCE { modulus INTEGER, -- n publicExponent INTEGER -- e }
An application wishing to create a RSAPublicKey to be used for, e.g. data
encryption or signature verifying 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 RSAPublicKey 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();
RSAPublicKey rsa_pub_key = (RSAPublicKey) key_pair.getPublic();
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.
PublicKeyInfo
,
RSAPublicKey
,
KeyPairGenerator
,
KeyPair
,
RSACipher
,
RSAPrivateKey
,
RSAKeyPairGenerator
,
RSAKeyFactory
,
Serialized Formpublic_key_algorithm
Constructor and Description |
---|
RSAPublicKey(ASN1Object obj)
Creates a new RSAPublicKey from the given ASN.1 object.
|
RSAPublicKey(java.math.BigInteger modulus,
java.math.BigInteger publicExponent)
Creates a new RSAPublicKey with given values for the modulus
n
and the public exponent e . |
RSAPublicKey(byte[] pk)
Creates a new RSAPublicKey from the given DER encoded byte array.
|
RSAPublicKey(java.io.InputStream is)
Creates a new RSAPublicKey from an InputStream.
|
RSAPublicKey(java.security.interfaces.RSAPublicKey key)
Creates a new RSAPublicKey from the given RSAPublicKey representing modulus
n and public exponent e . |
RSAPublicKey(java.security.spec.RSAPublicKeySpec keySpec)
Creates a new RSAPublicKey from the given RSAPublicKeySpec representing
modulus
n and public exponent e . |
Modifier and Type | Method and Description |
---|---|
java.math.BigInteger |
crypt(java.math.BigInteger message)
Deprecated.
Use iaik.pkcs.pkcs1.RSACipher#rawPublicRSA instead.
|
protected void |
decode(byte[] publicKey)
Decodes a RSAPublicKey, encoded in DER format (PKCS#1).
|
byte[] |
encode()
Returns the raw (PKCS#1) RSA public key (not wrapped in a X.509
PublicKeyInfo) as DER encoded ASN.1 object.
|
boolean |
equals(java.lang.Object obj)
Compares this RSAPublicKey object with the supplied object.
|
java.lang.String |
getAlgorithm()
Returns the name of the appertaining algorithm.
|
byte[] |
getFingerprint()
Returns the fingerprint of this RSA public key.
|
java.math.BigInteger |
getModulus()
Returns the modulus of the public key.
|
java.math.BigInteger |
getPublicExponent()
Returns the public exponent of the public key.
|
int |
hashCode()
Returns a hash code for this object.
|
boolean |
isValidSP80089SignatureVerificationKey()
Checks whether this public key is valid for signature verification
according to NIST SP 800-89.
|
boolean |
isValidSP80089SignatureVerificationKey(int securityStrength)
Checks whether this public key is valid for signature verification
according to NIST SP 800-89.
|
static RSAPublicKey |
parse(byte[] publicKey)
This method parses a RSA public key.
|
java.lang.String |
toString()
Returns a string that represents the contents of this RSA public key.
|
clone, createPublicKeyInfo, decode, getAlgorithmID, getEncoded, getFormat, getPublicKey, getPublicKey, getPublicKey, getPublicKey, getPublicKey, getPublicKey, toASN1Object, writeTo
public RSAPublicKey(java.math.BigInteger modulus, java.math.BigInteger publicExponent)
n
and the public exponent e
.modulus
- the modulus of the keypublicExponent
- the public exponent of the keypublic RSAPublicKey(java.security.spec.RSAPublicKeySpec keySpec)
n
and public exponent e
.keySpec
- the RSAPublicKeySpec representing modulus n
and
public exponent e
public RSAPublicKey(java.security.interfaces.RSAPublicKey key)
n
and public exponent e
.key
- the RSAPublicKey representing modulus n
and public
exponent e
public RSAPublicKey(byte[] pk) throws 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.
pk
- the byte array holding the DER encoded public key infojava.security.InvalidKeyException
- if something is wrong with the key encodingpublic RSAPublicKey(ASN1Object obj) throws java.security.InvalidKeyException
PublicKeyInfo
holding the RSA public key.obj
- the public key ASN.1 structurejava.security.InvalidKeyException
- if something is wrong with the key encodingpublic RSAPublicKey(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
.
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 inherent RSA public key.
decode
in class PublicKeyInfo
publicKey
- the public key as DER encoded ASN.1 object (PKCS#1)java.security.InvalidKeyException
- if something is wrong with the encoding of the keypublic static RSAPublicKey parse(byte[] publicKey) throws java.security.InvalidKeyException
publicKey
- a "RAW" RSA public keyjava.security.InvalidKeyException
- if the given key is not a RSA public keypublic byte[] encode()
This method typically may not be used by an application. Rather it is used
by the parent X.509 PublicKeyInfo
class for
encoding the inherent RSA public key.
encode
in class PublicKeyInfo
public java.math.BigInteger crypt(java.math.BigInteger message)
message
- the message to en/decrypt as BigIntegerpublic java.lang.String getAlgorithm()
getAlgorithm
in interface java.security.Key
getAlgorithm
in class PublicKeyInfo
public java.math.BigInteger getPublicExponent()
getPublicExponent
in interface java.security.interfaces.RSAPublicKey
public java.math.BigInteger getModulus()
getModulus
in interface java.security.interfaces.RSAKey
public byte[] getFingerprint()
getFingerprint
in class PublicKeyInfo
public int hashCode()
hashCode
in class PublicKeyInfo
public java.lang.String toString()
toString
in class PublicKeyInfo
public boolean equals(java.lang.Object obj)
equals
in class PublicKeyInfo
obj
- the object to be comparedtrue
if the two objects are RSAPublicKey objects with
same modulus and exponent, false
otherwisepublic boolean isValidSP80089SignatureVerificationKey()
This method may only be used to validate RSA public keys that are used for signature verification and compliance with NIST SP 800-89 is required. Note that in accordance with item 1) below this method will only accept keys with a modulus length of 1024, 2048 or 3072 bits!
According to NIST SP 800-89 an RSA public key that is used for signature verification shall be validated by the following procedure (see section 5.3.3 of NIST SP 800-89, (Explicit) Partial Public Key Validation for RSA):
true
, if the public key could be successfully validated
false
otherwisepublic boolean isValidSP80089SignatureVerificationKey(int securityStrength)
This method may only be used to validate RSA public keys that are used for signature verification and compliance with NIST SP 800-89 is required. Note that in accordance with item 1) below this method will only accept keys with a modulus length of 1024, 2048 or 3072 bits!
According to NIST SP 800-89 an RSA public key that is used for signature verification shall be validated by the following procedure (see section 5.3.3 of NIST SP 800-89, (Explicit) Partial Public Key Validation for RSA):
securityStrength
- the security strength to be checked or -1 if
the security strength shall not be checkedtrue
, if the public key could be successfully validated
false
otherwise