import javax.crypto.spec.SecretKeySpec;
import java.util.Arrays;
public class UsingDES
{
public static void main(String [] args) throws Exception {
byte[] keyBytes = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
byte[] IV = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
SecretKeySpec key = new SecretKeySpec(keyBytes, "DES");
System.out.println("\n\n");
System.out.println("DES ENCRYPTION EXAMPLE");
System.out.println();
// HERE COMES THE PLAINTEXT
//String plaintext = new String("CPSC495 is super. I wish it would be a multi-year course12.");
String plaintext = new String("0123456789abcdefgoooooo11111111");
//String plaintext = new String("CPSC495aFOOOOBAR");
// TURN PLAINTEXT STRING INTO BYTE ARRAY
// AND OUTPUT SOME INFORMATION ABOUT PLAINTEXT
byte[] dataBytes = plaintext.getBytes();
System.out.println("Plaintext \"" + plaintext + "\" in Bytes: " + Arrays.toString(dataBytes));
System.out.println("Length of plain text: " + dataBytes.length);
System.out.println();
// DO ENCRYPTION
// AND OUTPUT SOME INFORMATION ABOUT CIPHERTEXT
System.out.print("Encryption starting...");
byte[] encBytes = DESCTS.encrypt(dataBytes, key, IV);
System.out.println("finished!");
System.out.println("Ciphertext: " + Arrays.toString(encBytes));
System.out.println("Length of cipher text: " + encBytes.length);
System.out.println();
// DO DECRYPTION
// AND OUTPUT SOME INFORMATION ABOUT RECOVERED PLAINTEXT
System.out.print("Decryption starting...");
byte[] decBytes = DESCTS.decrypt(encBytes, key, IV);
System.out.println("finished!");
System.out.println("Recovered plaintext: \"" + new String(decBytes) + "\" in Bytes: " + Arrays.toString(decBytes));
System.out.println("Length of recovered plaintext: " + decBytes.length);
boolean expected = java.util.Arrays.equals(dataBytes, decBytes);
System.out.println("Encryption/Decryption " + (expected ? "SUCCEEDED!" : "FAILED!"));
System.out.println();
}
}