View Javadoc

1   /*
2    * $Id: SecretKeyEncryptionStrategy.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.config.i18n.CoreMessages;
15  import org.mule.util.StringMessageUtils;
16  
17  import java.security.GeneralSecurityException;
18  import java.security.spec.AlgorithmParameterSpec;
19  import java.security.spec.KeySpec;
20  
21  import javax.crypto.KeyGenerator;
22  import javax.crypto.SecretKey;
23  import javax.crypto.spec.SecretKeySpec;
24  
25  /**
26   * SecretKey based encryption using JCE. Users must specify a key as an array of
27   * bytes. This can be set directly on the strategy or a keyFactory can be specified.
28   * A keyFactory is an implementation of {@link SecretKeyFactory} and must return a
29   * byte array. The default algorthm used by this strategy is Blowfish, but users can
30   * specify any valid algorithm supported by JCE.
31   * 
32   * @see SecretKeyFactory
33   */
34  public class SecretKeyEncryptionStrategy extends AbstractJCEEncryptionStrategy
35  {
36  
37      public static final String DEFAULT_ALGORITHM = "Blowfish";
38  
39      private byte[] key;
40      private SecretKeyFactory keyFactory;
41  
42      public SecretKeyEncryptionStrategy()
43      {
44          algorithm = DEFAULT_ALGORITHM;
45      }
46  
47      public void initialise() throws InitialisationException
48      {
49          if (key == null)
50          {
51              if (keyFactory == null)
52              {
53                  throw new InitialisationException(CoreMessages.objectIsNull("Key / KeyFactory"), this);
54              }
55              else
56              {
57                  try
58                  {
59                      key = keyFactory.getKey();
60                  }
61                  catch (Exception e)
62                  {
63                      throw new InitialisationException(e, this);
64                  }
65              }
66          }
67          super.initialise();
68      }
69  
70      protected KeySpec createKeySpec()
71      {
72          return new SecretKeySpec(key, algorithm);
73      }
74  
75      protected AlgorithmParameterSpec createAlgorithmParameterSpec()
76      {
77          return null;
78      }
79  
80      public void setKey(byte[] rawKey)
81      {
82          this.key = rawKey;
83      }
84  
85      public void setKey(String rawKey)
86      {
87          this.key = StringMessageUtils.getBytes(rawKey);
88      }
89  
90      public SecretKeyFactory getKeyFactory()
91      {
92          return keyFactory;
93      }
94  
95      public void setKeyFactory(SecretKeyFactory keyFactory)
96      {
97          this.keyFactory = keyFactory;
98      }
99  
100     protected SecretKey getSecretKey() throws GeneralSecurityException
101     {
102         return KeyGenerator.getInstance(algorithm).generateKey();
103     }
104 
105 }