org.jaffa.security
Class EncryptionHelper

java.lang.Object
  extended byorg.jaffa.security.EncryptionHelper

public class EncryptionHelper
extends Object

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.

Version:
1.0
Author:
paule

Field Summary
static String ENCRYPT_POLICY
          This is the encryption policy that will be used
 
Constructor Summary
EncryptionHelper()
           
 
Method Summary
static SecretKey createKey()
          This method can be used from the command line for creating a Secret Key.
static String encryptObjectForURL(Object source, SecretKey key)
          Creates an encrypted and encode string from the source object.
static String encryptStringForURL(String source, SecretKey key)
          Creates an encrypted and encode string from the source string.
static byte fromHex(char c)
          Utility function to convert a hex character to a number.
static byte[] fromHexString(String in)
          Convert a String of hex values into a byte[].
static Object getObjectFromEncryptedURL(String data, SecretKey key)
          Get an Object from an Encoded and Encrypted String.
static String getStringFromEncryptedURL(String data, SecretKey key)
          Get a String from an Encoded and Encrypted String.
static byte[] intoBytes(String in)
          Converts a String (based on an 8-bit character set) into an byte array.
static byte[] intoBytes16(String in)
          Converts a String into an byte array.
static String intoHexString(byte[] in)
          Converts a byte[] into a hex string representation.
static String intoString(byte[] in)
          Converts a byte array into a string.
static String intoString16(byte[] in)
          Converts a byte array into a string.
static void main(String[] args)
          This method can be used from the command line for creating a Secret Key.
static SecretKey readKey(File file)
          Read a file that should contain a serialized Secret key
static SecretKey readKeyClassPath(String name)
          Read a file that should contain a serialized Secret key, the file is read as a resource on the classpath
static char toHex(byte b)
          Utility function to convert a number into a hex character.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ENCRYPT_POLICY

public static final String ENCRYPT_POLICY
This is the encryption policy that will be used

See Also:
Constant Field Values
Constructor Detail

EncryptionHelper

public EncryptionHelper()
Method Detail

main

public static void main(String[] args)
This method can be used from the command line for creating a Secret Key.

Parameters:
args - the command line arguments Requires one mandatory parameter, which is the file name to use to write out the SecretKey

createKey

public static SecretKey createKey()
This method can be used from the command line for creating a Secret Key.

Returns:
Returns the newley generated key, or null if there was an error.

readKey

public static SecretKey readKey(File file)
                         throws IOException,
                                ClassNotFoundException
Read a file that should contain a serialized Secret key

Parameters:
file - The file object that points to the key file
Returns:
The secret key object
Throws:
ClassNotFoundException - If the SecretKey class is not available
IOException - If the specfied file can't be loaded

readKeyClassPath

public static SecretKey readKeyClassPath(String name)
                                  throws IOException,
                                         ClassNotFoundException
Read a file that should contain a serialized Secret key, the file is read as a resource on the classpath

Parameters:
name - The resource name that points to the key file
Returns:
The secret key object
Throws:
ClassNotFoundException - If the SecretKey class is not available
IOException - If the specfied file can't be loaded

encryptStringForURL

public static String encryptStringForURL(String source,
                                         SecretKey key)
                                  throws NoSuchAlgorithmException,
                                         InvalidKeyException,
                                         NoSuchPaddingException,
                                         UnsupportedEncodingException,
                                         IllegalBlockSizeException,
                                         BadPaddingException
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
key - The secret key to use for encryption
Returns:
The encoded/encrypted string
Throws:
NoSuchAlgorithmException - May be thrown by the Cypher module
InvalidKeyException - May be thrown by the Cypher module
NoSuchPaddingException - May be thrown by the Cypher module
UnsupportedEncodingException - May be thrown by the Cypher module
IllegalBlockSizeException - May be thrown by the Cypher module
BadPaddingException - May be thrown by the Cypher module

getStringFromEncryptedURL

public static String getStringFromEncryptedURL(String data,
                                               SecretKey key)
                                        throws NoSuchAlgorithmException,
                                               InvalidKeyException,
                                               NoSuchPaddingException,
                                               IllegalBlockSizeException,
                                               BadPaddingException
Get a String from an Encoded and Encrypted String.

Parameters:
data - The encoded/encrypted string to process
key - The secret key used needed to decrypt the string
Returns:
The real string that the data represents
Throws:
NoSuchAlgorithmException - May be thrown by the Cypher module
InvalidKeyException - May be thrown by the Cypher module
NoSuchPaddingException - May be thrown by the Cypher module
IllegalBlockSizeException - May be thrown by the Cypher module
BadPaddingException - May be thrown by the Cypher module

encryptObjectForURL

public static String encryptObjectForURL(Object source,
                                         SecretKey key)
                                  throws NoSuchAlgorithmException,
                                         InvalidKeyException,
                                         NoSuchPaddingException,
                                         UnsupportedEncodingException,
                                         IllegalBlockSizeException,
                                         BadPaddingException,
                                         NotSerializableException
Creates an encrypted and encode string from the source object. This string can be used directly in a URL without encoding. This assumes that the object passed in can be serialized.

Parameters:
source - The source Object to encrypt/encode
key - The secret key to use for encryption
Returns:
The encoded/encrypted string
Throws:
NoSuchAlgorithmException - May be thrown by the Cypher module
InvalidKeyException - May be thrown by the Cypher module
NoSuchPaddingException - May be thrown by the Cypher module
UnsupportedEncodingException - May be thrown by the Cypher module
IllegalBlockSizeException - May be thrown by the Cypher module
BadPaddingException - May be thrown by the Cypher module
NotSerializableException - if the source object is not Serializable

getObjectFromEncryptedURL

public static Object getObjectFromEncryptedURL(String data,
                                               SecretKey key)
                                        throws NoSuchAlgorithmException,
                                               InvalidKeyException,
                                               NoSuchPaddingException,
                                               IllegalBlockSizeException,
                                               BadPaddingException
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
key - The secret key used needed to decrypt the string
Returns:
The real object that the data represents
Throws:
NoSuchAlgorithmException - May be thrown by the Cypher module
InvalidKeyException - May be thrown by the Cypher module
NoSuchPaddingException - May be thrown by the Cypher module
IllegalBlockSizeException - May be thrown by the Cypher module
BadPaddingException - May be thrown by the Cypher module

intoBytes

public static byte[] intoBytes(String in)
                        throws UnsupportedEncodingException
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
Returns:
The byte[] for the string
Throws:
UnsupportedEncodingException - Is thrown if there are any unsupported characters in the string (ie. greater that 8-bits)

intoString

public static String intoString(byte[] in)
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
Returns:
Converted string

intoBytes16

public static byte[] intoBytes16(String in)
Converts a String into an byte array. There will be two bytes per charater in the string.

Parameters:
in - The string to be converted
Returns:
The byte[] for the string

intoString16

public static String intoString16(byte[] in)
Converts a byte array into a string. It assumes that 16-bits represents a byte.

Parameters:
in - byte[] to be converted
Returns:
Converted string

intoHexString

public static String intoHexString(byte[] in)
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
Returns:
The string containing the Hex representation

fromHexString

public static byte[] fromHexString(String in)
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
Returns:
A byte[] of the real data

toHex

public static char toHex(byte b)
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
Returns:
The Hex character

fromHex

public static byte fromHex(char c)
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
Returns:
The number as a byte


Copyright © 2002-2004 JAFFA Project.