public void crypt(InputStream in, OutputStream out, Cipher cipher)
          throws GeneralSecurityException, IOException {
    int blocksize = cipher.getBlockSize();
    int outsize = cipher.getOutputSize(blocksize);
    byte[] inBytes = new byte[blocksize];
    byte[] outBytes = new byte[outsize];

    int inLen = 0;
    boolean more = true;
    while (more) {
      inLen = in.read(inBytes);
      if (inLen == blocksize) {
        int outLen = cipher.update(inBytes, 0, blocksize, outBytes);
        out.write(outBytes, 0, outLen);
      } else more = false;
    }

    if (inLen > 0) outBytes = cipher.doFinal(inBytes, 0, inLen);
    else outBytes = cipher.doFinal();

    out.write(outBytes);
  }