Decrypting Data

Decrypting the data is just as easy (or hard) as encrypting it. You've already seen the steps you must take, because you still create a key and initialize a Cipher object. The major difference is that the cipher mode must be Cipher.DECRYPT_MODE instead of Cipher.ENCRYPT_MODE. Other than that, you still use update and doFinal to process the data, or use CipherInputStream to read encrypted data from a stream and decrypt it.

Listing 33.4 shows a program that decrypts the data encrypted by the program in Listing 33.2.

Code Listing 33.4. Source Code for DecryptData.java
 package usingj2ee.security; import java.io.*; import javax.crypto.*; import javax.crypto.spec.*; import java.security.*; public class DecryptData { public static void ...

Get Special Edition Using Java™ 2 Enterprise Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.