iaik.security.rsa
Class RSAPublicKey

java.lang.Object
  |
  +--iaik.x509.PublicKeyInfo
        |
        +--iaik.security.rsa.RSAPublicKey
All Implemented Interfaces:
ASN1Type, Cloneable, Key, PublicKey, RSAKey, RSAPublicKey, Serializable

public class RSAPublicKey
extends PublicKeyInfo
implements RSAPublicKey, Serializable, Cloneable

This class implements a RSA public key and supports ASN.1 encoding. This class extends 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:

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:
PublicKeyInfo, RSAPublicKey, KeyPairGenerator, KeyPair, RSACipher, RSAKey, RSAPrivateKey, RSAKeyPairGenerator, RSAKeyFactory, Serialized Form

Fields inherited from class iaik.x509.PublicKeyInfo
public_key_algorithm
 
Fields inherited from interface java.security.PublicKey
serialVersionUID
 
Constructor Summary
RSAPublicKey(ASN1Object obj)
          Creates a new RSAPublicKey from the given ASN.1 object.
RSAPublicKey(BigInteger modulus, 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(InputStream is)
          Creates a new RSAPublicKey from an InputStream.
RSAPublicKey(RSAPublicKey key)
          Creates a new RSAPublicKey from the given RSAPublicKey representing modulus n and public exponent e.
RSAPublicKey(RSAPublicKeySpec keySpec)
          Creates a new RSAPublicKey from the given RSAPublicKeySpec representing modulus n and public exponent e.
 
Method Summary
 BigInteger crypt(BigInteger message)
          Deprecated. Use iaik.pkcs.pkcs1.RSACipher#rawPublicRSA instead.
protected  void decode(byte[] publicKey)
          Decodes a RSAPublicKey, encoded in DER format (PKCS#1).
protected  byte[] encode()
          Returns this RSA public key as DER encoded ASN.1 object (PKCS#1).
 boolean equals(Object obj)
          Compares this RSAPublicKey object with the supplied object.
 String getAlgorithm()
          Returns the name of the appertaining algorithm.
 byte[] getFingerprint()
          Returns the fingerprint of this RSA public key.
 BigInteger getModulus()
          Returns the modulus of the public key.
 BigInteger getPublicExponent()
          Returns the public exponent of the public key.
 int hashCode()
          Returns a hash code for this object.
static RSAPublicKey parse(byte[] publicKey)
          This method parses a RSA public key.
 String toString()
          Returns a string that represents the contents of this RSA public key.
 
Methods inherited from class iaik.x509.PublicKeyInfo
clone, createPublicKeyInfo, decode, getEncoded, getFormat, getPublicKey, getPublicKey, 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

RSAPublicKey

public RSAPublicKey(BigInteger modulus,
                    BigInteger publicExponent)
Creates a new RSAPublicKey with given values for the modulus n and the public exponent e.
Parameters:
modulus - the modulus of the key
publicExponent - the public exponent of the key

RSAPublicKey

public RSAPublicKey(RSAPublicKeySpec keySpec)
Creates a new RSAPublicKey from the given RSAPublicKeySpec representing modulus n and public exponent e.
Parameters:
keySpec - the RSAPublicKeySpec representing modulus n and public exponent e

RSAPublicKey

public RSAPublicKey(RSAPublicKey key)
Creates a new RSAPublicKey from the given RSAPublicKey representing modulus n and public exponent e.
Parameters:
key - the RSAPublicKey representing modulus n and public exponent e

RSAPublicKey

public RSAPublicKey(byte[] pk)
             throws InvalidKeyException
Creates a new RSAPublicKey from the given DER encoded byte array.

This constructor may be used for parsing an already exisiting RSA public key, wrapped into a X.509 PublicKeyInfo that is supplied as DER encoded byte array.

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

RSAPublicKey

public RSAPublicKey(ASN1Object obj)
             throws InvalidKeyException
Creates a new RSAPublicKey from the given ASN.1 object. The supplied ASN1Object represents a X.509 PublicKeyInfo holding the RSA public key.
Parameters:
obj - the public key ASN.1 structure
Throws:
InvalidKeyException - if something is wrong with the key encoding

RSAPublicKey

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

This constructor may be used for parsing an already exisiting RSA public key, wrapped into a X.509 PublicKeyInfo that is supplied as DER encoded byte array.

Parameters:
is - an input stream with the data to be read to initialize the public 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[] publicKey)
               throws InvalidKeyException
Decodes a RSAPublicKey, encoded in DER format (PKCS#1).

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.

Overrides:
decode in class PublicKeyInfo
Parameters:
publicKey - the public key as DER encoded ASN.1 object (PKCS#1)
Throws:
InvalidKeyException - if something is wrong with the encoding of the key

parse

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

encode

protected byte[] encode()
Returns this RSA public key as DER encoded ASN.1 object (PKCS#1).

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

Overrides:
encode in class PublicKeyInfo
Returns:
a byte array holding the RSA public key as a DER encoded ASN.1 datastructure (PKCS#1)

crypt

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

Uses the public key to encrypt or decrypt a message. Simply performs a modulo exponentiation.
Parameters:
message - the message to en/decrypt as BigInteger
Returns:
the encrypted or decrypted message

getAlgorithm

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

getPublicExponent

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

getModulus

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

getFingerprint

public byte[] getFingerprint()
Returns the fingerprint of this RSA public key. This is a MD5 hash of the DER encoded SubjectPublicKey.
Overrides:
getFingerprint in class PublicKeyInfo
Returns:
the fingerprint of this RSA public key

hashCode

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

toString

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

equals

public boolean equals(Object obj)
Compares this RSAPublicKey object with the supplied object.
Overrides:
equals in class PublicKeyInfo
Parameters:
obj - the object to be compared
Returns:
true if the two objects are RSAPublicKey objects with same modulus and exponent, false otherwise

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