CSci 4554 Lab 2: javax.crypto class

Due Monday, Sept 30 at 11:59pm (by e-mail)

25 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.

Some helpful resources:

  1. Java Cryptography Architecture Reference Guide (describes the entire crypto package)
  2. Cipher class that implements most of teh 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");

        // initialise 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);


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

        // re-initialise 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, as needed.
  2. For each of the following ciphers and modes, please perform the encryption of a text of your choice. Post the encryption (starting with IV as the first block when needed) on the wiki 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.

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

    1. All your program code (well-documented) and instructions for running it.
    2. Your plaintexts and the IV (even though the IV is posted).
    3. All the decryptions.

    CSci 4554 course web site.

    The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.