📄 encryptedoutputstream.java
字号:
/*************************************************************************"FreePastry" Peer-to-Peer Application Development Substrate Copyright 2002, Rice University. All rights reserved.Redistribution and use in source and binary forms, with or withoutmodification, are permitted provided that the following conditions aremet:- Redistributions of source code must retain the above copyrightnotice, this list of conditions and the following disclaimer.- Redistributions in binary form must reproduce the above copyrightnotice, this list of conditions and the following disclaimer in thedocumentation and/or other materials provided with the distribution.- Neither the name of Rice University (RICE) nor the names of itscontributors may be used to endorse or promote products derived fromthis software without specific prior written permission.This software is provided by RICE and the contributors on an "as is"basis, without any representations or warranties of any kind, expressor implied including, but not limited to, representations orwarranties of non-infringement, merchantability or fitness for aparticular purpose. In no event shall RICE or contributors be liablefor any direct, indirect, incidental, special, exemplary, orconsequential damages (including, but not limited to, procurement ofsubstitute goods or services; loss of use, data, or profits; orbusiness interruption) however caused and on any theory of liability,whether in contract, strict liability, or tort (including negligenceor otherwise) arising in any way out of the use of this software, evenif advised of the possibility of such damage.********************************************************************************/package rice.p2p.util;import java.io.*;import java.security.*;import java.util.*;/** * @(#) EncryptedOutputStream.java Class which serves as an output stream and * transparently encrypted the stream data with the given public key. It does * this by generating a symmetric key, k, and then encrypting k under the public * key. The header sent over the stream therefore looks like 4 bytes - * length(e(k)) e(k) chunks of upper-level data where each chunk is length(data) * [4 bytes] followed by data * * @version $Id: EncryptedOutputStream.java 2605 2005-06-24 22:49:20Z jeffh $ * @author Alan Mislove */public class EncryptedOutputStream extends OutputStream { // the maximal amount of data to buffer /** * DESCRIBE THE FIELD */ public final int BUFFER_SIZE; // = 32678; // the public key /** * DESCRIBE THE FIELD */ protected PublicKey publicKey; // the symmetric key /** * DESCRIBE THE FIELD */ protected byte[] key; // the actual underlying stream /** * DESCRIBE THE FIELD */ protected DataOutputStream stream; // the temporary array of bytes waiting to be sent out /** * DESCRIBE THE FIELD */ protected byte[] buffer; // the amount of the buffer which is currently used /** * DESCRIBE THE FIELD */ protected int bufferLength; /** * Builds an encrypted outputstream given a public key to encrypt thing under * * @param stream The underlying stream * @param publicKey DESCRIBE THE PARAMETER * @param bufferSize DESCRIBE THE PARAMETER * @exception IOException DESCRIBE THE EXCEPTION */ public EncryptedOutputStream(PublicKey publicKey, OutputStream stream, int bufferSize) throws IOException { BUFFER_SIZE = bufferSize; this.publicKey = publicKey; this.stream = new DataOutputStream(stream); this.key = SecurityUtils.generateKeySymmetric(); this.buffer = new byte[BUFFER_SIZE]; this.bufferLength = 0; writeHeader(); } /** * Utility method which writes out the header information * * @exception IOException DESCRIBE THE EXCEPTION */ private void writeHeader() throws IOException { byte[] enckey = SecurityUtils.encryptAsymmetric(key, publicKey); stream.writeInt(enckey.length); stream.write(enckey); } /** * Writes the specified byte to this output stream. * * @param b the byte * @exception IOException DESCRIBE THE EXCEPTION */ public void write(int b) throws IOException { write(new byte[]{(byte) (b & 0xff)}); } /** * Writes the given bytes to the output * * @param b the data. * @param off the start offset in the data. * @param len the number of bytes to write. * @exception IOException DESCRIBE THE EXCEPTION */ public void write(byte b[], int off, int len) throws IOException { if (b == null) { throw new NullPointerException(); } else if ((off < 0) || (off > b.length) || (len < 0) || ((off + len) > b.length) || ((off + len) < 0)) { throw new IndexOutOfBoundsException(); } else if (len == 0) { return; } // if it will fit in the buffer, just fill it up // otherwise, write what we can and recur if (len <= BUFFER_SIZE - bufferLength) { System.arraycopy(b, off, buffer, bufferLength, len); bufferLength += len; } else { int l = BUFFER_SIZE - bufferLength; System.arraycopy(b, off, buffer, bufferLength, l); bufferLength += l; writeBuffer(); write(b, off + l, len - l); } } /** * Internal method which writes out the buffered data * * @exception IOException DESCRIBE THE EXCEPTION */ protected void writeBuffer() throws IOException { byte[] encdata = SecurityUtils.encryptSymmetric(buffer, key, 0, bufferLength); stream.writeInt(encdata.length); stream.write(encdata); this.bufferLength = 0; } /** * Flushes this output stream and forces any buffered output bytes to be * written out. * * @exception IOException DESCRIBE THE EXCEPTION */ public void flush() throws IOException { writeBuffer(); stream.flush(); } /** * Closes this output stream and releases any system resources associated with * this stream. * * @exception IOException DESCRIBE THE EXCEPTION */ public void close() throws IOException { flush(); stream.close(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -