KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   Name: FileEncrypt.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.tagext.TagSupport JavaDoc;
27
28 /**
29  * JSP tag used for symmetric key encryption/decryption of files
30  *
31  * @jsp.tag
32  * name="fileencrypt"
33  * display-name="FileEncrypt"
34  * body-content="empty"
35  * example="
36  * <%-- Encrypts a file with standard algorithm --%>

37  * <jce:fileencrypt

38  * 	file=\"c:/zipfile.zip\"

39  * 	newfile=\"c:/zipfile.zip.encrypted\" 

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

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

42  * 	action=\"encrypt\"/> 


43  *
44  * <%-- Decrypts the encrypted file back again --%>

45  * <jce:fileencrypt

46  * 	file=\"c:/zipfile.zip.encrypted\"

47  * 	newfile=\"c:/zipfile.zip.decrypted\"

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

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

50  * 	action=\"decrypt\"/>"
51  *
52  * description="JSP tag used for symmetric key encryption/decryption of files"
53  *
54  * @author Gert Van Ham
55  * @author hamgert@users.sourceforge.net
56  * @author http://jcetaglib.sourceforge.net
57  * @version $Id: FileEncrypt.java,v 1.5 2004/04/15 07:28:35 hamgert Exp $
58  */

59 public class FileEncrypt extends TagSupport JavaDoc {
60     private static final String JavaDoc ENCRYPT = "encrypt";
61
62     private String JavaDoc file; // tag attribute
63
private String JavaDoc newfile; // tag attribute
64

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

71     private String JavaDoc action = "ENCRYPT"; // tag attribute
72

73     public int doStartTag() throws JspException JavaDoc {
74
75         // Encrypt or decrypt
76
try {
77             if (ENCRYPT.equalsIgnoreCase(action))
78                 Crypt.encryptFile(file, newfile, keyfile, passphrase, algorithm, mode, padding, null);
79             else
80                 Crypt.encryptFile(file, newfile, keyfile, passphrase, algorithm, mode, padding, null);
81         } catch (Exception JavaDoc e) {
82             throw new JspException JavaDoc("JCE Exception: " + e.getMessage(), e);
83         }
84
85         return SKIP_BODY;
86     } //doStartTag()
87

88     public void release() {
89         // Cleanup all sensitive information
90
Clean.blank(passphrase);
91
92         super.release();
93     } //release(
94

95     /**
96      * @jsp.attribute
97      * description="The key algorithm. Default is 'AES'"
98      * type="java.lang.String"
99      * required="false"
100      * rtexprvalue="true"
101      */

102     public void setAlgorithm(String JavaDoc algorithm) {
103         this.algorithm = algorithm;
104     }
105
106     /**
107      * @jsp.attribute
108      * description="Encryption mode. Default is 'CBC'"
109      * type="java.lang.String"
110      * required="false"
111      * rtexprvalue="true"
112      */

113     public void setMode(String JavaDoc mode) {
114         this.mode = mode;
115     }
116
117     /**
118      * @jsp.attribute
119      * description="Padding scheme. Default is 'PKCS7Padding'"
120      * type="java.lang.String"
121      * required="false"
122      * rtexprvalue="true"
123      */

124     public void setPadding(String JavaDoc padding) {
125         this.padding = padding;
126     }
127
128     /**
129      * @jsp.attribute
130      * description="The symmetric key file(name)"
131      * type="java.lang.String"
132      * required="true"
133      * rtexprvalue="true"
134      */

135     public void setKeyfile(String JavaDoc keyfile) {
136         this.keyfile = keyfile;
137     }
138
139     /**
140      * @jsp.attribute
141      * description="Keystore passphrase"
142      * type="java.lang.StringBuffer"
143      * required="true"
144      * rtexprvalue="true"
145      */

146     public void setPassphrase(StringBuffer JavaDoc passphrase) {
147         this.passphrase = passphrase;
148     }
149
150     /**
151      * @jsp.attribute
152      * description="The action to perform. 'encrypt' for encryption, 'decrypt' for decryption. Default is 'encrypt'"
153      * type="java.lang.String"
154      * required="false"
155      * rtexprvalue="false"
156      */

157     public void setAction(String JavaDoc action) {
158         this.action = action;
159     }
160
161     /**
162      * @jsp.attribute
163      * description="The file to encrypt or decrypt"
164      * type="java.lang.String"
165      * required="true"
166      * rtexprvalue="true"
167      */

168     public void setFile(String JavaDoc file) {
169         this.file = file;
170     }
171
172     /**
173      * @jsp.attribute
174      * description="The file to store the encrypted or decrypted result"
175      * type="java.lang.String"
176      * required="true"
177      * rtexprvalue="true"
178      */

179     public void setNewfile(String JavaDoc newfile) {
180         this.newfile = newfile;
181     }
182 }
Popular Tags