org.osesb.utilities.encryption
Class JCESymmetricEncryptionHelper

java.lang.Object
  extended by org.osesb.utilities.encryption.JCESymmetricEncryptionHelper
Direct Known Subclasses:
XMLSymmetricEncryptionHelper

public class JCESymmetricEncryptionHelper
extends java.lang.Object

This class contains utility methods for encryption/decryption using symmetric keys with JCE.

Construct this Class with an EncryptionProviderConfiguration or use getJCESymmetricEncryptionHelper() to get a JCEEncryptionHelper using a EncryptionProviderConfiguration file that has been configured for the framework.

An EncryptionProviderConfiguration file looks like:

<encryption-provider-configuration>
      <providerClassName>com.sun.crypto.provider.SunJCE</providerClassName>
      <encryptionAlgorithm>DES</encryptionAlgorithm>
      <keyFilePath>org/osesb/utilities/encryption/test/testdata/jce.des.encryption.test.key<</keyFilePath>
  </encryption-provider-configuration>
 

Methods in this Class include:

Since:
Version .9

Field Summary
static java.lang.String DEFAULT_ENCRYPTION_PROVIDER_CONFIGFILE
          The default EncryptionProviderConfiguration file for JCEEncryptionHelper(s) returned by getInstance().
static java.lang.String ENCRYPTION_PROVIDER_CONFIGFILE_PROPERTY
          The system property key used to set default EncryptionProviderConfiguration for JCEEncryptionHelper(s) returned by getInstance().
protected  java.security.Key encryptionKey
          The encryption Key as read from the location given by EncryptionProviderConfiguration, if there is a location specified.
protected  EncryptionProviderConfiguration encryptionProviderConfiguration
          The EncryptionProviderConfiguration for this helper.
protected static org.apache.commons.logging.Log log
          The logging implementation for this class.
protected  java.lang.String thisClassName
          The full name of this class.
 
Constructor Summary
JCESymmetricEncryptionHelper(EncryptionProviderConfiguration encryptionProviderConfiguration)
          Creates a new instance of JCEEncryptionHelper with the EncryptionProviderConfiguration.
 
Method Summary
 java.lang.String decryptBase64String(java.lang.String input)
          De-encrypts a Base64 encoded String as created by encryptToBase64String().
 byte[] decryptByteArray(byte[] input)
          De-encrypts a byte[].
 byte[] encryptByteArray(byte[] input)
          Encrypts a byte[].
 java.lang.String encryptToBase64String(java.lang.String input)
          Encrypts an input String and returns Base64 encoded representation of the encrypted String.
 java.security.Key generateKey()
          Generates a symmetric encryption Key (java.security.Key).
 java.security.Key getEncryptionKey()
          Returns the encryptionKey for this helper as a java.Security.Key
static JCESymmetricEncryptionHelper getJCESymmetricEncryptionHelper()
          Construct and return a new instance of JCEEncryptionHelper, using the following ordered lookup:

1.

 java.lang.String getProvidersInfo()
          Returns a human-readable description of the Providers registered with the Security object.
static void main(java.lang.String[] args)
          main() for sandboxing and testing.
 java.security.Key readKeyObjectFromFile(java.io.File file)
          Reads an encryption Key (java.security.Key) from a File.
 java.security.Key readKeyObjectFromPath(java.lang.String path)
          Reads an encryption Key (java.security.Key) from a path.
 java.security.Key readKeyObjectFromResource(java.lang.String resourcePath)
          Reads an encryption Key (java.security.Key) from a resource in a classLoader sensitive manner.
 void setEncryptionKey(java.security.Key encryptionKey)
          Sets the encryptionKey for this helper.
 void writeKeyObject(java.security.Key key, java.io.File file)
          Writes an encryption Key (java.security.Key) to a File.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

protected static org.apache.commons.logging.Log log
The logging implementation for this class.


thisClassName

protected java.lang.String thisClassName
The full name of this class.


encryptionProviderConfiguration

protected EncryptionProviderConfiguration encryptionProviderConfiguration
The EncryptionProviderConfiguration for this helper.


encryptionKey

protected java.security.Key encryptionKey
The encryption Key as read from the location given by EncryptionProviderConfiguration, if there is a location specified.


ENCRYPTION_PROVIDER_CONFIGFILE_PROPERTY

public static final java.lang.String ENCRYPTION_PROVIDER_CONFIGFILE_PROPERTY
The system property key used to set default EncryptionProviderConfiguration for JCEEncryptionHelper(s) returned by getInstance().

See Also:
Constant Field Values

DEFAULT_ENCRYPTION_PROVIDER_CONFIGFILE

public static final java.lang.String DEFAULT_ENCRYPTION_PROVIDER_CONFIGFILE
The default EncryptionProviderConfiguration file for JCEEncryptionHelper(s) returned by getInstance().

See Also:
Constant Field Values
Constructor Detail

JCESymmetricEncryptionHelper

public JCESymmetricEncryptionHelper(EncryptionProviderConfiguration encryptionProviderConfiguration)
                             throws java.lang.Exception
