01: /*
02: * $Id: PasswordBasedEncryptionStrategy.java 10808 2008-02-14 20:36:57Z acooke $
03: * --------------------------------------------------------------------------------------
04: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
05: *
06: * The software in this package is published under the terms of the CPAL v1.0
07: * license, a copy of which has been included with this distribution in the
08: * LICENSE.txt file.
09: */
10:
11: package org.mule.security;
12:
13: import org.mule.api.lifecycle.InitialisationException;
14: import org.mule.api.lifecycle.LifecycleTransitionResult;
15: import org.mule.config.i18n.CoreMessages;
16:
17: import java.security.GeneralSecurityException;
18: import java.security.spec.AlgorithmParameterSpec;
19: import java.security.spec.KeySpec;
20:
21: import javax.crypto.SecretKey;
22: import javax.crypto.SecretKeyFactory;
23: import javax.crypto.spec.PBEKeySpec;
24: import javax.crypto.spec.PBEParameterSpec;
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: public class PasswordBasedEncryptionStrategy extends
32: AbstractJCEEncryptionStrategy {
33:
34: public static final String DEFAULT_ALGORITHM = "PBEWithMD5AndDES";
35: public static final int DEFAULT_ITERATION_COUNT = 20;
36:
37: private byte[] salt = null;
38:
39: private int iterationCount = DEFAULT_ITERATION_COUNT;
40:
41: private char[] password;
42:
43: public PasswordBasedEncryptionStrategy() {
44: algorithm = DEFAULT_ALGORITHM;
45: }
46:
47: public LifecycleTransitionResult initialise()
48: throws InitialisationException {
49: if (salt == null) {
50: salt = new byte[] { (byte) 0xc7, (byte) 0x73, (byte) 0x21,
51: (byte) 0x8c, (byte) 0x7e, (byte) 0xc8, (byte) 0xee,
52: (byte) 0x99 };
53: logger.debug("Salt is not set. Using default salt");
54: }
55:
56: if (password == null) {
57: throw new InitialisationException(CoreMessages
58: .objectIsNull("Password"), this );
59: }
60: return super .initialise();
61: }
62:
63: protected KeySpec createKeySpec() {
64: return new PBEKeySpec(password);
65: }
66:
67: protected AlgorithmParameterSpec createAlgorithmParameterSpec() {
68: return new PBEParameterSpec(salt, iterationCount);
69: }
70:
71: public byte[] getSalt() {
72: return salt;
73: }
74:
75: public void setSalt(byte[] salt) {
76: this .salt = salt;
77: }
78:
79: public int getIterationCount() {
80: return iterationCount;
81: }
82:
83: public void setIterationCount(int iterationCount) {
84: this .iterationCount = iterationCount;
85: }
86:
87: public void setPassword(String password) {
88: this .password = password.toCharArray();
89: }
90:
91: protected SecretKey getSecretKey() throws GeneralSecurityException {
92: SecretKeyFactory keyFactory = SecretKeyFactory
93: .getInstance(getAlgorithm());
94: return keyFactory.generateSecret(keySpec);
95: }
96: }
|