1
2
3
4
5
6
7
8
9
10
11 package org.mule.impl.security;
12
13 import org.mule.config.i18n.CoreMessages;
14 import org.mule.umo.lifecycle.InitialisationException;
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
27
28
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 }