/** * Does the decryption using inBuffer as input and outBuffer as output. * * @param state the CipherState instance. * @param inByteBuffer the input buffer. * @param outByteBuffer the output buffer. * @throws IOException if an I/O error occurs. */ private void decryptBuffer(CipherState state, ByteBuffer inByteBuffer, ByteBuffer outByteBuffer) throws IOException { int inputSize = inByteBuffer.remaining(); try { int n = state.getCryptoCipher().update(inByteBuffer, outByteBuffer); if (n < inputSize) { /** * Typically code will not get here. CryptoCipher#update will * consume all input data and put result in outBuffer. * CryptoCipher#doFinal will reset the cipher context. */ state.getCryptoCipher().doFinal(inByteBuffer, outByteBuffer); state.reset(true); } } catch (ShortBufferException e) { throw new IOException(e); } catch (IllegalBlockSizeException e) { throw new IOException(e); } catch (BadPaddingException e) { throw new IOException(e); } }
/** * Calculates the counter and iv, reset the cipher. * * @param state the CipherState instance. * @param position the offset from the start of the stream. * @param iv the iv. * @throws IOException if an I/O error occurs. */ private void resetCipher(CipherState state, long position, byte[] iv) throws IOException { final long counter = getCounter(position); CtrCryptoInputStream.calculateIV(getInitIV(), counter, iv); try { state.getCryptoCipher().init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv)); } catch (InvalidKeyException e) { throw new IOException(e); } catch (InvalidAlgorithmParameterException e) { throw new IOException(e); } state.reset(false); }