public void doCipherOperation(InputStream in, OutputStream out, long nrOfBytesToCipher, int cipherMode, ProgressInfo progress) throws Exception {

		if (nrOfBytesToCipher > 0) {

			try {
				// If the nr of bytes to read is smaller than readBufferSize
				// use the nr of bytes as the max buffer size.
				int maxBufferSize = (int) Math.min(readBufferSize, nrOfBytesToCipher);
				byte[] readBuffer = new byte[maxBufferSize];

				if (isInitialized == false) {
					cipher.init(cipherMode, cipherKey, new IvParameterSpec(initializationVector.getEncoded()));
					isInitialized = true;
				}

				long nrOfBytesLeftToCipher = nrOfBytesToCipher;

				while (nrOfBytesLeftToCipher > 0) {

					in.read(readBuffer, 0, maxBufferSize);

					nrOfBytesLeftToCipher -= cipher.update(readBuffer, 0, maxBufferSize, readBuffer);

					// If a ProgressInfo was given, calculate the current
					// percentage and print it
					if (progress != null)
						progress.trackAndPrintProgress();

					out.write(readBuffer, 0, maxBufferSize);

					if (nrOfBytesLeftToCipher > 0 && nrOfBytesLeftToCipher < maxBufferSize) {
						maxBufferSize = (int) nrOfBytesLeftToCipher;
					}
				}
				//cipher.doFinal();

			} catch (IOException e) {
				throw new IOException("Error reading bytes to encrypt: " + e.getMessage());
			} catch (InvalidKeyException e) {
				throw new InvalidKeyException("Error : " + e.getMessage() + "\nKey file corrupt or invalid key parameters." + "\nTo use key sizes above 128 bits please install the JCE Unlimited Strength Jurisdiction Policy Files.");
			}
		} else {
			// Print progress even if nrOfBytesToCipher == 0
			if (progress != null)
				progress.trackAndPrintProgress();
		}
	}