KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > lightcrypto > Hsqldb


1 /*
2   Name: net.sourceforge.lightcrypto.Hsqldb
3   Licensing: LGPL (lesser GNU Public License)
4   API: Bouncy Castle (http://www.bouncycastle.org) lightweight API
5
6   Disclaimer:
7
8   COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY OF ANY KIND,
9   EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES THAT THE COVERED CODE
10   IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE OR NON-INFRINGING. THE ENTIRE
11   RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED CODE IS WITH YOU. SHOULD ANY COVERED CODE
12   PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR)
13   ASSUME THE COST OF ANY NECESSARY SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY
14   CONSTITUTES AN ESSENTIAL PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED
15   HEREUNDER EXCEPT UNDER THIS DISCLAIMER.
16
17   (C) Copyright 2003 Gert Van Ham
18
19 */

20
21 package net.sourceforge.lightcrypto;
22
23 import org.bouncycastle.util.encoders.Base64;
24
25 import java.io.ByteArrayInputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27
28 /**
29  * High-level methods for use with HSQLDB database engine (http://hsqldb.sourceforge.net)
30  * These methods encapsulate functionality (use String instead of StringBuffer) so it
31  * can be used as a function inside SQL statements.
32  *
33  * @author Gert Van Ham
34  * @author hamgert@users.sourceforge.net
35  * @author http://jcetaglib.sourceforge.net
36  * @version $Id: Hsqldb.java,v 1.3 2003/10/28 20:12:53 hamgert Exp $
37  */

38
39 public class Hsqldb {
40     private static int BUFFERSIZE_TEXT = 64;
41
42     /**
43      * Creates a digest from a string
44      *
45      * @param text creates the digest from this text
46      * @param algorithm the digest algorithm. Can be 'SHA1' or 'MD5' (default)
47      * @return the generated digest string in BASE64
48      * @exception CryptoException for all encryption errors
49      **/

50     public static String JavaDoc digest(
51             String JavaDoc text
52             , String JavaDoc algorithm) throws CryptoException {
53         return Digesters.digest(new ByteArrayInputStream JavaDoc(text.getBytes()), algorithm, BUFFERSIZE_TEXT).toString();
54     }
55
56     /**
57      * Load a symmetric key from the file, unwrap it and return the key as a Base64 string
58      *
59      * @param file the filename where the symmetric key is store
60      * @param passphrase the passphrase for the symmetric key
61      * @return the key as a BASE64 string
62      * @throws Exception for all errors
63      * @throws CryptoException for all encryption errors
64      * @throws KeyException when the key could not be loaded
65      */

66     public static StringBuffer JavaDoc loadkey(
67             String JavaDoc file
68             , StringBuffer JavaDoc passphrase) throws Exception JavaDoc, CryptoException, KeyException {
69
70         SafeObject so = new SafeObject();
71         so = Key.loadkey(file, passphrase);
72
73         StringBuffer JavaDoc key = new StringBuffer JavaDoc(so.getBase64());
74         so.clearText();
75
76         return key;
77     }
78
79     /**
80      * Encrypts a string with a symmetric key (AES light engine, CBC mode, PKCS7 padding) and returns
81      * the ciphered text in BASE64 format.
82      *
83      * @param text the plain text
84      * @param key the key (in BASE64 format)
85      * @return the cipherstring in BASE64 format
86      * @throws Exception for all errors
87      * @throws CryptoException for all encryption errors
88      * @throws IOException I/O errors
89      */

90     public static String JavaDoc encrypt(
91             String JavaDoc text
92             , String JavaDoc key
93             ) throws Exception JavaDoc, CryptoException, IOException JavaDoc {
94
95         SafeObject k = new SafeObject();
96         k.setText(Base64.decode(key));
97
98         String JavaDoc cipher = new String JavaDoc(Crypt.encrypt(new StringBuffer JavaDoc(text), k));
99         k.clearText();
100
101         return cipher;
102     }
103
104     /**
105      * Decrypts a ciphered BASE64 string with a symmetric key (AES light engine, CBC mode, PKCS7 padding)
106      *
107      * @param text the text to decipher
108      * @param key key (in BASE64 format)
109      * @return the plain text
110      * @throws Exception for all errors
111      * @throws CryptoException for all encryption errors
112      * @throws IOException I/O errors
113      */

114     public static String JavaDoc decrypt(
115             String JavaDoc text
116             , String JavaDoc key
117             ) throws Exception JavaDoc, CryptoException, IOException JavaDoc {
118
119         SafeObject k = new SafeObject();
120         k.setText(Base64.decode(key));
121
122         String JavaDoc decipher = new String JavaDoc(Crypt.decrypt(new StringBuffer JavaDoc(text), k));
123         k.clearText();
124
125         return decipher;
126     }
127
128     /**
129      * Encrypts a string with PBE and returns
130      * the ciphered text in BASE64 format.
131      *
132      * @param text the plain text
133      * @param passphrase the password or passphrase
134      * @return the cipherstring in BASE64 format
135      * @throws Exception for all errors
136      * @throws CryptoException for all encryption errors
137      * @throws IOException I/O errors
138      */

139     public static String JavaDoc PBEEncrypt(
140             String JavaDoc text
141             , String JavaDoc passphrase
142             ) throws Exception JavaDoc, CryptoException, IOException JavaDoc {
143
144         String JavaDoc cipher = new String JavaDoc(PBECrypt.encrypt(new StringBuffer JavaDoc(text), new StringBuffer JavaDoc(passphrase)));
145
146         return cipher;
147     }
148
149      /**
150      * Decrypts a ciphered BASE64 string with PBE
151      *
152      * @param text the text to decipher
153      * @param passphrase password or passphrase
154      * @return the plain text
155      * @throws Exception for all errors
156      * @throws CryptoException for all encryption errors
157      * @throws IOException I/O errors
158      */

159     public static String JavaDoc PBEDecrypt(
160             String JavaDoc text
161             , String JavaDoc passphrase
162             ) throws Exception JavaDoc, CryptoException, IOException JavaDoc {
163
164         String JavaDoc decipher = new String JavaDoc(PBECrypt.decrypt(new StringBuffer JavaDoc(text), new StringBuffer JavaDoc(passphrase)));
165
166         return decipher;
167     }
168
169     /**
170      * Create HMAC (Hash Message Authentication Code)
171      *
172      * @param text creates HMAC from this text
173      * @return HMAC in BASE64 format
174      * @throws CryptoException for HMAC errors
175      */

176     public static String JavaDoc hmac(
177             String JavaDoc text
178             ) throws CryptoException {
179
180         String JavaDoc hmac = new String JavaDoc(HMacs.mac(new StringBuffer JavaDoc(text)));
181         return hmac;
182     }
183
184      /**
185      * Create MAC (Message Authentication Code)
186      *
187      * @param text creates MAC from this text
188      * @param key key (in BASE64 format)
189      * @return MAC in BASE64 format
190      * @throws CryptoException for MAC errors
191      */

192     public static String JavaDoc mac(
193                String JavaDoc text
194                ,String JavaDoc key
195                ) throws Exception JavaDoc, CryptoException {
196
197         SafeObject k = new SafeObject();
198         k.setText(Base64.decode(key));
199
200         String JavaDoc mac = new String JavaDoc(Macs.mac(new StringBuffer JavaDoc(text), k));
201         k.clearText();
202
203         return mac;
204     }
205 }
206
Popular Tags