KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   Name: net.sourceforge.lightcrypto.Crypt
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.crypto.engines.RC4Engine;
24 import org.bouncycastle.crypto.params.KeyParameter;
25
26 import java.io.DataOutputStream JavaDoc;
27 import java.io.InputStream JavaDoc;
28
29 /**
30  * Streamcipher routines for use with the BouncyCastle lightweight API
31  *
32  * @author Gert Van Ham
33  * @author hamgert@users.sourceforge.net
34  * @author http://jcetaglib.sourceforge.net
35  * @version $Id: Stream.java,v 1.1 2003/10/28 20:12:53 hamgert Exp $
36  */

37
38 public class Stream {
39     /**
40      * Encrypts any inputstream with an RC4 stream cipher and returns
41      * the ciphered inputstream as a DataOutputStream
42      *
43      * @param is the inputstream to encrypt
44      * @param daos outputstream for the ciphertext
45      * @param keybytes the symmetric key (which generated with the net.sourceforge.lightcrypto.Key object)
46      * @param bufferlength buffer length in bytes
47      * @exception CryptoException for all encryption errors
48      **/

49     public static void encrypt(
50             InputStream JavaDoc is
51             , DataOutputStream JavaDoc daos
52             , SafeObject keybytes
53             , int bufferlength
54             ) throws CryptoException {
55
56         KeyParameter key = null;
57
58         try {
59             // Initialize the RC4 cipher
60
RC4Engine streamCipher = new RC4Engine();
61
62             // use the keybytes to create a key
63
key = new KeyParameter(keybytes.getBytes());
64
65             byte[] buffer = new byte[bufferlength];
66             int length = bufferlength;
67             byte[] result = new byte[length];
68
69             // initialize the cipher for encrypting with the key
70
streamCipher.init(true, key);
71
72             // read bytes into buffer and feed these bytes into the cipher
73
while ((length = is.read(buffer)) != -1) {
74                 streamCipher.processBytes(buffer, 0, length, result, 0);
75                 daos.write(result, 0, length);
76             }
77
78         } catch (Exception JavaDoc ex) {
79             ex.printStackTrace();
80             throw new CryptoException(ex.getMessage());
81         } finally {
82             // clean sensitive information from memory
83
key = null;
84         }
85     }
86
87     /**
88      * Decrypts a ciphered inputstream with an RC4 stream cipher
89      *
90      * @param is the inputstream to decipher
91      * @param daos outputstream for the plaintext
92      * @param keybytes the symmetric key (which generated with the net.sourceforge.lightcrypto.Key object)
93      * @param bufferlength buffer length in bytes
94      * @exception CryptoException for all encryption errors
95      **/

96     public static void decrypt(
97             InputStream JavaDoc is
98             , DataOutputStream JavaDoc daos
99             , SafeObject keybytes
100             , int bufferlength
101             ) throws CryptoException {
102         KeyParameter key = null;
103
104         try {
105             // use the keybytes to create a key
106
key = new KeyParameter(keybytes.getBytes());
107
108             // Initialize the RC4 cipher
109
RC4Engine streamCipher = new RC4Engine();
110
111             byte[] buffer = new byte[bufferlength];
112             int length = bufferlength;
113             byte[] result = new byte[length];
114
115             // initialize the cipher for decrypting with the key
116
streamCipher.init(false, key);
117
118             // read bytes into buffer and feed these bytes into the cipher
119
while ((length = is.read(buffer)) != -1) {
120                 streamCipher.processBytes(buffer, 0, length, result, 0);
121                 daos.write(result, 0, length);
122             }
123
124         } catch (Exception JavaDoc ex) {
125             ex.printStackTrace();
126             throw new CryptoException(ex.getMessage());
127
128         } finally {
129             // clean sensitive information from memory
130
key = null;
131         }
132     }
133 }
134
135
Popular Tags