Creates a new instance of JCEEncryptionHelper with the EncryptionProviderConfiguration.

Parameters:
encryptionProviderConfiguration - The EncryptionProviderConfiguration object to use for this helper.
Throws:
java.lang.Exception - if constructed with null EncryptionProviderConfiguration or if there is a problem loading the encryption provider or reading the encryption key.
Method Detail

getProvidersInfo

public java.lang.String getProvidersInfo()
Returns a human-readable description of the Providers registered with the Security object.

Returns:
a String[] where each entry is a human-readable description of a Providers that is registered with the Security object.

getJCESymmetricEncryptionHelper

public static JCESymmetricEncryptionHelper getJCESymmetricEncryptionHelper()
                                                                    throws java.lang.Exception
Construct and return a new instance of JCEEncryptionHelper, using the following ordered lookup:

1. The System Property defined by ENCRYPTION_PROVIDER_CONFIGFILE_PROPERTY is used to determine the EncryptionProviderConfiguration file, which is used to construct a new instance of JCEEncryptionHelper.

2. The default EncryptionProviderConfiguration file, DEFAULT_ENCRYPTION_PROVIDER_CONFIGFILE, is used to construct a new instance of JCEEncryptionHelper.

Returns:
a new instance of JCEEncryptionHelper.
Throws:
java.lang.Exception - if an instance of JCEEncryptionHelper cannot be returned.

encryptToBase64String

public java.lang.String encryptToBase64String(java.lang.String input)
                                       throws java.lang.Exception
Encrypts an input String and returns Base64 encoded representation of the encrypted String.

Parameters:
input - The String to encrypt.
Returns:
the Base64 encoded representation of the encrypted String
Throws:
java.lang.Exception - if called with null parameters or if there is a problem encrypting the input or Base64 encoding the encryption result.

decryptBase64String

public java.lang.String decryptBase64String(java.lang.String input)
                                     throws java.lang.Exception
De-encrypts a Base64 encoded String as created by encryptToBase64String().

Parameters:
input - The Base64 encoded representation of an encrypted String.
Returns:
The un-encrypted String
Throws:
java.lang.Exception - if called with null parameters or if there is a problem encountered during de-cryption.

encryptByteArray

public byte[] encryptByteArray(byte[] input)
                        throws java.lang.Exception
Encrypts a byte[].

Parameters:
input - The byte[] to encrypt.
Returns:
the encrypted byte[].
Throws:
java.lang.Exception - if called with null parameters or if there is a problem encrypting the byte[].

decryptByteArray

public byte[] decryptByteArray(byte[] input)
                        throws java.lang.Exception
De-encrypts a byte[].

Parameters:
input - The byte[] to de-crypt.
Returns:
The de-crypted byte[]
Throws:
java.lang.Exception - if called with null parameters or if there is a problem encountered during de-cryption.

generateKey

public java.security.Key generateKey()
                              throws java.lang.Exception
Generates a symmetric encryption Key (java.security.Key).

Returns:
an encryption Key
Throws:
java.lang.Exception - If there is a problem generating the Key.

writeKeyObject

public void writeKeyObject(java.security.Key key,
                           java.io.File file)
                    throws java.lang.Exception
Writes an encryption Key (java.security.Key) to a File.

Parameters:
key - The Key to write.
file - The File to write the key to.
Throws:
java.lang.Exception - if called with null parameters or if there is a problem writing the Key to the File.

readKeyObjectFromPath

public java.security.Key readKeyObjectFromPath(java.lang.String path)
                                        throws java.lang.Exception
Reads an encryption Key (java.security.Key) from a path. The path can be a file system path, a file path as a URL, or a resource path.

Parameters:
path - The path to read the Key from.
Returns:
The Key that was read.
Throws:
java.lang.Exception - if called with null parameters or if there is a problem reading the Key.

readKeyObjectFromFile

public java.security.Key readKeyObjectFromFile(java.io.File file)
                                        throws java.lang.Exception
Reads an encryption Key (java.security.Key) from a File.

Parameters:
file - The File to read the Key from.
Returns:
The Key that was read from the File.
Throws:
java.lang.Exception - if called with null parameters or if there is a problem reading the Key from the File.

readKeyObjectFromResource

public java.security.Key readKeyObjectFromResource(java.lang.String resourcePath)
                                            throws java.lang.Exception
Reads an encryption Key (java.security.Key) from a resource in a classLoader sensitive manner.

Parameters:
resourcePath - The path to the resource to read the Key from.
Returns:
The Key that was read from the resource.
Throws:
java.lang.Exception - if called with null parameters or if there is a problem reading the Key from the resource.

getEncryptionKey

public java.security.Key getEncryptionKey()
Returns the encryptionKey for this helper as a java.Security.Key

Returns:
the encryptionKey for this helper.

setEncryptionKey

public void setEncryptionKey(java.security.Key encryptionKey)
Sets the encryptionKey for this helper.

Parameters:
encryptionKey - The java.security.Key for this helper.

main

public static void main(java.lang.String[] args)
main() for sandboxing and testing.

Parameters:
args - commandline arguments