View Javadoc

1   /*
2    * $Id: AbstractJCEEncryptionStrategy.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.security;
12  
13  import org.mule.api.lifecycle.InitialisationException;
14  import org.mule.api.security.CryptoFailureException;
15  import org.mule.config.i18n.CoreMessages;
16  import org.mule.util.Base64;
17  
18  import java.security.GeneralSecurityException;
19  import java.security.spec.AlgorithmParameterSpec;
20  import java.security.spec.KeySpec;
21  
22  import javax.crypto.Cipher;
23  import javax.crypto.SecretKey;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  
28  /**
29   * A JCE based encryption strategy. It also provides base64 encoding of
30   * encrypted/decrypted data by setting the base64encoding attribute.
31   */
32  public abstract class AbstractJCEEncryptionStrategy extends AbstractNamedEncryptionStrategy
33  {
34      /**
35       * logger used by this class
36       */
37      protected transient Log logger = LogFactory.getLog(getClass());
38  
39      protected KeySpec keySpec;
40      protected SecretKey secretKey;
41      protected Cipher encryptCipher;
42      protected Cipher decryptCipher;
43  
44      protected String algorithm = null;
45  
46      protected boolean base64Encoding = true;
47  
48      public void initialise() throws InitialisationException
49      {
50          if (algorithm == null)
51          {
52              throw new InitialisationException(CoreMessages.objectIsNull("Algorithm"), this);
53          }
54          else
55          {
56              logger.debug("Using encryption algorithm: " + algorithm);
57          }
58  
59          keySpec = createKeySpec();
60  
61          try
62          {
63              secretKey = getSecretKey();
64              // Create Ciphers
65              encryptCipher = Cipher.getInstance(getAlgorithm());
66              decryptCipher = Cipher.getInstance(getAlgorithm());
67  
68              AlgorithmParameterSpec paramSpec = createAlgorithmParameterSpec();
69              if (paramSpec != null)
70              {
71                  encryptCipher.init(Cipher.ENCRYPT_MODE, secretKey, paramSpec);
72                  decryptCipher.init(Cipher.DECRYPT_MODE, secretKey, paramSpec);
73              }
74              else
75              {
76                  encryptCipher.init(Cipher.ENCRYPT_MODE, secretKey);
77                  decryptCipher.init(Cipher.DECRYPT_MODE, secretKey);
78              }
79  
80          }
81          catch (Exception e)
82          {
83              throw new InitialisationException(CoreMessages.failedToCreate("encryption ciphers"),
84                  e, this);
85          }
86      }
87  
88      protected abstract SecretKey getSecretKey() throws GeneralSecurityException;
89  
90      public byte[] encrypt(byte[] data, Object info) throws CryptoFailureException
91      {
92          try
93          {
94              byte[] buf = encryptCipher.doFinal(data);
95              if (base64Encoding)
96              {
97                  return Base64.encodeBytes(buf).getBytes();
98              }
99              else
100             {
101                 return buf;
102             }
103         }
104         catch (Exception e)
105         {
106             throw new CryptoFailureException(this, e);
107         }
108     }
109 
110     public byte[] decrypt(byte[] data, Object info) throws CryptoFailureException
111     {
112         try
113         {
114             byte[] dec = data;
115             if (base64Encoding)
116             {
117                 dec = Base64.decode(new String(data));
118             }
119             return decryptCipher.doFinal(dec);
120         }
121         catch (Exception e)
122         {
123             throw new CryptoFailureException(this, e);
124         }
125     }
126 
127     public String getAlgorithm()
128     {
129         return algorithm;
130     }
131 
132     public void setAlgorithm(String algorithm)
133     {
134         this.algorithm = algorithm;
135     }
136 
137     public String toString()
138     {
139         StringBuffer buf = new StringBuffer();
140         buf.append("Algorithm=").append(algorithm);
141         return buf.toString();
142     }
143 
144     public boolean isBase64Encoding()
145     {
146         return base64Encoding;
147     }
148 
149     public void setBase64Encoding(boolean base64Encoding)
150     {
151         this.base64Encoding = base64Encoding;
152     }
153 
154     protected abstract KeySpec createKeySpec();
155 
156     protected abstract AlgorithmParameterSpec createAlgorithmParameterSpec();
157 
158 }