View Javadoc

1   /*
2    * $Id: PasswordBasedEncryptionStrategy.java 11517 2008-03-31 21:34:19Z 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.security;
12  
13  import org.mule.api.lifecycle.InitialisationException;
14  import org.mule.config.i18n.CoreMessages;
15  
16  import java.security.GeneralSecurityException;
17  import java.security.spec.AlgorithmParameterSpec;
18  import java.security.spec.KeySpec;
19  
20  import javax.crypto.SecretKey;
21  import javax.crypto.SecretKeyFactory;
22  import javax.crypto.spec.PBEKeySpec;
23  import javax.crypto.spec.PBEParameterSpec;
24  
25  /**
26   * Provides password-based encryption using JCE. Users must specify a password and
27   * optionally a salt and iteration count as well. The default algorithm is
28   * PBEWithMD5AndDES, but users can specify any valid algorithm supported by JCE.
29   */
30  public class PasswordBasedEncryptionStrategy extends AbstractJCEEncryptionStrategy
31  {
32  
33      public static final String DEFAULT_ALGORITHM = "PBEWithMD5AndDES";
34      public static final int DEFAULT_ITERATION_COUNT = 20;
35  
36      private byte[] salt = null;
37  
38      private int iterationCount = DEFAULT_ITERATION_COUNT;
39  
40      private char[] password;
41  
42      public PasswordBasedEncryptionStrategy()
43      {
44          algorithm = DEFAULT_ALGORITHM;
45      }
46  
47      public void initialise() throws InitialisationException
48      {
49          if (salt == null)
50          {
51              salt = new byte[]{(byte) 0xc7, (byte) 0x73, (byte) 0x21, (byte) 0x8c, (byte) 0x7e, (byte) 0xc8,
52                  (byte) 0xee, (byte) 0x99};
53              logger.debug("Salt is not set. Using default salt");
54          }
55  
56          if (password == null)
57          {
58              throw new InitialisationException(CoreMessages.objectIsNull("Password"), this);
59          }
60          super.initialise();
61      }
62  
63      protected KeySpec createKeySpec()
64      {
65          return new PBEKeySpec(password);
66      }
67  
68      protected AlgorithmParameterSpec createAlgorithmParameterSpec()
69      {
70          return new PBEParameterSpec(salt, iterationCount);
71      }
72  
73      public byte[] getSalt()
74      {
75          return salt;
76      }
77  
78      public void setSalt(byte[] salt)
79      {
80          this.salt = salt;
81      }
82  
83      public int getIterationCount()
84      {
85          return iterationCount;
86      }
87  
88      public void setIterationCount(int iterationCount)
89      {
90          this.iterationCount = iterationCount;
91      }
92  
93      public void setPassword(String password)
94      {
95          this.password = password.toCharArray();
96      }
97  
98      protected SecretKey getSecretKey() throws GeneralSecurityException
99      {
100         SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(getAlgorithm());
101         return keyFactory.generateSecret(keySpec);
102     }
103 }