CIS 458 Lab 2: Java Encryption


What to turn in: Turn in a listing of your code. This should not be long, each of my programs for this lab are far less than 100 lines. Also, demonstrate your program working in lab on or before the due date.

For this lab, you will write two Java programs. One of these will encrypt a file using AES, the other will decrypt the file. Specifically, the encryption program should generate a random key, encrypt using the key, read plaintext from a file, and write the key, IV, and ciphertext to separate files. The decryption program should read the key, IV, and ciphertext, and output the plaintext. The following classes should be useful:

You may accomplish the lab tasks however you want to. A suggested set of steps to encrypt are as follows:

  1. Generate an AES key using KeyGenerator. You will first need to use getInstance and init to initialize it.
  2. Create and initialize a Cipher. The parameter you should bass to getInstance for the Cipher is "AES/CBC/PKCS5Padding". This says we are using AES with cipher block chaining, and adding padding according to the PKCS5 standard.
  3. Read in the plaintext and output the ciphertext. For outputting ciphertext, the simplest way is to use a CipherOutputStream.
  4. Write your IV and Key to files. You can use getEncoded to get an easily writable format of the key.

For decrypting, if you used getEncoded to write the key, you will need to use a SecretKeySpec to create the new key based on the written bytes. If you use getIV to write the IV, you can use an instance of IvParameterSpec to help get the IV back into a usable format for the cipher's init method. The rest of decrypting is left as an exercise for you to figure out.

When writing your code, keep in mind that coding style is important. Points will be taken off for unreadable code, including programs with no or minimal comments or programs without proper indentation.