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:
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));
}
}