KickJava   Java API By Example, From Geeks To Geeks.

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


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

37  * <jce:pbefileencrypt

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

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

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

41  * 	action=\"encrypt\"/>


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

44  * <jce:pbefileencrypt

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

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

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

48  * 	action=\"decrypt\"/>"
49  *
50  * description="JSP tag used for PBE (password-based) encryption/decryption of files"
51  *
52  * @author Gert Van Ham
53  * @author hamgert@users.sourceforge.net
54  * @author http://jcetaglib.sourceforge.net
55  * @version $Id: PBEFileEncrypt.java,v 1.5 2004/04/15 07:28:36 hamgert Exp $
56  */

57 public class PBEFileEncrypt extends TagSupport JavaDoc {
58     private static final String JavaDoc ENCRYPT = "encrypt";
59
60     private StringBuffer JavaDoc passphrase; // tag attribute
61
private String JavaDoc algorithm = "PBEWithSHAAndIDEA-CBC"; // tag attribute
62
private String JavaDoc seed; // tag attribute
63

64     private String JavaDoc action = "ENCRYPT"; // tag attribute
65

66     private String JavaDoc file; // tag attribute
67
private String JavaDoc newfile; // tag attribute
68

69     public int doStartTag() throws JspException JavaDoc {
70
71         // Encrypt or decrypt
72
try {
73             if (ENCRYPT.equalsIgnoreCase(action))
74                 if (seed == null) {
75                     PBECrypt.encryptFile(file, newfile, passphrase, null, algorithm);
76                 } else {
77                     PBECrypt.encryptFile(file, newfile, passphrase, seed.getBytes(), algorithm);
78                 }
79             else
80                 PBECrypt.decryptFile(file, newfile, passphrase, algorithm);
81         } catch (Exception JavaDoc e) {
82             // Cleanup sensitive information
83
Clean.blank(passphrase);
84             passphrase = null;
85             throw new JspException JavaDoc("JCE Exception: " + e.getMessage(), e);
86         }
87
88         // Cleanup all sensitive information
89
Clean.blank(passphrase);
90         passphrase = null;
91
92         return SKIP_BODY;
93     }
94
95     /**
96      * @jsp.attribute
97      * description="The passphrase or password to encrypt/decrypt"
98      * type="java.lang.StringBuffer"
99      * required="true"
100      * rtexprvalue="true"
101      */

102     public void setPassphrase(StringBuffer JavaDoc passphrase) {
103         this.passphrase = passphrase;
104     }
105
106     /**
107      * @jsp.attribute
108      * description="The encryption/decryption algorithm. Default is 'PBEWithSHAAndIDEA-CBC'"
109      * type="java.lang.String"
110      * required="false"
111      * rtexprvalue="true"
112      */

113     public void setAlgorithm(String JavaDoc algorithm) {
114         this.algorithm = algorithm;
115     }
116
117     /**
118      * @jsp.attribute
119      * description="Optional seed for SecureRandom "
120      * type="java.lang.String"
121      * required="false"
122      * rtexprvalue="true"
123      */

124     public void setSeed(String JavaDoc seed) {
125         this.seed = seed;
126     }
127
128     /**
129      * @jsp.attribute
130      * description="The action to perform. 'encrypt' for encryption, 'decrypt' for decryption. Default is 'encrypt'"
131      * type="java.lang.String"
132      * required="false"
133      * rtexprvalue="false"
134      */

135     public void setAction(String JavaDoc action) {
136         this.action = action;
137     }
138
139     /**
140      * @jsp.attribute
141      * description="The file to encrypt or decrypt"
142      * type="java.lang.String"
143      * required="true"
144      * rtexprvalue="true"
145      */

146     public void setFile(String JavaDoc file) {
147         this.file = file;
148     }
149
150     /**
151      * @jsp.attribute
152      * description="The file to store the encrypted or decrypted result"
153      * type="java.lang.String"
154      * required="true"
155      * rtexprvalue="true"
156      */

157     public void setNewfile(String JavaDoc newfile) {
158         this.newfile = newfile;
159     }
160 }
Popular Tags