View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.security;
8   
9   import org.mule.api.lifecycle.InitialisationException;
10  import org.mule.config.i18n.CoreMessages;
11  import org.mule.util.StringMessageUtils;
12  
13  import java.security.GeneralSecurityException;
14  import java.security.spec.AlgorithmParameterSpec;
15  import java.security.spec.KeySpec;
16  
17  import javax.crypto.KeyGenerator;
18  import javax.crypto.SecretKey;
19  import javax.crypto.spec.SecretKeySpec;
20  
21  /**
22   * SecretKey based encryption using JCE. Users must specify a key as an array of
23   * bytes. This can be set directly on the strategy or a keyFactory can be specified.
24   * A keyFactory is an implementation of {@link SecretKeyFactory} and must return a
25   * byte array. The default algorthm used by this strategy is Blowfish, but users can
26   * specify any valid algorithm supported by JCE.
27   * 
28   * @see SecretKeyFactory
29   */
30  public class SecretKeyEncryptionStrategy extends AbstractJCEEncryptionStrategy
31  {
32  
33      public static final String DEFAULT_ALGORITHM = "Blowfish";
34  
35      private byte[] key;
36      private SecretKeyFactory keyFactory;
37  
38      public SecretKeyEncryptionStrategy()
39      {
40          algorithm = DEFAULT_ALGORITHM;
41      }
42  
43      public void initialise() throws InitialisationException
44      {
45          if (key == null)
46          {
47              if (keyFactory == null)
48              {
49                  throw new InitialisationException(CoreMessages.objectIsNull("Key / KeyFactory"), this);
50              }
51              else
52              {
53                  try
54                  {
55                      key = keyFactory.getKey();
56                  }
57                  catch (Exception e)
58                  {
59                      throw new InitialisationException(e, this);
60                  }
61              }
62          }
63          super.initialise();
64      }
65  
66      protected KeySpec createKeySpec()
67      {
68          return new SecretKeySpec(key, algorithm);
69      }
70  
71      protected AlgorithmParameterSpec createAlgorithmParameterSpec()
72      {
73          return null;
74      }
75  
76      public void setKey(byte[] rawKey)
77      {
78          this.key = rawKey;
79      }
80  
81      public void setKey(String rawKey)
82      {
83          this.key = StringMessageUtils.getBytes(rawKey);
84      }
85  
86      public SecretKeyFactory getKeyFactory()
87      {
88          return keyFactory;
89      }
90  
91      public void setKeyFactory(SecretKeyFactory keyFactory)
92      {
93          this.keyFactory = keyFactory;
94      }
95  
96      protected SecretKey getSecretKey() throws GeneralSecurityException
97      {
98          return KeyGenerator.getInstance(algorithm).generateKey();
99      }
100 
101 }