KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > impl > security > PasswordBasedEncryptionStrategy


1 /*
2  * $Id: PasswordBasedEncryptionStrategy.java 3865 2006-11-09 17:11:08Z Lajos $
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 MuleSource MPL
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.Message;
14 import org.mule.config.i18n.Messages;
15 import org.mule.umo.lifecycle.InitialisationException;
16
17 import javax.crypto.SecretKey;
18 import javax.crypto.SecretKeyFactory;
19 import javax.crypto.spec.PBEKeySpec;
20 import javax.crypto.spec.PBEParameterSpec;
21
22 import java.security.GeneralSecurityException JavaDoc;
23 import java.security.spec.AlgorithmParameterSpec JavaDoc;
24 import java.security.spec.KeySpec JavaDoc;
25
26 /**
27  * PRovides password-based encryption using JCE. Users must specify a password and
28  * optionally a salt and iteration count as well. The default algorithm is
29  * PBEWithMD5AndDES, but users can specify any valid algorithm supported by JCE.
30  *
31  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
32  * @version $Revision: 3865 $
33  */

34 public class PasswordBasedEncryptionStrategy extends AbstractJCEEncryptionStrategy
35 {
36
37     public static final String JavaDoc DEFAULT_ALGORITHM = "PBEWithMD5AndDES";
38     private byte[] salt = null;
39
40     private int iterationCount = 20;
41     private char[] password;
42
43     public PasswordBasedEncryptionStrategy()
44     {
45         algorithm = DEFAULT_ALGORITHM;
46     }
47
48     public void initialise() throws InitialisationException
49     {
50         if (salt == null)
51         {
52             salt = new byte[]{(byte)0xc7, (byte)0x73, (byte)0x21, (byte)0x8c, (byte)0x7e, (byte)0xc8,
53                 (byte)0xee, (byte)0x99};
54             logger.debug("Salt is not set. Using default salt");
55         }
56
57         if (password == null)
58         {
59             throw new InitialisationException(new Message(Messages.X_IS_NULL, "Password"), this);
60         }
61         super.initialise();
62     }
63
64     protected KeySpec createKeySpec()
65     {
66         return new PBEKeySpec(password);
67     }
68
69     protected AlgorithmParameterSpec JavaDoc createAlgorithmParameterSpec()
70     {
71         return new PBEParameterSpec(salt, iterationCount);
72     }
73
74     public byte[] getSalt()
75     {
76         return salt;
77     }
78
79     public void setSalt(byte[] salt)
80     {
81         this.salt = salt;
82     }
83
84     public int getIterationCount()
85     {
86         return iterationCount;
87     }
88
89     public void setIterationCount(int iterationCount)
90     {
91         this.iterationCount = iterationCount;
92     }
93
94     public void setPassword(String JavaDoc password)
95     {
96         this.password = password.toCharArray();
97     }
98
99     protected SecretKey getSecretKey() throws GeneralSecurityException JavaDoc
100     {
101         SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(getAlgorithm());
102         return keyFactory.generateSecret(keySpec);
103     }
104 }
105
Popular Tags