public abstract class Encrypt extends Object
© Copyright 2007 Sans Pareil Technologies, Inc.
| Modifier and Type | Field and Description |
|---|---|
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.
|
| Modifier | Constructor and Description |
|---|---|
protected |
Encrypt()
Creates a new instance of Encrypt
|
protected |
Encrypt(int length)
Designated constructor.
|
| Modifier and Type | Method and Description |
|---|---|
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. |
byte[] |
toBytes(String hexString)
Convert the hex encoded string to original byte array.
|
String |
toHex(byte[] bytes)
Convert the specified byte array to hex characters.
|
public static final int KEY_LENGTH
public static final String ENCODING
protected final int keyLength
protected Encrypt()
protected Encrypt(int length)
length - The length of the key to use for encryption and decryptionpublic byte[] encrypt(String key, String value) throws InvalidKeyException, BadPaddingException, RuntimeException
value using the specified
key.key - The key to use to encrypt the value.value - The value that is to be encrypted.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.crypt(java.lang.String, byte[], int)public String decrypt(String key, byte[] value) throws InvalidKeyException, BadPaddingException, RuntimeException
value using the specified
key.key - The key to use to decrypt the value.value - The value that is to be decrypted.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.crypt(java.lang.String, byte[], int)public String toHex(byte[] bytes)
bytes - The byte array to convert into hex characters.public byte[] toBytes(String hexString)
hexString - The hex encoded string to re-convert to byte arrayprotected byte[] getKey(String key)
key. Ensure
that the returned key is of the requisite length for the
encryption scheme.key - The key that is to be corrected if necessary.key to use as a byte array.RuntimeException - If UnsupportedEncodingException is raised on
converting corrected key to ENCODING.protected RuntimeException handleException(Throwable throwable)
RuntimeException wrapping the specified throwable.throwable - The throwable instance that is to be wrapped.throwable.protected abstract byte[] crypt(String key, byte[] value, int mode) throws InvalidKeyException, BadPaddingException, RuntimeException
value using the specified
key.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).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.