public class DesEncrypt { public static final String DESEDE_ENCRYPTION_SCHEME = "DESede"; public static final String DES_ENCRYPTION_SCHEME = "DES"; private static String ENCRYPTION_SCHEME = "DESede"; private static String DEFAULT_ENCRYPTION_KEY = "This is a fairly long phrase used to encrypt"; private KeySpec keySpec; private SecretKeyFactory keyFactory; private Cipher cipher; private static DesEncrypt desede; private static DesEncrypt des; private static final String UNICODE_FORMAT = "UTF8"; private static final Log log = LogFactory.getLog(DesEncrypt.class);
private static DesEncrypt getDesEdeInstance()throws Exception{ if(desede == null){ ENCRYPTION_SCHEME = DESEDE_ENCRYPTION_SCHEME; desede = new DesEncrypt(); } return desede; } private static DesEncrypt getDesInstance()throws Exception{ if(des == null){ ENCRYPTION_SCHEME = DES_ENCRYPTION_SCHEME; des = new DesEncrypt(); } return des; } /** *@Purpose : this function provide get encrypt string functions *@param : String *@return :String *@throws Exception *@author : Yilong Xiao *@Contributing Author: N/A *@version 1.0 */
public static String desEdeEncrypt(String encrypt)throws Exception{ return getDesEdeInstance().encrypt(encrypt); }
/** *@Purpose : this function provide get decrypt string functions *@param : String *@return :String *@throws Exception *@author : Yilong Xiao *@Contributing Author: N/A *@version 1.0 */
public static String desEdeDecrypt(String dncrypt)throws Exception{ return getDesEdeInstance().decrypt(dncrypt); }
static{ DEFAULT_ENCRYPTION_KEY = Message.getMsg("LUTHERSLIST_ENCRYPT_KEY", false); } public DesEncrypt()throws EncryptionException{ if ( DEFAULT_ENCRYPTION_KEY == null ) throw new IllegalArgumentException( "encryption key was null" ); if ( DEFAULT_ENCRYPTION_KEY.trim().length() < 24 ) throw new IllegalArgumentException( "encryption key was less than 24 characters" );
public class DesEncrypt {
public static final String DESEDE_ENCRYPTION_SCHEME = "DESede";
public static final String DES_ENCRYPTION_SCHEME = "DES";
private static String ENCRYPTION_SCHEME = "DESede";
private static String DEFAULT_ENCRYPTION_KEY = "This is a fairly long phrase used to encrypt";
private KeySpec keySpec;
private SecretKeyFactory keyFactory;
private Cipher cipher;
private static DesEncrypt desede;
private static DesEncrypt des;
private static final String UNICODE_FORMAT = "UTF8";
private static final Log log = LogFactory.getLog(DesEncrypt.class);
private static DesEncrypt getDesEdeInstance()throws Exception{
if(desede == null){
ENCRYPTION_SCHEME = DESEDE_ENCRYPTION_SCHEME;
desede = new DesEncrypt();
}
return desede;
}
private static DesEncrypt getDesInstance()throws Exception{
if(des == null){
ENCRYPTION_SCHEME = DES_ENCRYPTION_SCHEME;
des = new DesEncrypt();
}
return des;
}
/**
*@Purpose : this function provide get encrypt string functions
*@param : String
*@return :String
*@throws Exception
*@author : Yilong Xiao
*@Contributing Author: N/A
*@version 1.0
*/
public static String desEdeEncrypt(String encrypt)throws Exception{
return getDesEdeInstance().encrypt(encrypt);
}
/**
*@Purpose : this function provide get decrypt string functions
*@param : String
*@return :String
*@throws Exception
*@author : Yilong Xiao
*@Contributing Author: N/A
*@version 1.0
*/
public static String desEdeDecrypt(String dncrypt)throws Exception{
return getDesEdeInstance().decrypt(dncrypt);
}
static{
DEFAULT_ENCRYPTION_KEY = Message.getMsg("LUTHERSLIST_ENCRYPT_KEY", false);
}
public DesEncrypt()throws EncryptionException{
if ( DEFAULT_ENCRYPTION_KEY == null )
throw new IllegalArgumentException( "encryption key was null" );
if ( DEFAULT_ENCRYPTION_KEY.trim().length() < 24 )
throw new IllegalArgumentException(
"encryption key was less than 24 characters" );
try
{
byte[] keyAsBytes = DEFAULT_ENCRYPTION_KEY.getBytes( UNICODE_FORMAT );
if ( ENCRYPTION_SCHEME.equals( DESEDE_ENCRYPTION_SCHEME) )
{
keySpec = new DESedeKeySpec( keyAsBytes );
}
else if ( ENCRYPTION_SCHEME.equals( DES_ENCRYPTION_SCHEME ) )
{
keySpec = new DESKeySpec( keyAsBytes );
}
else
{
throw new IllegalArgumentException( "Encryption scheme not supported: "
+ ENCRYPTION_SCHEME );
}
keyFactory = SecretKeyFactory.getInstance( ENCRYPTION_SCHEME );
cipher = Cipher.getInstance( ENCRYPTION_SCHEME );
}
catch (InvalidKeyException e)
{
throw new EncryptionException( e );
}
catch (UnsupportedEncodingException e)
{
throw new EncryptionException( e );
}
catch (NoSuchAlgorithmException e)
{
throw new EncryptionException( e );
}
catch (NoSuchPaddingException e)
{
throw new EncryptionException( e );
}
}
/**
*@Purpose : this function provide Encryption functions
*@param : String
*@return :String
*@throws Exception
*@author : Yilong Xiao
*@Contributing Author: N/A
*@version 1.0
*/
public String encrypt( String unencryptedString ) throws EncryptionException
{
if ( unencryptedString == null || unencryptedString.trim().length() == 0 )
throw new IllegalArgumentException(
"unencrypted string was null or empty" ); try
{
SecretKey key = keyFactory.generateSecret( keySpec );
cipher.init( Cipher.ENCRYPT_MODE, key );
byte[] cleartext = unencryptedString.getBytes( UNICODE_FORMAT );
byte[] ciphertext = cipher.doFinal( cleartext ); BASE64Encoder base64encoder = new BASE64Encoder();
return base64encoder.encode( ciphertext );
}
catch (Exception e)
{
throw new EncryptionException( e );
}
}
/**
*@Purpose : this function provide Decryption functions
*@param : String
*@return :String
*@throws Exception
*@author : Yilong Xiao
*@Contributing Author: N/A
*@version 1.0
*/ public String decrypt( String encryptedString ) throws EncryptionException
{
if ( encryptedString == null || encryptedString.trim().length() <= 0 )
throw new IllegalArgumentException( "encrypted string was null or empty" ); try
{
SecretKey key = keyFactory.generateSecret( keySpec );
cipher.init( Cipher.DECRYPT_MODE, key );
BASE64Decoder base64decoder = new BASE64Decoder();
byte[] cleartext = base64decoder.decodeBuffer( encryptedString );
byte[] ciphertext = cipher.doFinal( cleartext ); return bytes2String( ciphertext );
}
catch (Exception e)
{
throw new EncryptionException( e );
}
} private static String bytes2String( byte[] bytes )
{
StringBuffer stringBuffer = new StringBuffer();
for (int i = 0; i < bytes.length; i++)
{
stringBuffer.append( (char) bytes[i] );
}
return stringBuffer.toString();
} public static class EncryptionException extends Exception
{
public EncryptionException( Throwable t )
{
super(t);
}
}}