KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   Name: PBEEncrypt.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.PBECrypt;
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 PBE (password-based) encryption/decryption of strings
33  *
34  * @jsp.tag
35  * name="pbeencrypt"
36  * display-name="PBEEncrypt"
37  * body-content="JSP"
38  * example="
39  * <%-- Example 1: Encrypts a string with standard algorithm and prints it --%>

40  * <jce:pbeencrypt

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

42  * 	value=\"<%= new StringBuffer(\"Please encrypt this string\") %>\"/>


43  *
44  * <%-- Example 2: Encrypts tag body with TwoFish algorithm

45  * 	and store it in session variable 'myvar' --%>

46  * <jce:pbeencrypt 

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

48  * 	var=\"myvar\"

49  * 	scope=\"session\"

50  * 	algorithm=\"PBEWithSHAAndTwofish-CBC\">Please encrypt this string

51  * </jce:pbeencrypt>


52  *
53  * <%-- Example 3: Decrypts string --%>

54  * <jce:pbeencrypt

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

56  * 	value=\"<%= new StringBuffer(\"Hqd+709=\") %>\"

57  * 	action=\"decrypt\"/> "
58  *
59  * description="JSP tag used for PBE (password-based) encryption/decryption of strings"
60  *
61  * @author Gert Van Ham
62  * @author hamgert@users.sourceforge.net
63  * @author http://jcetaglib.sourceforge.net
64  * @version $Id: PBEEncrypt.java,v 1.5 2004/04/15 07:28:36 hamgert Exp $
65  */

66 public class PBEEncrypt extends BodyTagSupport JavaDoc {
67
68     private static final String JavaDoc PAGE = "page";
69     private static final String JavaDoc REQUEST = "request";
70     private static final String JavaDoc SESSION = "session";
71     private static final String JavaDoc APPLICATION = "application";
72
73     private static final String JavaDoc ENCRYPT = "encrypt";
74
75     private StringBuffer JavaDoc value; // tag attribute
76
private String JavaDoc var; // tag attribute
77
private int scope = PageContext.PAGE_SCOPE; // tag attribute
78

79     private StringBuffer JavaDoc passphrase; // tag attribute
80

81     private String JavaDoc algorithm = "PBEWithSHAAndIDEA-CBC"; // tag attribute
82
private String JavaDoc seed; // tag attribute
83

84     private String JavaDoc action = "ENCRYPT"; // tag attribute
85

86     private StringBuffer JavaDoc input; // what we'll store in scope:var
87
private StringBuffer JavaDoc output; // return text
88

89     public static int getScope(String JavaDoc scope) {
90         int ret = PageContext.PAGE_SCOPE; // default
91

92         if (REQUEST.equalsIgnoreCase(scope))
93             ret = PageContext.REQUEST_SCOPE;
94         else if (SESSION.equalsIgnoreCase(scope))
95             ret = PageContext.SESSION_SCOPE;
96         else if (APPLICATION.equalsIgnoreCase(scope))
97             ret = PageContext.APPLICATION_SCOPE;
98         else if (PAGE.equalsIgnoreCase(scope))
99             ret = PageContext.PAGE_SCOPE;
100
101         return ret;
102     } //getScope()
103

104     public int doEndTag() throws JspException JavaDoc {
105         // determine the value by...
106
if (value != null) {
107             // ... reading our attribute
108
input = value;
109         } else {
110             // ... retrieving and trimming our body
111
if (bodyContent == null || bodyContent.getString() == null) {
112                 input = new StringBuffer JavaDoc("");
113             } else {
114                 input = new StringBuffer JavaDoc(bodyContent.getString().trim());
115             }
116         }
117
118         // Encrypt or decrypt
119
try {
120             if (ENCRYPT.equalsIgnoreCase(action)) {
121                 if (seed == null) {
122                     output = PBECrypt.encrypt(input, passphrase, null, algorithm);
123                 } else {
124                     output = PBECrypt.encrypt(input, passphrase, seed.getBytes(), algorithm);
125                 }
126             } else {
127                 output = PBECrypt.decrypt(input, passphrase, algorithm);
128             }
129         } catch (Exception JavaDoc e) {
130             throw new JspException JavaDoc("JCE Exception: " + e.toString(), e);
131         }
132
133         // decide what to do with the result
134
if (var != null) {
135             if (output != null) {
136                 pageContext.setAttribute(var, output, scope);
137             }
138         } else {
139             if (bodyContent != null) {
140                 bodyContent.clearBody();
141             }
142
143             try {
144                 JspWriter JavaDoc w = pageContext.getOut();
145                 w.print(output);
146             } catch (IOException JavaDoc ex) {
147                 throw new JspException JavaDoc(ex.getMessage(), ex);
148             }
149         }
150
151         return EVAL_PAGE;
152     } // doEndTag()
153

154     public void release() {
155         // Cleanup all sensitive information
156
Clean.blank(value);
157         Clean.blank(passphrase);
158         Clean.blank(input);
159         Clean.blank(output);
160
161         super.release();
162     } //release()
163

164     /**
165      * @jsp.attribute
166      * description="Optional attribute, the string to encrypt or decrypt. The body of the tag will be taken if omitted"
167      * type="java.lang.StringBuffer"
168      * required="false"
169      * rtexprvalue="true"
170      */

171     public void setValue(StringBuffer JavaDoc value) {
172         this.value = value;
173     }
174
175     public StringBuffer JavaDoc getValue() {
176         return value;
177     }
178
179     /**
180      * @jsp.attribute
181      * description="Optional attribute, variable to store the encrypted or decrypted string. The encrypted/decrypted string will be printed if omitted"
182      * type="java.lang.String"
183      * required="false"
184      * rtexprvalue="false"
185      */

186     public void setVar(String JavaDoc var) {
187         this.var = var;
188     }
189
190     public String JavaDoc getVar() {
191         return var;
192     }
193
194     /**
195      * @jsp.attribute
196      * description="Scope of the 'var' attribute. Can be 'page', 'session', 'request' or 'application'. Default is 'page'"
197      * type="java.lang.String"
198      * required="false"
199      * rtexprvalue="false"
200      */

201     public void setScope(String JavaDoc scope) {
202         this.scope = getScope(scope);
203     }
204
205     /**
206      * @jsp.attribute
207      * description="The passphrase or password to encrypt/decrypt"
208      * type="java.lang.StringBuffer"
209      * required="true"
210      * rtexprvalue="true"
211      */

212     public void setPassphrase(StringBuffer JavaDoc passphrase) {
213         this.passphrase = passphrase;
214     }
215
216     /**
217      * @jsp.attribute
218      * description="The encryption/decryption algorithm. Default is 'PBEWithSHAAndIDEA-CBC'"
219      * type="java.lang.String"
220      * required="false"
221      * rtexprvalue="true"
222      */

223     public void setAlgorithm(String JavaDoc algorithm) {
224         this.algorithm = algorithm;
225     }
226
227     /**
228      * @jsp.attribute
229      * description="Optional seed for SecureRandom "
230      * type="java.lang.String"
231      * required="false"
232      * rtexprvalue="true"
233      */

234     public void setSeed(String JavaDoc seed) {
235         this.seed = seed;
236     }
237
238     /**
239      * @jsp.attribute
240      * description="The action to perform. 'encrypt' for encryption, 'decrypt' for decryption. Default is 'encrypt'"
241      * type="java.lang.String"
242      * required="false"
243      * rtexprvalue="false"
244      */

245     public void setAction(String JavaDoc action) {
246         this.action = action;
247     }
248 }
Popular Tags