Chapter 4 88 Now we simply read the bytes from the input stream and write them to the cipher stream. This will encrypt the entire file. When we're done, we close the input and output. System.out.println("Encrypting the file..."); int theByte = 0; while ((theByte = fis.read()) != -1) { cos.write(theByte); } fis.close(); cos.close(); } Decrypting the file is just the opposite. We read in the IV, initialize a cipher, and create a CipherInputStream and use it to decrypt the file. /** *   Decrypt a file using Rijndael. Load the key *   from the filesystem, given a password. */ private static void decrypt(char[] password, String fileInput, String fileOutput) throws Exception { System.out.println("Loading the key."); Key key = loadKey(password); System.out.println("Loaded the key."); // Create a cipher using that key to initialize it Cipher cipher = Cipher.getInstance("Rijndael/CBC/PKCS5Padding"); FileInputStream fis = new FileInputStream(fileInput); FileOutputStream fos = new FileOutputStream(fileOutput); // Read the IV from the file. It's the first 16 bytes. byte[] iv = new byte[16]; fis.read(iv); IvParameterSpec spec = new IvParameterSpec(iv); System.out.println("Initializing the cipher."); cipher.init(Cipher.DECRYPT_MODE, key, spec); CipherInputStream cis = new CipherInputStream(fis, cipher); System.out.println("Decrypting the file..."); int theByte = 0; while ((theByte = cis.read()) != -1) { fos.write(theByte); } cis.close(); fos.close(); } }