FileDocCategorySizeDatePackage
StringEncrypter.javaAPI DocAzureus 3.0.3.44127Thu Oct 13 06:26:46 BST 2005org.gudy.azureus2.ui.console.util

StringEncrypter

public class StringEncrypter extends Object
utility class to encrypt strings. this class was taken from the examples at: http://www.devx.com/Java/10MinuteSolution/21385/0/page/2

Fields Summary
public static final String
DESEDE_ENCRYPTION_SCHEME
public static final String
DES_ENCRYPTION_SCHEME
public static final String
DEFAULT_ENCRYPTION_KEY
private KeySpec
keySpec
private SecretKeyFactory
keyFactory
private Cipher
cipher
private static final String
UNICODE_FORMAT
Constructors Summary
public StringEncrypter(String encryptionScheme)

	
	      
	
		this( encryptionScheme, DEFAULT_ENCRYPTION_KEY );
	
public StringEncrypter(String encryptionScheme, String encryptionKey)

		if ( encryptionKey == null )
			throw new IllegalArgumentException( "encryption key was null" );
		if ( encryptionKey.trim().length() < 24 )
			throw new IllegalArgumentException(
			"encryption key was less than 24 characters" );
		
		try
		{
			byte[] keyAsBytes = encryptionKey.getBytes( UNICODE_FORMAT );
			
			if ( encryptionScheme.equals( DESEDE_ENCRYPTION_SCHEME) )
			{
				keySpec = new DESedeKeySpec( keyAsBytes );
			}
			else if ( encryptionScheme.equals( DES_ENCRYPTION_SCHEME ) )
			{
				keySpec = new DESKeySpec( keyAsBytes );
			}
			else
			{
				throw new IllegalArgumentException( "Encryption scheme not supported: "
						+ encryptionScheme );
			}
			
			keyFactory = SecretKeyFactory.getInstance( encryptionScheme );
			cipher = Cipher.getInstance( encryptionScheme );
			
		}
		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 );
		}
		
	
Methods Summary
private static java.lang.Stringbytes2String(byte[] bytes)

		StringBuffer stringBuffer = new StringBuffer();
		for (int i = 0; i < bytes.length; i++)
		{
			stringBuffer.append( (char) bytes[i] );
		}
		return stringBuffer.toString();
	
public java.lang.Stringdecrypt(java.lang.String encryptedString)

		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 );
			byte[] cleartext = Base64.decode( encryptedString );
			byte[] ciphertext = cipher.doFinal( cleartext );
			
			return bytes2String( ciphertext );
		}
		catch (Exception e)
		{
			throw new EncryptionException( e );
		}
	
public java.lang.Stringencrypt(java.lang.String unencryptedString)

		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 );
			
			return new String( Base64.encode( ciphertext ));
		}
		catch (Exception e)
		{
			throw new EncryptionException( e );
		}