1
2
3
4
5
6
7
8
9
10
11 package org.mule.security;
12
13 import org.mule.api.lifecycle.InitialisationException;
14 import org.mule.config.i18n.CoreMessages;
15 import org.mule.util.StringMessageUtils;
16
17 import java.security.GeneralSecurityException;
18 import java.security.spec.AlgorithmParameterSpec;
19 import java.security.spec.KeySpec;
20
21 import javax.crypto.KeyGenerator;
22 import javax.crypto.SecretKey;
23 import javax.crypto.spec.SecretKeySpec;
24
25
26
27
28
29
30
31
32
33
34 public class SecretKeyEncryptionStrategy extends AbstractJCEEncryptionStrategy
35 {
36
37 public static final String DEFAULT_ALGORITHM = "Blowfish";
38
39 private byte[] key;
40 private SecretKeyFactory keyFactory;
41
42 public SecretKeyEncryptionStrategy()
43 {
44 algorithm = DEFAULT_ALGORITHM;
45 }
46
47 public void initialise() throws InitialisationException
48 {
49 if (key == null)
50 {
51 if (keyFactory == null)
52 {
53 throw new InitialisationException(CoreMessages.objectIsNull("Key / KeyFactory"), this);
54 }
55 else
56 {
57 try
58 {
59 key = keyFactory.getKey();
60 }
61 catch (Exception e)
62 {
63 throw new InitialisationException(e, this);
64 }
65 }
66 }
67 super.initialise();
68 }
69
70 protected KeySpec createKeySpec()
71 {
72 return new SecretKeySpec(key, algorithm);
73 }
74
75 protected AlgorithmParameterSpec createAlgorithmParameterSpec()
76 {
77 return null;
78 }
79
80 public void setKey(byte[] rawKey)
81 {
82 this.key = rawKey;
83 }
84
85 public void setKey(String rawKey)
86 {
87 this.key = StringMessageUtils.getBytes(rawKey);
88 }
89
90 public SecretKeyFactory getKeyFactory()
91 {
92 return keyFactory;
93 }
94
95 public void setKeyFactory(SecretKeyFactory keyFactory)
96 {
97 this.keyFactory = keyFactory;
98 }
99
100 protected SecretKey getSecretKey() throws GeneralSecurityException
101 {
102 return KeyGenerator.getInstance(algorithm).generateKey();
103 }
104
105 }