KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > jcetaglib > taglib > crypto > Encrypt


1 /*
2   Name: Encrypt.java
3   Licensing: LGPL
4
5   API: Sun (http://java.sun.com) JCE 1.2.2 API (cleanroom implementation by Bouncy Castle)
6   Provider: Bouncy Castle (http://www.bouncycastle.org)
7
8   Disclaimer:
9
10   COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY OF ANY KIND,
11   EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES THAT THE COVERED CODE
12   IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE OR NON-INFRINGING. THE ENTIRE
13   RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED CODE IS WITH YOU. SHOULD ANY COVERED CODE
14   PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR)
15   ASSUME THE COST OF ANY NECESSARY SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY
16   CONSTITUTES AN ESSENTIAL PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED
17   HEREUNDER EXCEPT UNDER THIS DISCLAIMER.
18 */

19
20 package net.sourceforge.jcetaglib.taglib.crypto;
21
22 import net.sourceforge.jcetaglib.lib.Clean;
23 import net.sourceforge.jcetaglib.lib.Crypt;
24
25 import javax.servlet.jsp.JspException JavaDoc;
26 import javax.servlet.jsp.JspWriter JavaDoc;
27 import javax.servlet.jsp.PageContext JavaDoc;
28 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
29 import java.io.IOException JavaDoc;
30
31 /**
32  * JSP tag used for symmetric key encryption/decryption of strings
33  *
34  * @jsp.tag
35  * name="encrypt"
36  * display-name="Encrypt"
37  * body-content="JSP"
38  * example="
39  * <%-- Encrypts tag body with keyfile and store it in session variable 'myvar' --%>

40  * <jce:encrypt keyfile=\"c:/jspkey.key\"

41  * 	var=\"myvar\"

42  * 	passphrase=\"<%= new StringBuffer(\"password\") %>\" 

43  * 	scope=\"session\">

44  * Please encrypt this string...

45  * </jce:encrypt>"
46  *
47  * description="JSP tag used for symmetric key encryption/decryption of strings"
48  *
49  * @author Gert Van Ham
50  * @author hamgert@users.sourceforge.net
51  * @author http://jcetaglib.sourceforge.net
52  * @version $Id: Encrypt.java,v 1.5 2004/04/15 07:28:35 hamgert Exp $
53  */

