📄 cipheroutputstream.java
字号:
/*Christoforos Pirillos @ Villanova University - May 1999based on code from the book "Java Network Programming" by Hughes*/package encryption;import java.io.*;/**A filtered OutputStream that attaches to an existing OutputStream andencrypts all data written to it. The Cipher that is used to perform theencryption is specified in the constructor*/public class CipherOutputStream extends FilterOutputStream {protected ByteArrayOutputStream byteO;protected OutputStream o;protected Cipher c;/**In the constructor we accept an OutputStream o over which we will sendencrypted data and a Cipher c with which we will perform the encryption.*/public CipherOutputStream (OutputStream o, Cipher c) { super (new ByteArrayOutputStream ()); byteO = (ByteArrayOutputStream) out; this.o = o; this.c = c;}public void flush () throws IOException { writeEncrypted(); o.flush();}public void close () throws IOException { flush(); o.close();}/**Encrypts and sends the data that we have buffered*/protected void writeEncrypted () throws IOException { if (byteO.size() > 0) { byte[] b = byteO.toByteArray(); byteO.reset(); byte[] plain = new byte[b.length+4]; Crypt.intToBytes (plain.length, plain, 0); System.arraycopy (b, 0, plain, 4, b.length); byte[] cipher = c.encipher (plain); o.write (cipher); }}} /* end of class CipherOutputStream */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -