KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   Name: GenerateKey.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.Keystore;
24
25 import javax.servlet.jsp.JspException JavaDoc;
26 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
27
28 /**
29  * JSP tag used to generate a secret/shared (=symmetric) key and store it in a file
30  *
31  * @jsp.tag
32  * name="generatekey"
33  * display-name="GenerateKey"
34  * body-content="empty"
35  * example="
36  * <%-- Generate a symmetric key file with default algorithm parameters 

37  * 	(AES, 256 bit key, CBC/PKCS7Padding) --%>

38  * <jce:generatekey 

39  * 	keyfile=\"c:/jspkey.key\" 

40  * 	passphrase=\"<%= new StringBuffer(\"password\") %>\"/>"
41  *
42  * description="JSP tag used to generate a secret/shared (=symmetric) key and store it in a file"
43  *
44  * @author Gert Van Ham
45  * @author hamgert@users.sourceforge.net
46  * @author http://jcetaglib.sourceforge.net
47  * @version $Id: GenerateKey.java,v 1.5 2004/04/15 07:28:35 hamgert Exp $
48  */

49 public class GenerateKey extends TagSupport JavaDoc {
50     private String JavaDoc algorithm = "AES"; // tag attribute
51
private int strength = 256; // tag attribute
52
private String JavaDoc seed; // tag attribute
53
private String JavaDoc keyfile; // tag attribute
54
private StringBuffer JavaDoc passphrase; // tag attribute
55

56     public int doStartTag() throws JspException JavaDoc {
57
58         try {
59             if (seed == null) {
60                 Keystore.generateKey(algorithm, strength, null, keyfile, passphrase);
61             } else {
62                 Keystore.generateKey(algorithm, strength, seed.getBytes(), keyfile, passphrase);
63             }
64         } catch (Exception JavaDoc e) {
65             e.printStackTrace();
66             throw new JspException JavaDoc("JCE Exception: " + e.getMessage(), e);
67         } finally {
68             this.release();
69         }
70
71         return SKIP_BODY;
72     }
73
74     public void release() {
75         // Cleanup all sensitive information
76
Clean.blank(passphrase);
77
78         super.release();
79     } //release()
80

81     /**
82      * @jsp.attribute
83      * description="The key algorithm. Default is 'AES'"
84      * type="java.lang.String"
85      * required="false"
86      * rtexprvalue="true"
87      */

88     public void setAlgorithm(String JavaDoc algorithm) {
89         this.algorithm = algorithm;
90     }
91
92     /**
93      * @jsp.attribute
94      * description="The key size in bits (integer value). Values depends on the used algorithm (see next paragraph). Default is 256 (for AES)"
95      * type="java.lang.String"
96      * required="false"
97      * rtexprvalue="true"
98      */

99     public void setStrength(int strength) {
100         this.strength = strength;
101     }
102
103     /**
104      * @jsp.attribute
105      * description="Optional seed for SecureRandom "
106      * type="java.lang.String"
107      * required="false"
108      * rtexprvalue="true"
109      */

110     public void setSeed(String JavaDoc seed) {
111         this.seed = seed;
112     }
113
114     /**
115      * @jsp.attribute
116      * description="The symmetric key file(name)"
117      * type="java.lang.String"
118      * required="true"
119      * rtexprvalue="true"
120      */

121     public void setKeyfile(String JavaDoc keyfile) {
122         this.keyfile = keyfile;
123     }
124
125     /**
126      * @jsp.attribute
127      * description="Keystore passphrase"
128      * type="java.lang.StringBuffer"
129      * required="true"
130      * rtexprvalue="true"
131      */

132     public void setPassphrase(StringBuffer JavaDoc passphrase) {
133         this.passphrase = passphrase;
134     }
135 }
Popular Tags