public final void encryptStream ( SecretKey sharedKey , InputStream input , OutputStream output ) throws IOException { logger . entry ( ) ; CipherOutputStream cipherOutput = null ; byte [ ] buffer = new byte [ 2048 ] ; try { logger . debug ( "Creating a special output stream to do the work..." ) ; cipherOutput = encryptionOutputStream ( sharedKey , output ) ; logger . debug ( "Reading from the input and writing to the encrypting output stream..." ) ; // Can't use IOUtils.copy(input, cipherOutput) here because need to purge buffer later... int bytesRead ; while ( ( bytesRead = input . read ( buffer ) ) != - 1 ) { cipherOutput . write ( buffer , 0 , bytesRead ) ; } cipherOutput . flush ( ) ; } finally { logger . debug ( "Purging any plaintext hanging around in memory..." ) ; Arrays . fill ( buffer , ( byte ) 0 ) ; if ( cipherOutput != null ) cipherOutput . close ( ) ; } logger . exit ( ) ; }
This method encrypts a byte stream using a shared key .