iaik.me.security
Class CryptoBag

java.lang.Object
  |
  +--iaik.me.security.CryptoBag
Direct Known Subclasses:
PrivateKey, PublicKey

public class CryptoBag
extends Object

This is the central class for keying material and parameters. It is used to represent secret keys, initialization vectors, public keys, private keys, key parameters, keypairs, etc. Instances are created using the make* methods defined in this class and those methods and the constructors in the PublicKey and PrivateKey classes.

Objects of this class are immutable, i.e. they cannot be modified once they have been created. The values contained in the object can be retrieved using the appropriate get*() method using one of the V_* constants valid for the object, i.e.

There are separate PublicKey and PrivateKey classes but these are for type safety and import/export only. The basic functionality is all present in this class.

Example:
To create a secret key:

   byte[] keyData = ...; // the given key as a byte array
   byte[] ivData = ...; // the given iv as a byte array
   CryptoBag key = CryptoBag.makeSecretKey(keyData);
   CryptoBag iv = CryptoBag.makeIv(ivData);
   cipher.init(Cipher.ENCRYPT_MODE, key, iv, null); // initialize a cipher object
 

See Also:
PublicKey, PrivateKey

Field Summary
static int TYPE_DH_PARAMS
          Type constant for Diffie-Hellman parameters.
static int TYPE_DH_PRIVATE
          Type constant for an Diffie-Hellman private key.
static int TYPE_DH_PUBLIC
          Type constant for an Diffie-Hellman public key.
static int TYPE_DSA_PARAMS
          Type constant for DSA parameters.
static int TYPE_DSA_PRIVATE
          Type constant for an DSA private key.
static int TYPE_DSA_PUBLIC
          Type constant for an DSA public key.
static int TYPE_IV
          Type constant for an initialization vector.
static int TYPE_KEYPAIR
          Type constant for a key pair, i.e. a public key and a private key
static int TYPE_RSA_PRIVATE
          Type constant for an RSA private key.
static int TYPE_RSA_PUBLIC
          Type constant for an RSA public key.
static int TYPE_SECRET_KEY
          Type constant for a secret key.
static int V_DH_G
           
static int V_DH_P
           
static int V_DH_PARAMS
           
static int V_DH_X
           
static int V_DH_Y
           
static int V_DSA_G
           
static int V_DSA_P
           
static int V_DSA_PARAMS
           
static int V_DSA_Q
           
static int V_DSA_X
           
static int V_DSA_Y
           
static int V_IV
           
static int V_KEY
           
static int V_KEY_PRIVATE
           
static int V_KEY_PUBLIC
           
static int V_RSA_CRT_C
           
static int V_RSA_CRT_EP
           
static int V_RSA_CRT_EQ
           
static int V_RSA_CRT_P
           
static int V_RSA_CRT_Q
           
static int V_RSA_D
           
static int V_RSA_E
           
static int V_RSA_N
           
 
Constructor Summary
CryptoBag(ASN1 asn1)
          Creates a CryptoBag from its ASN.1 object representing an DSA or DH algorithmID with parameters included.
 
Method Summary
 boolean equals(Object obj)
           
 ASN1 getASN1()
          Returns this CryptoBag as ASN.1 object representing an DSA or DH algorithmID with parameters included.
 BigInteger getBigInteger(int param)
          Gets a BigInteger component out of the CryptoBag.
 byte[] getByteArray(int param)
          Gets a byte array component out of the CryptoBag.
 CryptoBag getCryptoBag(int param)
          Gets a CryptoBag component out of the CryptoBag.
 byte[] getEncoded()
          Returns this CryptoBag as DER encoded byte array.
 int getKeyLength()
           
 int getType()
           
 int hashCode()
           
static CryptoBag makeDHParameters(BigInteger p, BigInteger g)
          Creates a CryptoBag for the given DH parameters.
static CryptoBag makeDSAParameters(BigInteger p, BigInteger q, BigInteger g)
          Creates a CryptoBag for the given DSA parameters.
static CryptoBag makeIV(byte[] iv)
          Creates a CryptoBag for the given initialization vector.
static CryptoBag makeKeyPair(PublicKey pubKey, PrivateKey privKey)
          Creates a CryptoBag for the given key pair.
static CryptoBag makeSecretKey(byte[] key)
          Creates a CryptoBag for the given raw secret key encoding.
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

TYPE_SECRET_KEY

public static final int TYPE_SECRET_KEY
Type constant for a secret key. Used with symmetric cipher, macs, etc.

TYPE_IV

public static final int TYPE_IV
Type constant for an initialization vector. Used with symmetric ciphers in CBC mode.

TYPE_RSA_PUBLIC

public static final int TYPE_RSA_PUBLIC
Type constant for an RSA public key.

TYPE_RSA_PRIVATE

public static final int TYPE_RSA_PRIVATE
Type constant for an RSA private key.

TYPE_DSA_PUBLIC

public static final int TYPE_DSA_PUBLIC
Type constant for an DSA public key.

TYPE_DSA_PRIVATE

public static final int TYPE_DSA_PRIVATE
Type constant for an DSA private key.

TYPE_DSA_PARAMS

public static final int TYPE_DSA_PARAMS
Type constant for DSA parameters.

TYPE_DH_PUBLIC

public static final int TYPE_DH_PUBLIC
Type constant for an Diffie-Hellman public key.

TYPE_DH_PRIVATE

public static final int TYPE_DH_PRIVATE
Type constant for an Diffie-Hellman private key.

TYPE_DH_PARAMS

public static final int TYPE_DH_PARAMS
Type constant for Diffie-Hellman parameters.