54 public class Encrypt extends BodyTagSupport JavaDoc {
55
56     private static final String JavaDoc PAGE = "page";
57     private static final String JavaDoc REQUEST = "request";
58     private static final String JavaDoc SESSION = "session";
59     private static final String JavaDoc APPLICATION = "application";
60
61     private static final String JavaDoc ENCRYPT = "encrypt";
62
63     private StringBuffer JavaDoc value; // tag attribute
64
private String JavaDoc var; // tag attribute
65
private int scope = PageContext.PAGE_SCOPE; // tag attribute
66

67     private String JavaDoc algorithm = "AES"; // tag attribute
68
private String JavaDoc mode = "CBC"; // tag attribute
69
private String JavaDoc padding = "PKCS7Padding"; // tag attribute
70
private String JavaDoc keyfile; // tag attribute
71
private StringBuffer JavaDoc passphrase; // tag attribute
72

73     private String JavaDoc action = "ENCRYPT"; // tag attribute
74

75     private StringBuffer JavaDoc input; // what we'll store in scope:var
76
private StringBuffer JavaDoc output; // return text
77

78     public static int getScope(String JavaDoc scope) {
79         int ret = PageContext.PAGE_SCOPE; // default
80

81         if (REQUEST.equalsIgnoreCase(scope))
82             ret = PageContext.REQUEST_SCOPE;
83         else if (SESSION.equalsIgnoreCase(scope))
84             ret = PageContext.SESSION_SCOPE;
85         else if (APPLICATION.equalsIgnoreCase(scope))
86             ret = PageContext.APPLICATION_SCOPE;
87         else if (PAGE.equalsIgnoreCase(scope))
88             ret = PageContext.PAGE_SCOPE;
89
90         return ret;
91     } //getScope()
92

93     public int doEndTag() throws JspException JavaDoc {
94         // determine the value by...
95
if (value != null) {
96             // ... reading our attribute
97
input = value;
98         } else {
99             // ... retrieving and trimming our body
100
if (bodyContent == null || bodyContent.getString() == null) {
101                 input = new StringBuffer JavaDoc("");
102             } else {
103                 input = new StringBuffer JavaDoc(bodyContent.getString().trim());
104             }
105         }
106
107         // Encrypt or decrypt
108
try {
109             if (ENCRYPT.equalsIgnoreCase(action)) {
110                 output = Crypt.encrypt(input, keyfile, passphrase, algorithm, mode, padding, null);
111             } else {
112                 output = Crypt.decrypt(input, keyfile, passphrase, algorithm, mode, padding);
113             }
114         } catch (Exception JavaDoc e) {
115             throw new JspException JavaDoc("JCE Exception: " + e.getMessage(), e);
116         }
117
118         // decide what to do with the result
119
if (var != null) {
120             if (output != null) {
121                 pageContext.setAttribute(var, output, scope);
122             }
123         } else {
124             if (bodyContent != null) {
125                 bodyContent.clearBody();
126             }
127
128             try {
129                 JspWriter JavaDoc w = pageContext.getOut();
130                 w.print(output);
131             } catch (IOException JavaDoc ex) {
132                 throw new JspException JavaDoc(ex.getMessage(), ex);
133             }
134         }
135
136         return EVAL_PAGE;
137     } // doEndTag()
138

139     public void release() {
140         // Cleanup all sensitive information
141
Clean.blank(value);
142         Clean.blank(passphrase);
143         Clean.blank(input);
144         Clean.blank(output);
145
146         super.release();
147     } //release()
148

149     /**
150      * @jsp.attribute
151      * description="Optional attribute, the string to encrypt or decrypt. The body of the tag will be taken if omitted"
152      * type="java.lang.StringBuffer"
153      * required="false"
154      * rtexprvalue="true"
155      */

156     public void setValue(StringBuffer JavaDoc value) {
157         this.value = value;
158     }
159
160     public StringBuffer JavaDoc getValue() {
161         return value;
162     }
163
164     /**
165      * @jsp.attribute
166      * description="Optional attribute, variable to store the encrypted or decrypted string. The encrypted/decrypted string will be printed if omitted"
167      * type="java.lang.String"
168      * required="false"
169      * rtexprvalue="false"
170      */

171     public void setVar(String JavaDoc var) {
172         this.var = var;
173     }
174
175     public String JavaDoc getVar() {
176         return var;
177     }
178
179     /**
180      * @jsp.attribute
181      * description="Scope of the 'var' attribute. Can be 'page', 'session', 'request' or 'application'. Default is 'page'"
182      * type="java.lang.String"
183      * required="false"
184      * rtexprvalue="false"
185      */

186     public void setScope(String JavaDoc scope) {
187         this.scope = getScope(scope);
188     }
189
190     /**
191      * @jsp.attribute
192      * description="The key algorithm. Default is 'AES'"
193      * type="java.lang.String"
194      * required="false"
195      * rtexprvalue="true"
196      */

197     public void setAlgorithm(String JavaDoc algorithm) {
198         this.algorithm = algorithm;
199     }
200
201     /**
202      * @jsp.attribute
203      * description="Encryption mode. Default is 'CBC'"
204      * type="java.lang.String"
205      * required="false"
206      * rtexprvalue="true"
207      */

208     public void setMode(String JavaDoc mode) {
209         this.mode = mode;
210     }
211
212     /**
213      * @jsp.attribute
214      * description="Padding scheme. Default is 'PKCS7Padding'"
215      * type="java.lang.String"
216      * required="false"
217      * rtexprvalue="true"
218      */

219     public void setPadding(String JavaDoc padding) {
220         this.padding = padding;
221     }
222
223     /**
224      * @jsp.attribute
225      * description="The symmetric key file(name)"
226      * type="java.lang.String"
227      * required="true"
228      * rtexprvalue="true"
229      */

230     public void setKeyfile(String JavaDoc keyfile) {
231         this.keyfile = keyfile;
232     }
233
234     /**
235      * @jsp.attribute
236      * description="Keystore passphrase"
237      * type="java.lang.StringBuffer"
238      * required="true"
239      * rtexprvalue="true"
240      */

241     public void setPassphrase(StringBuffer JavaDoc passphrase) {
242         this.passphrase = passphrase;
243     }
244
245     /**
246      * @jsp.attribute
247      * description="The action to perform. 'encrypt' for encryption, 'decrypt' for decryption. Default is 'encrypt'"
248      * type="java.lang.String"
249      * required="false"
250      * rtexprvalue="false"
251      */

252     public void setAction(String JavaDoc action) {
253         this.action = action;
254     }
255 }
Popular Tags