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