Chapter 4 78 BASE64Decoder decoder = new BASE64Decoder(); byte[] saltArray = decoder.decodeBuffer(salt); byte[] ciphertextArray = decoder.decodeBuffer(ciphertext); // Create the PBEKeySpec with the given password PBEKeySpec keySpec = new PBEKeySpec(password); // Get a SecretKeyFactory for PBEWithSHAAndTwofish SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithSHAAndTwofish-CBC"); // Create our key SecretKey key = keyFactory.generateSecret(keySpec); // Now create a parameter spec for our salt and iterations PBEParameterSpec paramSpec = new PBEParameterSpec(saltArray, ITERATIONS); // Create a cipher and initialize it for encrypting Cipher cipher = Cipher.getInstance("PBEWithSHAAndTwofish-CBC"); cipher.init(Cipher.DECRYPT_MODE, key, paramSpec); // Perform the actual decryption byte[] plaintextArray = cipher.doFinal(ciphertextArray); return new String(plaintextArray); } } Running the Example After compiling, we can run the example. For example, to encrypt we could type: C:\> java PBE –e sasquatch "Hello World!" This will encrypt the string "Hello World!" using PBE with the password of "sasquatch". We should see output similar to the following: +xeivHEOb1M=AT/VYJlYIlJoQSMfKryaKw== We can decrypt the output with: C:\> java PBE –d sasquatch "+xeivHEOb1M=AT/VYJlYIlJoQSMfKryaKw==" and of course, we get our original message returned to us: Hello World!