question: private void byteArrayTest ( String transformation , byte [ ] key , byte [ ] iv ) throws GeneralSecurityException { int blockSize = enc . getBlockSize ( ) ; int [ ] dataLenList = transformation . equals ( "AES/CBC/NoPadding" ) ? new int [ ] { 10 * 1024 } : new int [ ] { 10 * 1024 , 10 * 1024 - 3 } ; for ( int dataLen : dataLenList ) { byte [ ] plainText = new byte [ dataLen ] ; Random random = new SecureRandom ( ) ; random . nextBytes ( plainText ) ; byte [ ] cipherText = new byte [ dataLen + blockSize ] ; int [ ] bufferLenList = new int [ ] { 2 * 1024 - 128 , 2 * 1024 - 125 } ; for ( int bufferLen : bufferLenList ) { resetCipher ( transformation , key , iv ) ; int offset = 0 ; int cipherPos = 0 ; for ( int i = 0 ; i < dataLen / bufferLen ; i ++ ) { cipherPos += enc . update ( plainText , offset , bufferLen , cipherText , cipherPos ) ; offset += bufferLen ; } cipherPos += enc . doFinal ( plainText , offset , dataLen % bufferLen , cipherText , cipherPos ) ; offset = 0 ; byte [ ] realPlainText = new byte [ cipherPos + blockSize ] ; int plainPos = 0 ; for ( int i = 0 ; i < cipherPos / bufferLen ; i ++ ) { plainPos += dec . update ( cipherText , offset , bufferLen , realPlainText , plainPos ) ; offset += bufferLen ; } plainPos += dec . doFinal ( cipherText , offset , cipherPos % bufferLen , realPlainText , plainPos ) ; Assert . assertEquals ( "random byte array length changes after transformation" , dataLen , plainPos ) ; byte [ ] shrinkPlainText = new byte [ plainPos ] ; System . arraycopy ( realPlainText , 0 , shrinkPlainText , 0 , plainPos ) ; Assert . assertArrayEquals ( "random byte array contents changes after transformation" , plainText , shrinkPlainText ) ; } } }
target:  test byte array whose data is randomly generated 