CSci 4554 Lab 1: javax.crypto class

Due Wednesday, February 22 at 11:59pm (by e-mail)

30 points total

Overview and background

This lab is done in groups of 2.

The purpose of the lab is to introduce you to the implementations of common block ciphers and their modes of operation in the Java crypto library. You will be using AES, DES, and DESede (Triple DES that performs encrypt-decrypt-encrypt sequence with three DES keys).

Some helpful resources:

  1. Java Cryptography Architecture Reference Guide (describes the entire crypto package)
  2. Cipher class that implements most of the functionality
  3. Naming conventions for various algorithms
  4. An Example of using the crypto package
  5. DatatypeConverter class to convert between byte arrays and other types.

An example


import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.xml.bind.DatatypeConverter;

public class EncryptionExample {

    public static void main(String[] args) throws Exception {

        // create a key generator for the AES cipher
        KeyGenerator keygenerator = KeyGenerator.getInstance("AES");

        // create a key
        SecretKey secretkey = keygenerator.generateKey();

        // create a cipher based upon AES
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

        // initialize cipher with the secret key
        cipher.init(Cipher.ENCRYPT_MODE, secretkey);

        // Note: IV is generated automatically, stored in the cipher

        // ENCRYPTION:

        // get the text to encrypt
        String inputText = "This is a string that tests whether the encryption is working";

        // encrypt message
        byte[] encrypted = cipher.doFinal(inputText.getBytes());

        // get the IV:
        byte [] iv = cipher.getIV();
        String ivAsText =  DatatypeConverter.printBase64Binary(iv);

        System.out.println("The IV is " + ivAsText);

        //formatting bytes as hexadecimal
        String encryptedAsText = DatatypeConverter.printBase64Binary(encrypted);

        System.out.println(encryptedAsText);

        // DECRYPTION:
        // re-convert iv
        byte [] ivAsBytes = DatatypeConverter.parseBase64Binary(ivAsText);

        // re-initialize the cipher to be in decrypt mode
        cipher.init(Cipher.DECRYPT_MODE, secretkey,  new IvParameterSpec(ivAsBytes));

        byte [] encryptedAsBytes = DatatypeConverter.parseBase64Binary(encryptedAsText);

        // decrypt message
        byte[] decrypted = cipher.doFinal(encryptedAsBytes);

        // and display the results
        System.out.println("Decryted text: " + new String(decrypted));

    }
}

Tasks for the lab:

  1. Every group needs to find a partner group and generate a common secret key for AES, DES, and DESede.
  2. For each of the following ciphers and modes, please perform the encryption of a text of your choice. Create a shared google doc with your partner team (please share it with me as well) and store your encrypted texts and your keys there. Include all the necessary information (the cipher, the mode, the IV). The ciphers/modes you need to use are:
    1. AES in OFB and CTR modes.
    2. DES in CBC mode.
    3. DESede in CBC, OFB, and CTR modes. Note the DESedeKeySpec class that generates a DESede key.
    Use padding when needed.

What to submit (by e-mail to me, CC your partner)

  1. All your program code (well-documented) and instructions for running it.
  2. All the plaintext messages that you sent to the other team (carefully labeled by which cipher, mode, etc. you used)
  3. A link to your shared document.
  4. All the generated IV (although they are included in the encrypted text as well)
  5. All the decryptions, carefully labeled.

CSci 4554 course web site.