KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > pdf > OutputStreamEncryption


1 /*
2  * $Id: OutputStreamEncryption.java 2730 2007-04-28 12:52:49Z blowagie $
3  *
4  * Copyright 2006 Paulo Soares
5  *
6  * The contents of this file are subject to the Mozilla Public License Version 1.1
7  * (the "License"); you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12  * for the specific language governing rights and limitations under the License.
13  *
14  * The Original Code is 'iText, a free JAVA-PDF library'.
15  *
16  * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
17  * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
18  * All Rights Reserved.
19  * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
20  * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source code
23  * where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above. If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
34  *
35  * This library is free software; you can redistribute it and/or modify it
36  * under the terms of the MPL as stated above or under the terms of the GNU
37  * Library General Public License as published by the Free Software Foundation;
38  * either version 2 of the License, or any later version.
39  *
40  * This library is distributed in the hope that it will be useful, but WITHOUT
41  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
42  * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
43  * details.
44  *
45  * If you didn't download this code from the following link, you should check if
46  * you aren't using an obsolete version:
47  * http://www.lowagie.com/iText/
48  */

49 package com.lowagie.text.pdf;
50 import com.lowagie.text.ExceptionConverter;
51 import com.lowagie.text.pdf.crypto.AESCipher;
52 import com.lowagie.text.pdf.crypto.IVGenerator;
53 import com.lowagie.text.pdf.crypto.ARCFOUREncryption;
54 import java.io.IOException JavaDoc;
55 import java.io.OutputStream JavaDoc;
56
57 public class OutputStreamEncryption extends OutputStream JavaDoc {
58     
59     protected OutputStream JavaDoc out;
60     protected ARCFOUREncryption arcfour;
61     protected AESCipher cipher;
62     private byte[] sb = new byte[1];
63     private static final int AES_128 = 4;
64     private boolean aes;
65     private boolean finished;
66     
67     /** Creates a new instance of OutputStreamCounter */
68     public OutputStreamEncryption(OutputStream JavaDoc out, byte key[], int off, int len, int revision) {
69         try {
70             this.out = out;
71             aes = revision == AES_128;
72             if (aes) {
73                 byte[] iv = IVGenerator.getIV();
74                 byte[] nkey = new byte[len];
75                 System.arraycopy(key, off, nkey, 0, len);
76                 cipher = new AESCipher(true, nkey, iv);
77                 write(iv);
78             }
79             else {
80                 arcfour = new ARCFOUREncryption();
81                 arcfour.prepareARCFOURKey(key, off, len);
82             }
83         } catch (Exception JavaDoc ex) {
84             throw new ExceptionConverter(ex);
85         }
86     }
87     
88     public OutputStreamEncryption(OutputStream JavaDoc out, byte key[], int revision) {
89         this(out, key, 0, key.length, revision);
90     }
91     
92     /** Closes this output stream and releases any system resources
93      * associated with this stream. The general contract of <code>close</code>
94      * is that it closes the output stream. A closed stream cannot perform
95      * output operations and cannot be reopened.
96      * <p>
97      * The <code>close</code> method of <code>OutputStream</code> does nothing.
98      *
99      * @exception IOException if an I/O error occurs.
100      *
101      */

102     public void close() throws IOException JavaDoc {
103         finish();
104         out.close();
105     }
106     
107     /** Flushes this output stream and forces any buffered output bytes
108      * to be written out. The general contract of <code>flush</code> is
109      * that calling it is an indication that, if any bytes previously
110      * written have been buffered by the implementation of the output
111      * stream, such bytes should immediately be written to their
112      * intended destination.
113      * <p>
114      * The <code>flush</code> method of <code>OutputStream</code> does nothing.
115      *
116      * @exception IOException if an I/O error occurs.
117      *
118      */

119     public void flush() throws IOException JavaDoc {
120         out.flush();
121     }
122     
123     /** Writes <code>b.length</code> bytes from the specified byte array
124      * to this output stream. The general contract for <code>write(b)</code>
125      * is that it should have exactly the same effect as the call
126      * <code>write(b, 0, b.length)</code>.
127      *
128      * @param b the data.
129      * @exception IOException if an I/O error occurs.
130      * @see java.io.OutputStream#write(byte[], int, int)
131      *
132      */

133     public void write(byte[] b) throws IOException JavaDoc {
134         write(b, 0, b.length);
135     }
136     
137     /** Writes the specified byte to this output stream. The general
138      * contract for <code>write</code> is that one byte is written
139      * to the output stream. The byte to be written is the eight
140      * low-order bits of the argument <code>b</code>. The 24
141      * high-order bits of <code>b</code> are ignored.
142      * <p>
143      * Subclasses of <code>OutputStream</code> must provide an
144      * implementation for this method.
145      *
146      * @param b the <code>byte</code>.
147      * @exception IOException if an I/O error occurs. In particular,
148      * an <code>IOException</code> may be thrown if the
149      * output stream has been closed.
150      *
151      */

152     public void write(int b) throws IOException JavaDoc {
153         sb[0] = (byte)b;
154         write(sb, 0, 1);
155     }
156     
157     /** Writes <code>len</code> bytes from the specified byte array
158      * starting at offset <code>off</code> to this output stream.
159      * The general contract for <code>write(b, off, len)</code> is that
160      * some of the bytes in the array <code>b</code> are written to the
161      * output stream in order; element <code>b[off]</code> is the first
162      * byte written and <code>b[off+len-1]</code> is the last byte written
163      * by this operation.
164      * <p>
165      * The <code>write</code> method of <code>OutputStream</code> calls
166      * the write method of one argument on each of the bytes to be
167      * written out. Subclasses are encouraged to override this method and
168      * provide a more efficient implementation.
169      * <p>
170      * If <code>b</code> is <code>null</code>, a
171      * <code>NullPointerException</code> is thrown.
172      * <p>
173      * If <code>off</code> is negative, or <code>len</code> is negative, or
174      * <code>off+len</code> is greater than the length of the array
175      * <code>b</code>, then an <tt>IndexOutOfBoundsException</tt> is thrown.
176      *
177      * @param b the data.
178      * @param off the start offset in the data.
179      * @param len the number of bytes to write.
180      * @exception IOException if an I/O error occurs. In particular,
181      * an <code>IOException</code> is thrown if the output
182      * stream is closed.
183      *
184      */

185     public void write(byte[] b, int off, int len) throws IOException JavaDoc {
186         if (aes) {
187             byte[] b2 = cipher.update(b, off, len);
188             if (b2 == null || b2.length == 0)
189                 return;
190             out.write(b2, 0, b2.length);
191         }
192         else {
193             byte[] b2 = new byte[Math.min(len, 4192)];
194             while (len > 0) {
195                 int sz = Math.min(len, b2.length);
196                 arcfour.encryptARCFOUR(b, off, sz, b2, 0);
197                 out.write(b2, 0, sz);
198                 len -= sz;
199                 off += sz;
200             }
201         }
202     }
203     
204     public void finish() throws IOException JavaDoc {
205         if (!finished) {
206             finished = true;
207             if (aes) {
208                 byte[] b;
209                 try {
210                     b = cipher.doFinal();
211                 } catch (Exception JavaDoc ex) {
212                     throw new ExceptionConverter(ex);
213                 }
214                 out.write(b, 0, b.length);
215             }
216         }
217     }
218 }
Popular Tags