goldengate.common.crypto
Class KeyObject

java.lang.Object
  extended by goldengate.common.crypto.KeyObject
Direct Known Subclasses:
Aes, Blowfish, Des, DynamicKeyObject

public abstract class KeyObject
extends Object

This class handles method to crypt and decrypt using the chosen algorithm.

Usage:

Author:
frederic bregier

Constructor Summary
KeyObject()
          Empty constructor
 
Method Summary
 byte[] crypt(byte[] plaintext)
          Crypt one array of bytes and returns the crypted array of bytes
 byte[] crypt(String plaintext)
          Crypt one String and returns the crypted array of bytes
 String cryptToHex(byte[] plaintext)
          Crypt one array of bytes and returns the crypted String as HEX format
 String cryptToHex(String plaintext)
          Crypt one String and returns the crypted String as HEX format
 byte[] decodeHex(String encoded)
           
 byte[] decrypt(byte[] ciphertext)
          Decrypt an array of bytes and returns the uncrypted array of bytes
 byte[] decryptHexFile(File file)
          Decode from a file containing a HEX crypted string
 byte[] decryptHexInBytes(byte[] ciphertext)
          Decrypt an array of bytes as HEX format representing a crypted array of bytes and returns the uncrypted array of bytes
 byte[] decryptHexInBytes(String ciphertext)
          Decrypt a String as HEX format representing a crypted array of bytes and returns the uncrypted array of bytes
 String decryptHexInString(String ciphertext)
          Decrypt a String as HEX format representing a crypted array of bytes and returns the uncrypted String
 String decryptInString(byte[] ciphertext)
          Decrypt an array of bytes and returns the uncrypted String
 String encodeHex(byte[] bytes)
           
 void generateKey()
          Generate a key from nothing
abstract  String getAlgorithm()
           
abstract  String getInstance()
           
abstract  int getKeySize()
           
 Key getSecretKey()
           
 byte[] getSecretKeyInBytes()
          Returns the key as an array of bytes in order to be stored somewhere else and retrieved using the setSecretKey(byte[] keyData) method.
 boolean keyReady()
           
 void saveSecretKey(File file)
          Save a Key to a File
 void setSecretKey(byte[] keyData)
          Reconstruct a key from an array of bytes
 void setSecretKey(File file)
          Create a Key from a File
 void setSecretKey(Key secretKey)
          Set the secretKey
 Cipher toCrypt()
          Returns a cipher for encryption associated with the key
 Cipher toDecrypt()
          Returns a cipher for decryption associated with the key
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

KeyObject

public KeyObject()
Empty constructor

Method Detail

getAlgorithm

public abstract String getAlgorithm()
Returns:
the algorithm used (Java name)

getInstance

public abstract String getInstance()
Returns:
the instance used (Java name)

getKeySize

public abstract int getKeySize()
Returns:
the size for the algorithm key

getSecretKey

public Key getSecretKey()
Returns:
the key associated with this object

keyReady

public boolean keyReady()
Returns:
True if this key is ready to be used

getSecretKeyInBytes

public byte[] getSecretKeyInBytes()
Returns the key as an array of bytes in order to be stored somewhere else and retrieved using the setSecretKey(byte[] keyData) method.

Returns:
the key as an array of bytes (or null if not ready)

setSecretKey

public void setSecretKey(Key secretKey)
Set the secretKey

Parameters:
secretKey -

setSecretKey

public void setSecretKey(byte[] keyData)
Reconstruct a key from an array of bytes


setSecretKey

public void setSecretKey(File file)
                  throws CryptoException,
                         IOException
Create a Key from a File

Parameters:
file -
Throws:
CryptoException
IOException

saveSecretKey

public void saveSecretKey(File file)
                   throws CryptoException,
                          IOException
Save a Key to a File

Parameters:
file -
Throws:
CryptoException
IOException

generateKey

public void generateKey()
                 throws Exception
Generate a key from nothing

Throws:
Exception

toCrypt

public Cipher toCrypt()
Returns a cipher for encryption associated with the key

Returns:
the cipher for encryption or null if it fails in case Encryption method or key is incorrect

crypt

public byte[] crypt(byte[] plaintext)
             throws Exception
Crypt one array of bytes and returns the crypted array of bytes

Parameters:
plaintext -
Returns:
the crypted array of bytes
Throws:
Exception

cryptToHex

public String cryptToHex(byte[] plaintext)
                  throws Exception
Crypt one array of bytes and returns the crypted String as HEX format

Parameters:
plaintext -
Returns:
the crypted String as HEX format
Throws:
Exception

crypt

public byte[] crypt(String plaintext)
             throws Exception
Crypt one String and returns the crypted array of bytes

Parameters:
plaintext -
Returns:
the crypted array of bytes
Throws:
Exception

cryptToHex

public String cryptToHex(String plaintext)
                  throws Exception
Crypt one String and returns the crypted String as HEX format

Parameters:
plaintext -
Returns:
the crypted String as HEX format
Throws:
Exception

toDecrypt

public Cipher toDecrypt()
Returns a cipher for decryption associated with the key

Returns:
the cipher for decryption or null if it fails in case Encryption method or key is incorrect

decrypt

public byte[] decrypt(byte[] ciphertext)
               throws Exception
Decrypt an array of bytes and returns the uncrypted array of bytes

Parameters:
ciphertext -
Returns:
the uncrypted array of bytes
Throws:
Exception

decryptInString

public String decryptInString(byte[] ciphertext)
                       throws Exception
Decrypt an array of bytes and returns the uncrypted String

Parameters:
ciphertext -
Returns:
the uncrypted array of bytes
Throws:
Exception

decryptHexInBytes

public byte[] decryptHexInBytes(String ciphertext)
                         throws Exception
Decrypt a String as HEX format representing a crypted array of bytes and returns the uncrypted array of bytes

Parameters:
ciphertext -
Returns:
the uncrypted array of bytes
Throws:
Exception

decryptHexInBytes

public byte[] decryptHexInBytes(byte[] ciphertext)
                         throws Exception
Decrypt an array of bytes as HEX format representing a crypted array of bytes and returns the uncrypted array of bytes

Parameters:
ciphertext -
Returns:
the uncrypted array of bytes
Throws:
Exception

decryptHexInString

public String decryptHexInString(String ciphertext)
                          throws Exception
Decrypt a String as HEX format representing a crypted array of bytes and returns the uncrypted String

Parameters:
ciphertext -
Returns:
the uncrypted String
Throws:
Exception

decryptHexFile

public byte[] decryptHexFile(File file)
                      throws Exception
Decode from a file containing a HEX crypted string

Parameters:
file -
Returns:
the decoded uncrypted content of the file
Throws:
Exception

decodeHex

public byte[] decodeHex(String encoded)
Parameters:
encoded -
Returns:
the array of bytes from encoded String (HEX)

encodeHex

public String encodeHex(byte[] bytes)
Parameters:
bytes -
Returns:
The encoded array of bytes in HEX


Copyright © 2009-2012 Frederic Bregier. All Rights Reserved.