KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > impl > security > AbstractJCEEncryptionStrategy


1 /*
2  * $Id: AbstractJCEEncryptionStrategy.java 4259 2006-12-14 03:12:07Z aperepel $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.impl.security;
12
13 import java.security.GeneralSecurityException JavaDoc;
14 import java.security.spec.AlgorithmParameterSpec JavaDoc;
15 import java.security.spec.KeySpec JavaDoc;
16
17 import javax.crypto.Cipher;
18 import javax.crypto.SecretKey;
19
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22 import org.mule.config.i18n.Message;
23 import org.mule.config.i18n.Messages;
24 import org.mule.umo.UMOEncryptionStrategy;
25 import org.mule.umo.lifecycle.InitialisationException;
26 import org.mule.umo.security.CryptoFailureException;
27 import org.mule.util.Base64;
28
29 /**
30  * A JCE based encryption strategy. It also provides base64 encoding of
31  * encrypted/decrypted data by setting the base64encoding attribute.
32  */

33 public abstract class AbstractJCEEncryptionStrategy implements UMOEncryptionStrategy
34 {
35     /**
36      * logger used by this class
37      */

38     protected transient Log logger = LogFactory.getLog(getClass());
39
40     protected KeySpec JavaDoc keySpec;
41     protected SecretKey secretKey;
42     protected Cipher encryptCipher;
43     protected Cipher decryptCipher;
44
45     protected String JavaDoc algorithm = null;
46
47     protected boolean base64Encoding = true;
48
49     public void initialise() throws InitialisationException
50     {
51         if (algorithm == null)
52         {
53             throw new InitialisationException(new Message(Messages.X_IS_NULL, "Algorithm"), this);
54         }
55         else
56         {
57             logger.debug("Using encryption algorithm: " + algorithm);
58         }
59
60         keySpec = createKeySpec();
61
62         try
63         {
64             secretKey = getSecretKey();
65             // Create Ciphers
66
encryptCipher = Cipher.getInstance(getAlgorithm());
67             decryptCipher = Cipher.getInstance(getAlgorithm());
68
69             AlgorithmParameterSpec JavaDoc paramSpec = createAlgorithmParameterSpec();
70             if (paramSpec != null)
71             {
72                 encryptCipher.init(Cipher.ENCRYPT_MODE, secretKey, paramSpec);
73                 decryptCipher.init(Cipher.DECRYPT_MODE, secretKey, paramSpec);
74             }
75             else
76             {
77                 encryptCipher.init(Cipher.ENCRYPT_MODE, secretKey);
78                 decryptCipher.init(Cipher.DECRYPT_MODE, secretKey);
79             }
80
81         }
82         catch (Exception JavaDoc e)
83         {
84             throw new InitialisationException(new Message(Messages.FAILED_TO_CREATE_X, "encryption ciphers"),
85                 e, this);
86         }
87     }
88
89     protected abstract SecretKey getSecretKey() throws GeneralSecurityException JavaDoc;
90
91     public byte[] encrypt(byte[] data, Object JavaDoc info) throws CryptoFailureException
92     {
93         try
94         {
95             byte[] buf = encryptCipher.doFinal(data);
96             if (base64Encoding)
97             {
98                 return Base64.encodeBytes(buf).getBytes();
99             }
100             else
101             {
102                 return buf;
103             }
104         }
105         catch (Exception JavaDoc e)
106         {
107             throw new CryptoFailureException(this, e);
108         }
109     }
110
111     public byte[] decrypt(byte[] data, Object JavaDoc info) throws CryptoFailureException
112     {
113         try
114         {
115             byte[] dec = data;
116             if (base64Encoding)
117             {
118                 dec = Base64.decode(new String JavaDoc(data));
119             }
120             return decryptCipher.doFinal(dec);
121         }
122         catch (Exception JavaDoc e)
123         {
124             throw new CryptoFailureException(this, e);
125         }
126     }
127
128     public String JavaDoc getAlgorithm()
129     {
130         return algorithm;
131     }
132
133     public void setAlgorithm(String JavaDoc algorithm)
134     {
135         this.algorithm = algorithm;
136     }
137
138     public String JavaDoc toString()
139     {
140         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
141         buf.append("Algorithm=").append(algorithm);
142         return buf.toString();
143     }
144
145     public boolean isBase64Encoding()
146     {
147         return base64Encoding;
148     }
149
150     public void setBase64Encoding(boolean base64Encoding)
151     {
152         this.base64Encoding = base64Encoding;
153     }
154
155     protected abstract KeySpec JavaDoc createKeySpec();
156
157     protected abstract AlgorithmParameterSpec JavaDoc createAlgorithmParameterSpec();
158
159 }
160
Popular Tags