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