DES Encryption using Ciphers in JAVA

In this article, we will make a simple Java application that encrypts and decrypts strings using the DES algorithm.

Let’s get started by making a java class

public class DES{
}

The first step is to generate the SecretKey using a static method:

public static SecretKey generateKey() throws Exception{
    return KeyGenerator.getInstance("DES").generateKey();
}

Once we have the generator setup, let’s go and add the constructors. I’ve chosen a no-param constructor that uses the generateKey() method and a constructor that takes as a parameter the key.

private final SecretKey key;

public DES() throws Exception{
    this.key = generateKey();
}

public DES(SecretKey key) throws Exception{
    this.key = key;
}

Now that we have our constructors, we can dive into creating and setting up the ciphers. First thing to do is to create the instance variables and then a method that we’ll call initCiphers() and then update the constructors


private Cipher encCipher;
private Cipher decCipher;

public DES() throws Exception{
this.key = generateKey();
initCiphers();
}

public DES(SecretKey key) throws Exception{
this.key = key;
initCiphers();
}

private void initCiphers() throws Exception{
    encCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
    decCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
    encCipher.init(Cipher.ENCRYPT_MODE,key);
    decCipher.init(Cipher.DECRYPT_MODE,key, new IvParameterSpec(encCipher.getIV()));
}

Now that we have initialized our key, we can add the two encrypt and decrypt methods

public byte[] encrypt(String message) throws Exception{
    return encCipher.doFinal(message.getBytes());
}

public String decryt(byte[] messsage) throws Exception{
    return new String(decCipher.doFinal(messsage));
}

Up to now, the DEC class should look like this

While initializing the ciphers, we had to specify the IV in the decryption cipher. This can be bypassed if we use the ECB instead of the CBC block mode, the change can be done only in the initCiphers() method like such:

private void initCiphers() throws Exception{
    encCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
    decCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
    encCipher.init(Cipher.ENCRYPT_MODE,key);
    decCipher.init(Cipher.DECRYPT_MODE,key);
}

We can run our tests in any main method, We’ll need to add some helper methods to encode the message after encryption and decode it before decrypting it:

public static String encode(byte[] data) {
    return Base64.getEncoder().encodeToString(data);
}

public static byte[] decoder(String data) {
    return Base64.getDecoder().decode(data);
}

Now we can set up our main method

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

    SecretKey key = DES.generateKey();
    System.out.print("Encrypt/Decrypt Key: ");
    System.out.println(encode(key.getEncoded()));
    System.out.println();

    String message = "The X Coders";

    DES des = new DES(key);
    String encryptedMessage = encode(des.encrypt(message));
    System.out.println("Encrypted Message: "+encryptedMessage);
    System.out.println("Decrypted Message: "+des.decryt(decoder(encryptedMessage)));

}

And here’s the result:

Encrypt/Decrypt Key: 5UmDdorpm20=

Encrypted Message: cFPR8N1IgExeg4k4a2vURA==
Decrypted Message: The X Coders

And the final code can be found in my gist:

If you want to watch the video here it isss:

Thank you for taking the time to read! If you want to suggest what I would do next, feel free to comment on the video!

Here are some useful links: