KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > users > StringEncrypter


1 package com.openedit.users;
2
3 import java.io.UnsupportedEncodingException JavaDoc;
4 import java.security.InvalidKeyException JavaDoc;
5 import java.security.NoSuchAlgorithmException JavaDoc;
6 import java.security.spec.KeySpec JavaDoc;
7
8 import javax.crypto.Cipher;
9 import javax.crypto.NoSuchPaddingException;
10 import javax.crypto.SecretKey;
11 import javax.crypto.SecretKeyFactory;
12 import javax.crypto.spec.DESKeySpec;
13 import javax.crypto.spec.DESedeKeySpec;
14
15 import org.apache.commons.codec.binary.Base64;
16
17 import com.openedit.OpenEditException;
18
19 public class StringEncrypter
20 {
21     public static final String JavaDoc DESEDE_ENCRYPTION_SCHEME = "DESede";
22     public static final String JavaDoc DES_ENCRYPTION_SCHEME = "DES";
23     public static final String JavaDoc DEFAULT_ENCRYPTION_KEY = "This is a fairly long phrase used to encrypt";
24     
25     private KeySpec JavaDoc keySpec;
26     private SecretKeyFactory keyFactory;
27     private Cipher cipher;
28     
29     private static final String JavaDoc UNICODE_FORMAT = "UTF8";
30
31     public StringEncrypter( String JavaDoc encryptionScheme ) throws OpenEditException
32     {
33         this( encryptionScheme, DEFAULT_ENCRYPTION_KEY );
34     }
35
36     public StringEncrypter( String JavaDoc encryptionScheme, String JavaDoc encryptionKey )
37             throws OpenEditException
38     {
39
40         if ( encryptionKey == null )
41                 throw new IllegalArgumentException JavaDoc( "encryption key was null" );
42         if ( encryptionKey.trim().length() < 24 )
43                 throw new IllegalArgumentException JavaDoc(
44                         "encryption key was less than 24 characters" );
45
46         try
47         {
48             byte[] keyAsBytes = encryptionKey.getBytes( UNICODE_FORMAT );
49
50             if ( encryptionScheme.equals( DESEDE_ENCRYPTION_SCHEME) )
51             {
52                 keySpec = new DESedeKeySpec( keyAsBytes );
53             }
54             else if ( encryptionScheme.equals( DES_ENCRYPTION_SCHEME ) )
55             {
56                 keySpec = new DESKeySpec( keyAsBytes );
57             }
58             else
59             {
60                 throw new IllegalArgumentException JavaDoc( "Encryption scheme not supported: "
61                                                     + encryptionScheme );
62             }
63
64             keyFactory = SecretKeyFactory.getInstance( encryptionScheme );
65             cipher = Cipher.getInstance( encryptionScheme );
66
67         }
68         catch (InvalidKeyException JavaDoc e)
69         {
70             throw new OpenEditException( e );
71         }
72         catch (UnsupportedEncodingException JavaDoc e)
73         {
74             throw new OpenEditException( e );
75         }
76         catch (NoSuchAlgorithmException JavaDoc e)
77         {
78             throw new OpenEditException( e );
79         }
80         catch (NoSuchPaddingException e)
81         {
82             throw new OpenEditException( e );
83         }
84
85     }
86
87     public String JavaDoc encrypt( String JavaDoc unencryptedString ) throws OpenEditException
88     {
89         if ( unencryptedString == null || unencryptedString.trim().length() == 0 )
90                 throw new IllegalArgumentException JavaDoc(
91                         "unencrypted string was null or empty" );
92
93         try
94         {
95             SecretKey key = keyFactory.generateSecret( keySpec );
96             cipher.init( Cipher.ENCRYPT_MODE, key );
97             byte[] cleartext = unencryptedString.getBytes( UNICODE_FORMAT );
98             byte[] ciphertext = cipher.doFinal( cleartext );
99
100             Base64 base64encoder = new Base64();
101             return new String JavaDoc( base64encoder.encode( ciphertext ), UNICODE_FORMAT );
102         }
103         catch (Exception JavaDoc e)
104         {
105             throw new OpenEditException( e );
106         }
107     }
108
109     public String JavaDoc decrypt( String JavaDoc encryptedString ) throws OpenEditException
110     {
111         if ( encryptedString == null || encryptedString.trim().length() <= 0 )
112                 throw new IllegalArgumentException JavaDoc( "encrypted string was null or empty" );
113
114         try
115         {
116             SecretKey key = keyFactory.generateSecret( keySpec );
117             cipher.init( Cipher.DECRYPT_MODE, key );
118             Base64 base64decoder = new Base64();
119             byte[] cleartext = base64decoder.decode( encryptedString.getBytes( UNICODE_FORMAT ) );
120             byte[] ciphertext = cipher.doFinal( cleartext );
121
122             return bytes2String( ciphertext );
123         }
124         catch (Exception JavaDoc e)
125         {
126             throw new OpenEditException( e );
127         }
128     }
129
130     private static String JavaDoc bytes2String( byte[] bytes )
131     {
132         StringBuffer JavaDoc stringBuffer = new StringBuffer JavaDoc();
133         for (int i = 0; i < bytes.length; i++)
134         {
135             stringBuffer.append( (char) bytes[i] );
136         }
137         return stringBuffer.toString();
138     }
139
140 }
Popular Tags