TYPE_KEYPAIR

public static final int TYPE_KEYPAIR
Type constant for a key pair, i.e. a public key and a private key

V_KEY

public static final int V_KEY

V_IV

public static final int V_IV

V_RSA_N

public static final int V_RSA_N

V_RSA_E

public static final int V_RSA_E

V_RSA_D

public static final int V_RSA_D

V_RSA_CRT_P

public static final int V_RSA_CRT_P

V_RSA_CRT_Q

public static final int V_RSA_CRT_Q

V_RSA_CRT_EP

public static final int V_RSA_CRT_EP

V_RSA_CRT_EQ

public static final int V_RSA_CRT_EQ

V_RSA_CRT_C

public static final int V_RSA_CRT_C

V_DSA_X

public static final int V_DSA_X

V_DSA_Y

public static final int V_DSA_Y

V_DSA_PARAMS

public static final int V_DSA_PARAMS

V_DSA_P

public static final int V_DSA_P

V_DSA_Q

public static final int V_DSA_Q

V_DSA_G

public static final int V_DSA_G

V_DH_X

public static final int V_DH_X

V_DH_Y

public static final int V_DH_Y

V_DH_PARAMS

public static final int V_DH_PARAMS

V_DH_P

public static final int V_DH_P

V_DH_G

public static final int V_DH_G

V_KEY_PUBLIC

public static final int V_KEY_PUBLIC

V_KEY_PRIVATE

public static final int V_KEY_PRIVATE
Constructor Detail

CryptoBag

public CryptoBag(ASN1 asn1)
          throws IOException
Creates a CryptoBag from its ASN.1 object representing an DSA or DH algorithmID with parameters included. For decoding a key use class PublicKey immediately.
Parameters:
asn1 - the CryptoBag as ASN1 object
Throws:
IOException - if an parsing error occurs or if the ASN1 object does not represent DSA or DH parameters
Method Detail

getASN1

public ASN1 getASN1()
Returns this CryptoBag as ASN.1 object representing an DSA or DH algorithmID with parameters included. For retrieving the ASN1 representation of a key use class PublicKey immediately.
Returns:
the CryptoBag as ASN1 object

getEncoded

public byte[] getEncoded()
Returns this CryptoBag as DER encoded byte array.
Returns:
the DER encoding of this CryptoBag

getType

public int getType()

getKeyLength

public int getKeyLength()

getBigInteger

public BigInteger getBigInteger(int param)
Gets a BigInteger component out of the CryptoBag. When calling this method be sure that the component referenced by param represents a BigInteger, e.g.:
 BigInteger pub_exp = rsaPublicKey.getBigInteger(CryptoBag.V_RSA_E);
 
retrieves the public exponent of an RSA public key.
Parameters:
the - V_ constant referencing the BigInteger component to be retrieved
Returns:
the BigInteger component

getByteArray

public byte[] getByteArray(int param)
Gets a byte array component out of the CryptoBag. When calling this method be sure that the component referenced by param is represented by a byte array, e.g.:
 byte[] ivData = cryptoBag.getByteArray(CryptoBag.V_IV);
 
retrieves an initialization vector as byte array.
Parameters:
the - V_ constant referencing the byte array component to be retrieved
Returns:
the byte array component

getCryptoBag

public CryptoBag getCryptoBag(int param)
Gets a CryptoBag component out of the CryptoBag. When calling this method be sure that the component referenced by param represents a CryptoBag itself, e.g.:
 CryptoBag keyPair = ...;
 PublicKey rsa_pub = (PublicKey)keyPair.getCryptoBag(CryptoBag.V_KEY_PUBLIC);
 
retrieves the public key of an RSA key pair (note that PublicKey is a CryptoBag and therefore method getCryptoBag can be used here).
Parameters:
the - V_ constant referencing the CryptoBag component to be retrieved
Returns:
the CryptoBag component

makeSecretKey

public static CryptoBag makeSecretKey(byte[] key)
Creates a CryptoBag for the given raw secret key encoding.
Parameters:
key - the secret key encoding
Returns:
the CryptoBag containing the secret key

makeIV

public static CryptoBag makeIV(byte[] iv)
Creates a CryptoBag for the given initialization vector.
Parameters:
iv - the initialization vector as byte array
Returns:
the CryptoBag containing the initialization vector

makeDSAParameters

public static CryptoBag makeDSAParameters(BigInteger p,
                                          BigInteger q,
                                          BigInteger g)
Creates a CryptoBag for the given DSA parameters.
Parameters:
p - the public prime value
q - the public sub-prime value
g - the public base value
Returns:
the CryptoBag containing the DSA parameters

makeDHParameters

public static CryptoBag makeDHParameters(BigInteger p,
                                         BigInteger g)
Creates a CryptoBag for the given DH parameters.
Parameters:
p - prime modulus p
g - the base generator
Returns:
the CryptoBag containing the DH parameters

makeKeyPair

public static CryptoBag makeKeyPair(PublicKey pubKey,
                                    PrivateKey privKey)
Creates a CryptoBag for the given key pair.
Parameters:
pubKey - the public key
privKey - the private key
Returns:
the CryptoBag containing the key pair

equals

public boolean equals(Object obj)
Overrides:
equals in class Object

hashCode

public int hashCode()
Overrides:
hashCode in class Object

toString

public String toString()
Overrides:
toString in class Object

This Javadoc may contain text parts from IETF Internet Standard specifications, see copyright note) and RSA Data Security Public-Key Cryptography Standards (see copyright note).

IAIK-JCE ME 3.04, (c) 2002 IAIK, (c) 2003 to 2006 Stiftung SIC