com.digitalsanctuary.atg.crypto
Class AbstractEncryptor

java.lang.Object
  extended bycom.digitalsanctuary.atg.crypto.AbstractEncryptor
All Implemented Interfaces:
java.io.Serializable
Direct Known Subclasses:
AESEncryptor

public abstract class AbstractEncryptor
extends java.lang.Object
implements java.io.Serializable

Author:
Devon Hillard
See Also:
Serialized Form

Field Summary
static java.lang.String CHAR_SET
          This defines the character set which is used explicitly during byte[]<-->String conversions.
 
Constructor Summary
AbstractEncryptor(byte[] pKey)
          This constructor takes in a cipher key in the form of a byte array.
AbstractEncryptor(java.lang.String pKey)
          This constructor takes in a cipher key in the form of a String.
 
Method Summary
 java.lang.String computeMAC(java.lang.String pClearText)
          This method computes the MAC message signature for the clear text.
protected  byte[] decrypt(byte[] pBytesToDecrypt)
          This method takes in a byte array and decrypts it using the AES algorithm and the key used to setup this class.
 java.lang.String decrypt(java.lang.String pStringToDecrypt)
          This method wraps the byte array based encrypt and allows you to pass in a String, which it decodes to a byte array.
protected  byte[] encrypt(byte[] pBytesToEncrypt)
          This method takes in a byte array and encrypts it using the AES algorithm and the key used to setup this class.
 java.lang.String encrypt(java.lang.String pStringToEncrypt)
          This method wraps the byte array based encrypt and allows you to pass in a String, which it decodes to a byte array.
protected abstract  java.lang.String getAlgorithmName()
          The Algorithm name for the Encryptor.
 void setKey(byte[] pKey)
          This method takes in a cipher key byte array, sets it into this class's member variable KeyBytes, and then calls initialize() to initialize the encryption and decryption Cipher instances with this new key.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CHAR_SET

public static final java.lang.String CHAR_SET
This defines the character set which is used explicitly during byte[]<-->String conversions.

See Also:
Constant Field Values
Constructor Detail

AbstractEncryptor

public AbstractEncryptor(byte[] pKey)
                  throws EncryptorException
This constructor takes in a cipher key in the form of a byte array. This should be 128, 192, or 256 bits long, with 256 bits (32 bytes) being strong recommended.

Parameters:
pKey - byte array of the cipher key. 32 bytes/256 bits recommended.
Throws:
EncryptorException - if the key cannot be used to setup the encryption and decryption Cipher instances.

AbstractEncryptor

public AbstractEncryptor(java.lang.String pKey)
                  throws EncryptorException
This constructor takes in a cipher key in the form of a String. This should be 128, 192, or 256 bits long, with 256 bits (32 characters) being strong recommended.

Parameters:
pKey - String of the cipher key. 32 characters/256 bits recommended.
Throws:
EncryptorException - if the key cannot be used to setup the encryption and decryption Cipher instances.
Method Detail

computeMAC

public java.lang.String computeMAC(java.lang.String pClearText)
                            throws EncryptorException
This method computes the MAC message signature for the clear text. This is basically a checksum so you know you decrypted the data successfully and that it wasn't tampered with.

Parameters:
pClearText - the plain text you wish to compute the MAC for. This must match the plain text you encrypt.
Returns:
a String, which is a Base64 encoded version of the checksum signature of the cleartext, based on the current algorithm and key.
Throws:
EncryptorException - if the key is invalid, if the algorithm is improperly defined, or if the encoding fails.

decrypt

public final java.lang.String decrypt(java.lang.String pStringToDecrypt)
                               throws EncryptorException
This method wraps the byte array based encrypt and allows you to pass in a String, which it decodes to a byte array. The encrypted resulting byte array is then encoded into a String and returned.

Parameters:
pStringToDecrypt - The String you wish to decrypt with this Encryptor
Returns:
The decryped version of the input String, after it was decoded into a byte array using Base64 decoding. The decrypted byte array was then encoded back into a String and returns here.
Throws:
EncryptorException - if the String to decrypt could not be decoded using Base64.

encrypt

public final java.lang.String encrypt(java.lang.String pStringToEncrypt)
                               throws EncryptorException
This method wraps the byte array based encrypt and allows you to pass in a String, which it decodes to a byte array. The encrypted resulting byte array is then encoded into a String and returned.

Parameters:
pStringToEncrypt - The String you wish to encrypt with this AESEncryptor
Returns:
The encryped version of the input String, encoded into a String using Base64 encoding.
Throws:
EncryptorException - if the encrypted bytes could not be encoded using Base64.

setKey

public final void setKey(byte[] pKey)
                  throws EncryptorException
This method takes in a cipher key byte array, sets it into this class's member variable KeyBytes, and then calls initialize() to initialize the encryption and decryption Cipher instances with this new key.

Parameters:
pKey - byte array of the cipher key. 32 bytes/256 bits recommended.
Throws:
EncryptorException - if initialization fails, typically due to a bad key.

decrypt

protected final byte[] decrypt(byte[] pBytesToDecrypt)
                        throws EncryptorException
This method takes in a byte array and decrypts it using the AES algorithm and the key used to setup this class.

Parameters:
pBytesToDecrypt - the byte array to be decrypted using AES and the key in mKeyBytes
Returns:
a byte array of decrypted data
Throws:
EncryptorException - if decryption fails.

encrypt

protected final byte[] encrypt(byte[] pBytesToEncrypt)
                        throws EncryptorException
This method takes in a byte array and encrypts it using the AES algorithm and the key used to setup this class.

Parameters:
pBytesToEncrypt - the byte array to be encrypted using AES and the key in mKeyBytes
Returns:
a byte array of encrypted data
Throws:
EncryptorException - if encryption fails.

getAlgorithmName

protected abstract java.lang.String getAlgorithmName()
The Algorithm name for the Encryptor.

Returns:
the Algorithm name for this instance of the encryptor.