/** * Calculates the counter and iv, resets the cipher. * * @param position the given position in the data. * @throws IOException if an I/O error occurs. */ protected void resetCipher(long position) throws IOException { final long counter = getCounter(position); CtrCryptoInputStream.calculateIV(initIV, counter, iv); try { cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv)); } catch (InvalidKeyException e) { throw new IOException(e); } catch (InvalidAlgorithmParameterException e) { throw new IOException(e); } cipherReset = false; }
/** * Resets the {@link #cipher}: calculate counter and {@link #padding}. * * @throws IOException if an I/O error occurs. */ private void resetCipher() throws IOException { final long counter = streamOffset / cipher.getBlockSize(); padding = (byte) (streamOffset % cipher.getBlockSize()); inBuffer.position(padding); // Set proper position for input data. CtrCryptoInputStream.calculateIV(initIV, counter, iv); try { cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(iv)); } catch (InvalidKeyException e) { throw new IOException(e); } catch (InvalidAlgorithmParameterException e) { throw new IOException(e); } cipherReset = false; }
/** * 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); }