de.upb.swtpra05.group03.crypto
Class Crypto

java.lang.Object
  extended byde.upb.swtpra05.group03.crypto.Crypto
Direct Known Subclasses:
AliceCrypto, BobCrypto

public abstract class Crypto
extends java.lang.Object

This is the main Crypto class. It is used to encrypt and decrypt messages.

Version:
$Revision: 1.3 $

Field Summary
protected static java.lang.String AGREEMENT_ALGORITHM
          Algorithm used for public/private key agreement
protected static java.lang.String CIPHER_ALGORITHM
          Algorithm used for symmetric en-/decryption
private  javax.crypto.Cipher decrypter
          Cipher object used for decryption
private  javax.crypto.Cipher encrypter
          Cipher object used for encryption
protected static java.lang.String KEY_ALGORITHM
          Algorithm used to create the secret key
protected  java.security.KeyFactory pubKeyFactory
          factory used to create public keys from x509 encoding
private  javax.crypto.SecretKey secretKey
          secret key with which data is en-/decrypted
protected static java.lang.String SIGNATURE_ALGORITHM
          Algorithm used for signing and verifying data
protected  java.security.Signature signer
          Signature object used for signing
protected  int state
          holds the state of this object
static int STATE_INIT
          Constant for State INIT
static int STATE_READY
          Constant for State READY
static int STATE_WAITING
          Constant for State WAITING
protected  java.security.Signature verifyer
          Signature object used for verifying signatures
 
Constructor Summary
Crypto()
          Constructor to create the Crypto object.
 
Method Summary
 java.lang.Object decryptObject(DataSigPair dsp)
          decrypts an bytearray to an object
 DataSigPair encryptObject(java.io.Serializable obj)
          encrypts an object
protected  java.security.PublicKey genPublicKeyFromX509(byte[] x509Key)
          generates a public key from the bytearray in the x509 format
 byte[] getSecretKey()
          gets the secret key
 boolean isReady()
          if state is READY
protected  void loadSigningKeys(java.lang.String name)
          loads public and private key with the SigningKeys class.
 void reset()
          reset the state to INIT so a new key agreement can be made
 void setSecretKey(byte[] key)
          to set the secret key directly without key agreement
protected  void setSecretKey(javax.crypto.SecretKey secretKey)
          sets the secret key directly without key agreement
protected  byte[] sign(byte[] data)
          signs data
protected  boolean verify(DataSigPair dsp)
          verifies signature for data
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

STATE_INIT

public static final int STATE_INIT
Constant for State INIT

See Also:
Constant Field Values

STATE_WAITING

public static final int STATE_WAITING
Constant for State WAITING

See Also:
Constant Field Values

STATE_READY

public static final int STATE_READY
Constant for State READY

See Also:
Constant Field Values

AGREEMENT_ALGORITHM

protected static final java.lang.String AGREEMENT_ALGORITHM
Algorithm used for public/private key agreement

See Also:
Constant Field Values

KEY_ALGORITHM

protected static final java.lang.String KEY_ALGORITHM
Algorithm used to create the secret key

See Also:
Constant Field Values

CIPHER_ALGORITHM

protected static final java.lang.String CIPHER_ALGORITHM
Algorithm used for symmetric en-/decryption

See Also:
Constant Field Values

SIGNATURE_ALGORITHM

protected static final java.lang.String SIGNATURE_ALGORITHM
Algorithm used for signing and verifying data

See Also:
Constant Field Values

state

protected int state
holds the state of this object


secretKey

private javax.crypto.SecretKey secretKey
secret key with which data is en-/decrypted


encrypter

private final javax.crypto.Cipher encrypter
Cipher object used for encryption


decrypter

private final javax.crypto.Cipher decrypter
Cipher object used for decryption


signer

protected final java.security.Signature signer
Signature object used for signing


verifyer

protected final java.security.Signature verifyer
Signature object used for verifying signatures


pubKeyFactory

protected final java.security.KeyFactory pubKeyFactory
factory used to create public keys from x509 encoding

Constructor Detail

Crypto

public Crypto()
       throws java.security.NoSuchAlgorithmException,
              javax.crypto.NoSuchPaddingException
Constructor to create the Crypto object.

Throws:
javax.crypto.NoSuchPaddingException
java.security.NoSuchAlgorithmException
Method Detail

loadSigningKeys

protected void loadSigningKeys(java.lang.String name)
                        throws java.security.NoSuchAlgorithmException,
                               java.security.spec.InvalidKeySpecException,
                               java.io.IOException,
                               java.security.InvalidKeyException
loads public and private key with the SigningKeys class. the public key is used by the verifyer and the secret one is used by the signer

Parameters:
name - have to be either "shuttle" or "company"
Throws:
java.security.NoSuchAlgorithmException
java.security.spec.InvalidKeySpecException
java.io.IOException
java.security.InvalidKeyException
See Also:
SigningKeys.loadKeys(String)

setSecretKey

public void setSecretKey(byte[] key)
                  throws java.io.IOException,
                         java.lang.ClassNotFoundException,
                         java.security.InvalidKeyException
to set the secret key directly without key agreement

Parameters:
key - a bytearray of the serialized SecretKey object
Throws:
java.io.IOException
java.lang.ClassNotFoundException
java.security.InvalidKeyException

setSecretKey

protected void setSecretKey(javax.crypto.SecretKey secretKey)
                     throws java.security.InvalidKeyException
sets the secret key directly without key agreement

Parameters:
secretKey - the new SecretKey
Throws:
java.security.InvalidKeyException

getSecretKey

public byte[] getSecretKey()
                    throws java.io.IOException
gets the secret key

Returns:
secret key serialized as a bytearray
Throws:
java.io.IOException

reset

public void reset()
reset the state to INIT so a new key agreement can be made


encryptObject

public DataSigPair encryptObject(java.io.Serializable obj)
                          throws java.io.IOException,
                                 java.security.SignatureException
encrypts an object

Parameters:
obj - object wich implements Serializable
Returns:
encrypted object and the coresponding signature
Throws:
java.io.IOException
java.security.SignatureException
java.security.SignatureException

decryptObject

public java.lang.Object decryptObject(DataSigPair dsp)
                               throws java.lang.IllegalStateException,
                                      java.io.IOException,
                                      java.lang.ClassNotFoundException,
                                      java.security.SignatureException
decrypts an bytearray to an object

Parameters:
dsp - object containing data and signature
Returns:
decrypted object
Throws:
java.lang.IllegalStateException
java.io.IOException
java.lang.ClassNotFoundException
java.security.SignatureException

sign

protected byte[] sign(byte[] data)
               throws java.security.SignatureException
signs data

Parameters:
data - a bytearray
Returns:
signature for data
Throws:
java.security.SignatureException

verify

protected boolean verify(DataSigPair dsp)
                  throws java.security.SignatureException
verifies signature for data

Parameters:
dsp - object containing data and signature
Returns:
true if the signature was verified, false if not.
Throws:
java.security.SignatureException

isReady

public boolean isReady()
if state is READY

Returns:
if state is READY

genPublicKeyFromX509

protected java.security.PublicKey genPublicKeyFromX509(byte[] x509Key)
                                                throws java.security.spec.InvalidKeySpecException
generates a public key from the bytearray in the x509 format

Parameters:
x509Key - public key in the x509 format
Returns:
public key decoded from the x509 format
Throws:
java.security.spec.InvalidKeySpecException