KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > impl > security > SecretKeyEncryptionStrategy


1 /*
2  * $Id: SecretKeyEncryptionStrategy.java 4259 2006-12-14 03:12:07Z aperepel $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.impl.security;
12
13 import java.security.GeneralSecurityException JavaDoc;
14 import java.security.spec.AlgorithmParameterSpec JavaDoc;
15 import java.security.spec.KeySpec JavaDoc;
16
17 import javax.crypto.KeyGenerator;
18 import javax.crypto.SecretKey;
19 import javax.crypto.spec.SecretKeySpec;
20
21 import org.mule.config.i18n.Message;
22 import org.mule.config.i18n.Messages;
23 import org.mule.umo.lifecycle.InitialisationException;
24 import org.mule.util.ObjectFactory;
25 import org.mule.util.StringMessageUtils;
26
27 /**
28  * SecretKey based encryption using JCE. Users must specify a key as an array of
29  * bytes. This can be set directly on the strategy or a keyFactory can be specified.
30  * A keyFactory is an implementation of org.mule.util.ObjectFactory and must return a
31  * byte array. The default algorthm used by this strategy is Blowfish, but users can
32  * specify any valid algorithm supported by JCE.
33  *
34  * @see ObjectFactory
35  */

36 public class SecretKeyEncryptionStrategy extends AbstractJCEEncryptionStrategy
37 {
38
39     public static final String JavaDoc DEFAULT_ALGORITHM = "Blowfish";
40
41     private byte[] key;
42     private ObjectFactory keyFactory;
43
44     public SecretKeyEncryptionStrategy()
45     {
46         algorithm = DEFAULT_ALGORITHM;
47     }
48
49     public void initialise() throws InitialisationException
50     {
51         if (key == null)
52         {
53             if (keyFactory == null)
54             {
55                 throw new InitialisationException(new Message(Messages.X_IS_NULL, "Key / KeyFactory"), this);
56             }
57             else
58             {
59                 try
60                 {
61                     key = (byte[])keyFactory.create();
62                 }
63                 catch (Exception JavaDoc e)
64                 {
65                     throw new InitialisationException(e, this);
66                 }
67             }
68         }
69         super.initialise();
70     }
71
72     protected KeySpec JavaDoc createKeySpec()
73     {
74         return new SecretKeySpec(key, algorithm);
75     }
76
77     protected AlgorithmParameterSpec JavaDoc createAlgorithmParameterSpec()
78     {
79         return null;
80     }
81
82     public void setKey(byte[] rawKey)
83     {
84         this.key = rawKey;
85     }
86
87     public void setKey(String JavaDoc rawKey)
88     {
89         this.key = StringMessageUtils.getBytes(rawKey);
90     }
91
92     public ObjectFactory getKeyFactory()
93     {
94         return keyFactory;
95     }
96
97     public void setKeyFactory(ObjectFactory keyFactory)
98     {
99         this.keyFactory = keyFactory;
100     }
101
102     protected SecretKey getSecretKey() throws GeneralSecurityException JavaDoc
103     {
104         return KeyGenerator.getInstance(algorithm).generateKey();
105     }
106
107 }
108
Popular Tags