| java.lang.Object org.jaffa.security.EncryptionHelper
EncryptionHelper | public class EncryptionHelper (Code) | | This class has some utility functions for encrypting objects using the
JCE Security Package.
Its main purpose is to be able to take a Object/String and encrypt it, and then
convert the encrypted data into a HexString, so that it can be passed arround as
a String, and hence used in URL's.
A good exmple of this is if you have an Object that you want to pass to a servlet,
then you can use this routine to get a HexString version of that object and pass
it accross in the URL as a paramater "data=1234567890ABC...", Data will not only be a serialization
of the object, it will also be encrypted with a SecretKey, that the recievoing servlet must use
when converting it back to an object.
The String version of this process is optimized to convert the String in to a UTF-8 byte array.
This results in a much smaller string then regular obejct serialization.
author: paule version: 1.0 |
Method Summary | |
public static SecretKey | createKey() This method can be used from the command line for creating a Secret Key. | public static String | encryptObjectForURL(Object source, SecretKey key) Creates an encrypted and encode string from the source object. | public static String | encryptStringForURL(String source, SecretKey key) Creates an encrypted and encode string from the source string. | public static byte | fromHex(char c) Utility function to convert a hex character to a number. | public static byte[] | fromHexString(String in) Convert a String of hex values into a byte[]. | public static Object | getObjectFromEncryptedURL(String data, SecretKey key) Get an Object from an Encoded and Encrypted String. | public static String | getStringFromEncryptedURL(String data, SecretKey key) Get a String from an Encoded and Encrypted String. | public static byte[] | intoBytes(String in) Converts a String (based on an 8-bit character set) into an byte array.
There will be one byte per charater in the string.
Parameters: in - The string to be converted throws: UnsupportedEncodingException - Is thrown if there are any unsupported characters in the string (ie. | public static byte[] | intoBytes16(String in) Converts a String into an byte array. | public static String | intoHexString(byte[] in) Converts a byte[] into a hex string representation. | public static String | intoString(byte[] in) Converts a byte array into a string. | public static String | intoString16(byte[] in) Converts a byte array into a string. | public static void | main(String args) This method can be used from the command line for creating a Secret Key. | public static SecretKey | readKey(File file) | public static SecretKey | readKeyClassPath(String name) | public static char | toHex(byte b) Utility function to convert a number into a hex character. |
ENCRYPT_POLICY | final public static String ENCRYPT_POLICY(Code) | | This is the encryption policy that will be used
|
createKey | public static SecretKey createKey()(Code) | | This method can be used from the command line for creating a Secret Key.
Returns the newley generated key, or null if there was an error. |
encryptStringForURL | public static String encryptStringForURL(String source, SecretKey key) throws java.security.NoSuchAlgorithmException, java.security.InvalidKeyException, javax.crypto.NoSuchPaddingException, java.io.UnsupportedEncodingException, javax.crypto.IllegalBlockSizeException, javax.crypto.BadPaddingException(Code) | | Creates an encrypted and encode string from the source string.
This string can be used directly in a URL without encoding.
Parameters: source - The source string to encrypt/encode Parameters: key - The secret key to use for encryption throws: NoSuchAlgorithmException - May be thrown by the Cypher module throws: InvalidKeyException - May be thrown by the Cypher module throws: NoSuchPaddingException - May be thrown by the Cypher module throws: UnsupportedEncodingException - May be thrown by the Cypher module throws: IllegalBlockSizeException - May be thrown by the Cypher module throws: BadPaddingException - May be thrown by the Cypher module The encoded/encrypted string |
fromHex | public static byte fromHex(char c)(Code) | | Utility function to convert a hex character to a number.
The character must be '0'..'F', the byte will be 0-15.
Parameters: c - The character to convert The number as a byte |
fromHexString | public static byte[] fromHexString(String in)(Code) | | Convert a String of hex values into a byte[]. Each two characters in the string
represent 1 byte.
Parameters: in - The hex string to be converted A byte[] of the real data |
getObjectFromEncryptedURL | public static Object getObjectFromEncryptedURL(String data, SecretKey key) throws java.security.NoSuchAlgorithmException, java.security.InvalidKeyException, javax.crypto.NoSuchPaddingException, javax.crypto.IllegalBlockSizeException, javax.crypto.BadPaddingException(Code) | | Get an Object from an Encoded and Encrypted String. This assumes that the
object can be recreated by de-serialization, and that the original class for the
object is accessable.
Parameters: data - The encoded/encrypted string to process Parameters: key - The secret key used needed to decrypt the string throws: NoSuchAlgorithmException - May be thrown by the Cypher module throws: InvalidKeyException - May be thrown by the Cypher module throws: NoSuchPaddingException - May be thrown by the Cypher module throws: IllegalBlockSizeException - May be thrown by the Cypher module throws: BadPaddingException - May be thrown by the Cypher module The real object that the data represents |
getStringFromEncryptedURL | public static String getStringFromEncryptedURL(String data, SecretKey key) throws java.security.NoSuchAlgorithmException, java.security.InvalidKeyException, javax.crypto.NoSuchPaddingException, javax.crypto.IllegalBlockSizeException, javax.crypto.BadPaddingException(Code) | | Get a String from an Encoded and Encrypted String.
Parameters: data - The encoded/encrypted string to process Parameters: key - The secret key used needed to decrypt the string throws: NoSuchAlgorithmException - May be thrown by the Cypher module throws: InvalidKeyException - May be thrown by the Cypher module throws: NoSuchPaddingException - May be thrown by the Cypher module throws: IllegalBlockSizeException - May be thrown by the Cypher module throws: BadPaddingException - May be thrown by the Cypher module The real string that the data represents |
intoBytes | public static byte[] intoBytes(String in) throws UnsupportedEncodingException(Code) | | Converts a String (based on an 8-bit character set) into an byte array.
There will be one byte per charater in the string.
Parameters: in - The string to be converted throws: UnsupportedEncodingException - Is thrown if there are any unsupported characters in the string (ie. greater that 8-bits) The byte[] for the string |
intoBytes16 | public static byte[] intoBytes16(String in)(Code) | | Converts a String into an byte array.
There will be two bytes per charater in the string.
Parameters: in - The string to be converted The byte[] for the string |
intoHexString | public static String intoHexString(byte[] in)(Code) | | Converts a byte[] into a hex string representation. Each byte will be represented
by a 2-digit hex number (00-FF).
Parameters: in - The byte[] to convert The string containing the Hex representation |
intoString | public static String intoString(byte[] in)(Code) | | Converts a byte array into a string. It assumes that 8-bits represents a byte.
There should there for be one character per byte.
Parameters: in - byte[] to be converted Converted string |
intoString16 | public static String intoString16(byte[] in)(Code) | | Converts a byte array into a string. It assumes that 16-bits represents a byte.
Parameters: in - byte[] to be converted Converted string |
main | public static void main(String args)(Code) | | This method can be used from the command line for creating a Secret Key.
Parameters: args - the command line argumentsRequires one mandatory parameter, which is the file name to use to write out the SecretKey |
readKeyClassPath | public static SecretKey readKeyClassPath(String name) throws IOException, ClassNotFoundException(Code) | | Read a file that should contain a serialized Secret key, the file
is read as a resource on the classpath
The secret key object Parameters: name - The resource name that points to the key file throws: ClassNotFoundException - If the SecretKey class is not available throws: IOException - If the specfied file can't be loaded |
toHex | public static char toHex(byte b)(Code) | | Utility function to convert a number into a hex character.
Takes the lowest 4 bits and converts it to a character '0'..'F'
Parameters: b - The byte to convert The Hex character |
|
|