KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > jcetaglib > jsp20 > ELFunctions


1 /*
2   Name: ELFunctions.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   (C) Copyright 2004 Gert Van Ham
20 */

21
22 package net.sourceforge.jcetaglib.jsp20;
23
24 import net.sourceforge.jcetaglib.exceptions.CryptoException;
25 import net.sourceforge.jcetaglib.exceptions.KeystoreException;
26 import net.sourceforge.jcetaglib.lib.Crypt;
27 import net.sourceforge.jcetaglib.lib.Digesters;
28 import net.sourceforge.jcetaglib.lib.Macs;
29 import net.sourceforge.jcetaglib.lib.PBECrypt;
30
31 import java.io.IOException JavaDoc;
32
33 /**
34  * JSP 2.0 EL (Expression Language) based JCE functions
35  *
36  * @author Gert Van Ham
37  * @author hamgert@users.sourceforge.net
38  * @author http://jcetaglib.sourceforge.net
39  * @version $Id: ELFunctions.java,v 1.1 2004/04/14 09:48:29 hamgert Exp $
40  */

41 public class ELFunctions {
42
43     /**
44      * Creates a digest from a string
45      *
46      * @param text creates the digest from this text
47      * @param algorithm the digest algorithm. 'SHA-1' if null
48      * @return the generated digest string in BASE64
49      * @throws CryptoException for all encryption errors
50      */

51     public static String JavaDoc digest(String JavaDoc text,
52                                 String JavaDoc algorithm) throws CryptoException {
53
54         if (algorithm == null || algorithm.equalsIgnoreCase("")) {
55             return new String JavaDoc(Digesters.hash(new StringBuffer JavaDoc(text), "SHA-1"));
56         } else {
57             return new String JavaDoc(Digesters.hash(new StringBuffer JavaDoc(text), algorithm));
58         }
59     }
60
61     /**
62      * Creates a form digest from a parameter string
63      *
64      * @param text create the form digest from this parameter string
65      * @param digest the digest algorithm. 'SHA-1' if null
66      * @param keyfile the symmetric key file
67      * @param passphrase the symmetric key file passphrase
68      * @param algorithm the symmetric key algorithm
69      * @return form digest in BASE64 format
70      * @throws CryptoException for all encryption errors
71      * @throws IOException I/O errors
72      */

73     public static String JavaDoc formdigest(String JavaDoc text,
74                                     String JavaDoc digest,
75                                     String JavaDoc keyfile,
76                                     String JavaDoc passphrase,
77                                     String JavaDoc algorithm) throws CryptoException, IOException JavaDoc {
78         if (digest == null || digest.equalsIgnoreCase("")) {
79             return new String JavaDoc(Digesters.formDigest(new StringBuffer JavaDoc(text), "SHA-1", keyfile, new StringBuffer JavaDoc(passphrase), algorithm));
80         } else {
81             return new String JavaDoc(Digesters.formDigest(new StringBuffer JavaDoc(text), digest, keyfile, new StringBuffer JavaDoc(passphrase), algorithm));
82         }
83     }
84
85     /**
86      * Encrypts a string with PBE and returns
87      * the ciphered text in BASE64 format.
88      *
89      * @param text the plain text
90      * @param passphrase the password or passphrase
91      * @param algorithm the PBE algorithm. "PBEWithSHAAndIDEA-CBC" if null
92      * @return the cipherstring in BASE64 format
93      * @throws CryptoException for all encryption errors
94      */

95     public static String JavaDoc pbeencrypt(String JavaDoc text,
96                                     String JavaDoc passphrase,
97                                     String JavaDoc algorithm) throws CryptoException {
98         if (algorithm == null || algorithm.equalsIgnoreCase("")) {
99             return new String JavaDoc(PBECrypt.encrypt(new StringBuffer JavaDoc(text), new StringBuffer JavaDoc(passphrase), "PBEWithSHAAndIDEA-CBC"));
100         } else {
101             return new String JavaDoc(PBECrypt.encrypt(new StringBuffer JavaDoc(text), new StringBuffer JavaDoc(passphrase), algorithm));
102         }
103     }
104
105     /**
106      * Decrypts a ciphered BASE64 string with PBE
107      *
108      * @param ciphertext the text to decipher
109      * @param passphrase password or passphrase
110      * @param algorithm the PBE algorithm. "PBEWithSHAAndIDEA-CBC" if null
111      * @return the plain text
112      * @throws CryptoException for all encryption errors
113      */

114     public static String JavaDoc pbedecrypt(String JavaDoc ciphertext,
115                                     String JavaDoc passphrase,
116                                     String JavaDoc algorithm) throws CryptoException {
117         if (algorithm == null || algorithm.equalsIgnoreCase("")) {
118             return new String JavaDoc(PBECrypt.decrypt(new StringBuffer JavaDoc(ciphertext), new StringBuffer JavaDoc(passphrase), "PBEWithSHAAndIDEA-CBC"));
119         } else {
120             return new String JavaDoc(PBECrypt.decrypt(new StringBuffer JavaDoc(ciphertext), new StringBuffer JavaDoc(passphrase), algorithm));
121         }
122     }
123
124     /**
125      * Encrypts a string with a symmetric key and returns
126      * the ciphered text in BASE64 format.
127      *
128      * @param text the string to encrypt
129      * @param keyfile the symmetric key file
130      * @param passphrase symmetric key file passphrase
131      * @param algorithm symmetric key file algorithm. AES if null
132      * @param mode encryption mode. CBC if null
133      * @param padding encryption padding. PKCS7Padding if null
134      * @return the encrypted text in BASE64 format
135      * @throws CryptoException encryption errors
136      * @throws KeystoreException keystore errors
137      */

138     public static String JavaDoc encrypt(String JavaDoc text,
139                                  String JavaDoc keyfile,
140                                  String JavaDoc passphrase,
141                                  String JavaDoc algorithm,
142                                  String JavaDoc mode,
143                                  String JavaDoc padding) throws CryptoException, KeystoreException {
144
145         String JavaDoc t_algorithm = algorithm;
146         String JavaDoc t_mode = mode;
147         String JavaDoc t_padding = padding;
148
149         if (algorithm == null || algorithm.equalsIgnoreCase("")) {
150             t_algorithm = "AES";
151         }
152
153         if (mode == null || mode.equalsIgnoreCase("")) {
154             t_mode = "CBC";
155         }
156
157         if (padding == null || padding.equalsIgnoreCase("")) {
158             t_padding = "PKCS7Padding";
159         }
160
161         return new String JavaDoc(Crypt.encrypt(new StringBuffer JavaDoc(text), keyfile, new StringBuffer JavaDoc(passphrase), t_algorithm, t_mode, t_padding, null));
162     }
163
164     /**
165      * Decrypts a ciphered BASE64 string with a symmetric key
166      *
167      * @param ciphertext the ciphered BASE64 string
168      * @param keyfile the symmetric key file
169      * @param passphrase symmetric key file passphrase
170      * @param algorithm symmetric key file algorithm. AES if null
171      * @param mode encryption mode. CBC if null
172      * @param padding encryption padding. PKCS7Padding if null
173      * @return the plaintext
174      * @throws CryptoException encryption errors
175      * @throws KeystoreException keystore errors
176      */

177     public static String JavaDoc decrypt(String JavaDoc ciphertext,
178                                  String JavaDoc keyfile,
179                                  String JavaDoc passphrase,
180                                  String JavaDoc algorithm,
181                                  String JavaDoc mode,
182                                  String JavaDoc padding) throws CryptoException, KeystoreException {
183
184         String JavaDoc t_algorithm = algorithm;
185         String JavaDoc t_mode = mode;
186         String JavaDoc t_padding = padding;
187
188         if (algorithm == null || algorithm.equalsIgnoreCase("")) {
189             t_algorithm = "AES";
190         }
191
192         if (mode == null || mode.equalsIgnoreCase("")) {
193             t_mode = "CBC";
194         }
195
196         if (padding == null || padding.equalsIgnoreCase("")) {
197             t_padding = "PKCS7Padding";
198         }
199
200         return new String JavaDoc(Crypt.decrypt(new StringBuffer JavaDoc(ciphertext), keyfile, new StringBuffer JavaDoc(passphrase), t_algorithm, t_mode, t_padding));
201     }
202
203     /**
204      * Create a MAC from a string
205      *
206      * @param text the text string
207      * @param keyfile the symmetric key file location
208      * @param passphrase the symmetric key file passphrase
209      * @param algorithm the symmetric key file algorithm
210      * @param macname MAC algorithm. "HMac-SHA512" if null
211      * @return MAC code
212      * @throws CryptoException MAC errors
213      */

214     public static String JavaDoc mac(String JavaDoc text,
215                              String JavaDoc keyfile,
216                              String JavaDoc passphrase,
217                              String JavaDoc algorithm,
218                              String JavaDoc macname) throws CryptoException {
219         if (macname == null || macname.equalsIgnoreCase("")) {
220             return new String JavaDoc(Macs.generateMAC(new StringBuffer JavaDoc(text), keyfile, new StringBuffer JavaDoc(passphrase), algorithm, "HMac-SHA512"));
221         } else {
222             return new String JavaDoc(Macs.generateMAC(new StringBuffer JavaDoc(text), keyfile, new StringBuffer JavaDoc(passphrase), algorithm, macname));
223         }
224     }
225 }
Popular Tags