001: /*
002: * $Id: OutputStreamEncryption.java 2730 2007-04-28 12:52:49Z blowagie $
003: *
004: * Copyright 2006 Paulo Soares
005: *
006: * The contents of this file are subject to the Mozilla Public License Version 1.1
007: * (the "License"); you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
009: *
010: * Software distributed under the License is distributed on an "AS IS" basis,
011: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
012: * for the specific language governing rights and limitations under the License.
013: *
014: * The Original Code is 'iText, a free JAVA-PDF library'.
015: *
016: * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
017: * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
018: * All Rights Reserved.
019: * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
020: * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
021: *
022: * Contributor(s): all the names of the contributors are added in the source code
023: * where applicable.
024: *
025: * Alternatively, the contents of this file may be used under the terms of the
026: * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
027: * provisions of LGPL are applicable instead of those above. If you wish to
028: * allow use of your version of this file only under the terms of the LGPL
029: * License and not to allow others to use your version of this file under
030: * the MPL, indicate your decision by deleting the provisions above and
031: * replace them with the notice and other provisions required by the LGPL.
032: * If you do not delete the provisions above, a recipient may use your version
033: * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
034: *
035: * This library is free software; you can redistribute it and/or modify it
036: * under the terms of the MPL as stated above or under the terms of the GNU
037: * Library General Public License as published by the Free Software Foundation;
038: * either version 2 of the License, or any later version.
039: *
040: * This library is distributed in the hope that it will be useful, but WITHOUT
041: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
042: * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
043: * details.
044: *
045: * If you didn't download this code from the following link, you should check if
046: * you aren't using an obsolete version:
047: * http://www.lowagie.com/iText/
048: */
049: package com.lowagie.text.pdf;
050:
051: import com.lowagie.text.ExceptionConverter;
052: import com.lowagie.text.pdf.crypto.AESCipher;
053: import com.lowagie.text.pdf.crypto.IVGenerator;
054: import com.lowagie.text.pdf.crypto.ARCFOUREncryption;
055: import java.io.IOException;
056: import java.io.OutputStream;
057:
058: public class OutputStreamEncryption extends OutputStream {
059:
060: protected OutputStream out;
061: protected ARCFOUREncryption arcfour;
062: protected AESCipher cipher;
063: private byte[] sb = new byte[1];
064: private static final int AES_128 = 4;
065: private boolean aes;
066: private boolean finished;
067:
068: /** Creates a new instance of OutputStreamCounter */
069: public OutputStreamEncryption(OutputStream out, byte key[],
070: int off, int len, int revision) {
071: try {
072: this .out = out;
073: aes = revision == AES_128;
074: if (aes) {
075: byte[] iv = IVGenerator.getIV();
076: byte[] nkey = new byte[len];
077: System.arraycopy(key, off, nkey, 0, len);
078: cipher = new AESCipher(true, nkey, iv);
079: write(iv);
080: } else {
081: arcfour = new ARCFOUREncryption();
082: arcfour.prepareARCFOURKey(key, off, len);
083: }
084: } catch (Exception ex) {
085: throw new ExceptionConverter(ex);
086: }
087: }
088:
089: public OutputStreamEncryption(OutputStream out, byte key[],
090: int revision) {
091: this (out, key, 0, key.length, revision);
092: }
093:
094: /** Closes this output stream and releases any system resources
095: * associated with this stream. The general contract of <code>close</code>
096: * is that it closes the output stream. A closed stream cannot perform
097: * output operations and cannot be reopened.
098: * <p>
099: * The <code>close</code> method of <code>OutputStream</code> does nothing.
100: *
101: * @exception IOException if an I/O error occurs.
102: *
103: */
104: public void close() throws IOException {
105: finish();
106: out.close();
107: }
108:
109: /** Flushes this output stream and forces any buffered output bytes
110: * to be written out. The general contract of <code>flush</code> is
111: * that calling it is an indication that, if any bytes previously
112: * written have been buffered by the implementation of the output
113: * stream, such bytes should immediately be written to their
114: * intended destination.
115: * <p>
116: * The <code>flush</code> method of <code>OutputStream</code> does nothing.
117: *
118: * @exception IOException if an I/O error occurs.
119: *
120: */
121: public void flush() throws IOException {
122: out.flush();
123: }
124:
125: /** Writes <code>b.length</code> bytes from the specified byte array
126: * to this output stream. The general contract for <code>write(b)</code>
127: * is that it should have exactly the same effect as the call
128: * <code>write(b, 0, b.length)</code>.
129: *
130: * @param b the data.
131: * @exception IOException if an I/O error occurs.
132: * @see java.io.OutputStream#write(byte[], int, int)
133: *
134: */
135: public void write(byte[] b) throws IOException {
136: write(b, 0, b.length);
137: }
138:
139: /** Writes the specified byte to this output stream. The general
140: * contract for <code>write</code> is that one byte is written
141: * to the output stream. The byte to be written is the eight
142: * low-order bits of the argument <code>b</code>. The 24
143: * high-order bits of <code>b</code> are ignored.
144: * <p>
145: * Subclasses of <code>OutputStream</code> must provide an
146: * implementation for this method.
147: *
148: * @param b the <code>byte</code>.
149: * @exception IOException if an I/O error occurs. In particular,
150: * an <code>IOException</code> may be thrown if the
151: * output stream has been closed.
152: *
153: */
154: public void write(int b) throws IOException {
155: sb[0] = (byte) b;
156: write(sb, 0, 1);
157: }
158:
159: /** Writes <code>len</code> bytes from the specified byte array
160: * starting at offset <code>off</code> to this output stream.
161: * The general contract for <code>write(b, off, len)</code> is that
162: * some of the bytes in the array <code>b</code> are written to the
163: * output stream in order; element <code>b[off]</code> is the first
164: * byte written and <code>b[off+len-1]</code> is the last byte written
165: * by this operation.
166: * <p>
167: * The <code>write</code> method of <code>OutputStream</code> calls
168: * the write method of one argument on each of the bytes to be
169: * written out. Subclasses are encouraged to override this method and
170: * provide a more efficient implementation.
171: * <p>
172: * If <code>b</code> is <code>null</code>, a
173: * <code>NullPointerException</code> is thrown.
174: * <p>
175: * If <code>off</code> is negative, or <code>len</code> is negative, or
176: * <code>off+len</code> is greater than the length of the array
177: * <code>b</code>, then an <tt>IndexOutOfBoundsException</tt> is thrown.
178: *
179: * @param b the data.
180: * @param off the start offset in the data.
181: * @param len the number of bytes to write.
182: * @exception IOException if an I/O error occurs. In particular,
183: * an <code>IOException</code> is thrown if the output
184: * stream is closed.
185: *
186: */
187: public void write(byte[] b, int off, int len) throws IOException {
188: if (aes) {
189: byte[] b2 = cipher.update(b, off, len);
190: if (b2 == null || b2.length == 0)
191: return;
192: out.write(b2, 0, b2.length);
193: } else {
194: byte[] b2 = new byte[Math.min(len, 4192)];
195: while (len > 0) {
196: int sz = Math.min(len, b2.length);
197: arcfour.encryptARCFOUR(b, off, sz, b2, 0);
198: out.write(b2, 0, sz);
199: len -= sz;
200: off += sz;
201: }
202: }
203: }
204:
205: public void finish() throws IOException {
206: if (!finished) {
207: finished = true;
208: if (aes) {
209: byte[] b;
210: try {
211: b = cipher.doFinal();
212: } catch (Exception ex) {
213: throw new ExceptionConverter(ex);
214: }
215: out.write(b, 0, b.length);
216: }
217: }
218: }
219: }
|