SPT Core API

com.sptci.util
Class Encrypt

java.lang.Object
  extended by com.sptci.util.Encrypt
Direct Known Subclasses:
AESEncrypt, BlowfishEncrypt, TripleDESEncrypt

public abstract class Encrypt
extends Object

Abstract base class of all classes that implement common encryption routins.

© Copyright 2007 Sans Pareil Technologies, Inc.

Version:
$Id: Encrypt.java 4553 2008-12-24 10:34:16Z rakesh $
Author:
Rakesh Vidyadharan 2007-09-14

Field Summary
static String ENCODING
          The default encoding to use to convert between strings and bytes.
static int KEY_LENGTH
          The length of the key used to encrypt the password.
protected  int keyLength
          The length of the keys used by this instance for encryption and decryption.
 
Constructor Summary
protected Encrypt()
          Creates a new instance of Encrypt
protected Encrypt(int length)
          Designated constructor.
 
Method Summary
protected abstract  byte[] crypt(String key, byte[] value, int mode)
          Encrypt/decrypt the specified value using the specified key.
 String decrypt(String key, byte[] value)
          Decrypt the specified value using the specified key.
 byte[] encrypt(String key, String value)
          Encrypt the specified value using the specified key.
protected  byte[] getKey(String key)
          Return a cleaned up key based on the specified key.
protected  RuntimeException handleException(Throwable throwable)
          Throw a RuntimeException wrapping the specified throwable.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

KEY_LENGTH

public static final int KEY_LENGTH
The length of the key used to encrypt the password. Uses safe 128bit encryption. Other valid values would be 24 and 32. 16

See Also:
Constant Field Values

ENCODING

public static final String ENCODING
The default encoding to use to convert between strings and bytes. "UTF-8"

See Also:
Constant Field Values

keyLength

protected final int keyLength
The length of the keys used by this instance for encryption and decryption.

Constructor Detail

Encrypt

protected Encrypt()
Creates a new instance of Encrypt


Encrypt

protected Encrypt(int length)
Designated constructor. Create a new instance that uses the specified key length.

Parameters:
length - The length of the key to use for encryption and decryption
Method Detail

encrypt

public byte[] encrypt(String key,
                      String value)
               throws InvalidKeyException,
                      BadPaddingException,
                      RuntimeException
Encrypt the specified value using the specified key.

Parameters:
key - The key to use to encrypt the value.
value - The value that is to be encrypted.
Returns:
The encrypted bytes for the specified value using key.
Throws:
InvalidKeyException - If the given key is inappropriate for initializing the Cipher, or its keysize exceeds the maximum allowable keysize (as determined from the configured jurisdiction policy files).
BadPaddingException - Declared in underlying implementation but never raised in this method.
RuntimeException - If IllegalBlockSizeException, NoSuchPaddingException, NoSuchAlgorithmException are raised while encrypting.
See Also:
crypt(java.lang.String, byte[], int)

decrypt

public String decrypt(String key,
                      byte[] value)
               throws InvalidKeyException,
                      BadPaddingException,
                      RuntimeException
Decrypt the specified value using the specified key.

Parameters:
key - The key to use to decrypt the value.
value - The value that is to be decrypted.
Returns:
The decrypted value for the bytes.
Throws:
InvalidKeyException - If the given key is inappropriate for initializing the Cipher, or its keysize exceeds the maximum allowable keysize (as determined from the configured jurisdiction policy files).
BadPaddingException - If (un)padding has been requested, but the decrypted data is not bounded by the appropriate padding bytes.
RuntimeException - If IllegalBlockSizeException, NoSuchPaddingException, NoSuchAlgorithmException are raised while decrypting.
See Also:
crypt(java.lang.String, byte[], int)

getKey

protected byte[] getKey(String key)
Return a cleaned up key based on the specified key. Ensure that the returned key is of the requisite length for the encryption scheme.

Parameters:
key - The key that is to be corrected if necessary.
Returns:
The corrected key to use as a byte array.
Throws:
RuntimeException - If UnsupportedEncodingException is raised on converting corrected key to ENCODING.

handleException

protected RuntimeException handleException(Throwable throwable)
Throw a RuntimeException wrapping the specified throwable.

Parameters:
throwable - The throwable instance that is to be wrapped.
Returns:
The unchecked exception that wraps the throwable.

crypt

protected abstract byte[] crypt(String key,
                                byte[] value,
                                int mode)
                         throws InvalidKeyException,
                                BadPaddingException,
                                RuntimeException
Encrypt/decrypt the specified value using the specified key.

Parameters:
key - The key to use to encrypt the value.
value - The value that is to be encrypted or decrypted.
mode - The operation mode of this cipher (this is one of the following: Cipher.ENCRYPT_MODE, Cipher.DECRYPT_MODE, Cipher.WRAP_MODE or Cipher.UNWRAP_MODE).
Returns:
The encrypted bytes.
Throws:
InvalidKeyException - If the given key is inappropriate for initializing the Cipher, or its keysize exceeds the maximum allowable keysize (as determined from the configured jurisdiction policy files).
BadPaddingException - Declared in underlying implementation but never raised in this method.
RuntimeException - If IllegalBlockSizeException, NoSuchPaddingException, NoSuchAlgorithmException are raised while encrypting or decrypting.

SPT Core API