View Javadoc

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