Search code examples
javaencryptionbrute-forcedestripledes

How would I implement brute-force that would attack TripleDES using Java?


I have been working on my science project when I wanted to perform an experiment where I would test how secure TripleDES is by brute-forcing it. TripleDES is composed of three keys, each 56-bits (plus 8 parity-bits), where each of the keys can be two modes, decryption or encryption. Thus, there are eight possible combinations (As Listed Here) :

enter image description here

Using this principle, how would I design a Java program which would brute-force all possible keys for TripleDES and possibly find the original plain-text?

Here is what I have tried for single DES:

import java.security.spec.AlgorithmParameterSpec;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.util.BitSet;

public class DES64 {

    public static byte[] generateKey(byte [] encoded_key2, int N_BYTE) {

        byte[] encoded_key = new byte[] { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 };
        for (int i = 0; i < N_BYTE; i++) {
            encoded_key[8 - N_BYTE + i] = encoded_key2[i];
        }

        return encoded_key;

    }

    public static byte[] des_attack(String plaintext, byte[] encoded_key2, int N_BYTE) throws Exception {

        byte[] encoded_key = generateKey(encoded_key2, N_BYTE);

        SecretKey key = new SecretKeySpec(encoded_key, "DES");  

        byte[] initVector = new byte[] { 0x10, 0x10, 0x01, 0x04, 0x01, 0x01, 0x01, 0x02 };
        AlgorithmParameterSpec algParamSpec = new IvParameterSpec(initVector);

        Cipher m_encrypter = Cipher.getInstance("DES/CBC/PKCS5Padding");
        m_encrypter.init(Cipher.ENCRYPT_MODE, key, algParamSpec);

        Cipher m_decrypter = Cipher.getInstance("DES/CBC/PKCS5Padding");

        byte[] clearText = plaintext.getBytes();

        byte[] encryptedText = m_encrypter.doFinal(clearText);

        int guess = 0;
        int N_COMB = (int) Math.pow(2.0, (N_BYTE) * 7);
        System.out.println("All possible combiantions : " + N_COMB);

        BitSet encoding_attack_key = new BitSet(8 * N_BYTE);
        encoding_attack_key.clear();
        byte[] mykey = new byte[] { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 };

        for (int i = 0; i < N_COMB; i++) {

            String tmp = Integer.toBinaryString(i);

            int z = 0;
            int j = tmp.length() - 1;
            while (j >= 0) {

                if (z != 63 && z != 55 && z != 47 && z != 39 && z != 31 && z != 23 && z != 15 && z != 7) {
                    if (tmp.charAt(j) == '1') {
                        encoding_attack_key.set(z);

                    }
                    j--;
                    z++;
                } else {
                    z++;
                }

            }

            for (int k = 0; k < N_BYTE; k++) {

                if (encoding_attack_key.get(k * 8, (k * 8) + 7).cardinality() % 2 != 0) {

                    encoding_attack_key.set((k * 8) + 7);

                }
            }

            if (guess % 1000000 == 0) {
                System.out.println("Guess : " + guess + " out of : " + N_COMB + " ------- "
                        + 100 * (guess + 0.0) / N_COMB + "% completed");

            }
            guess++;

            int len = encoding_attack_key.toByteArray().length;
            mykey[0] = len >= 8 ? encoding_attack_key.toByteArray()[7] : 0x0;
            mykey[1] = len >= 7 ? encoding_attack_key.toByteArray()[6] : 0x0;
            mykey[2] = len >= 6 ? encoding_attack_key.toByteArray()[5] : 0x0;
            mykey[3] = len >= 5 ? encoding_attack_key.toByteArray()[4] : 0x0;
            mykey[4] = len >= 4 ? encoding_attack_key.toByteArray()[3] : 0x0;
            mykey[5] = len >= 3 ? encoding_attack_key.toByteArray()[2] : 0x0;
            mykey[6] = len >= 2 ? encoding_attack_key.toByteArray()[1] : 0x0;
            mykey[7] = len >= 1 ? encoding_attack_key.toByteArray()[0] : 0x0;

            SecretKey key_g = new SecretKeySpec(mykey, "DES");
            m_decrypter.init(Cipher.DECRYPT_MODE, key_g, algParamSpec);
            try {
                byte[] text_g = m_decrypter.doFinal(encryptedText);
                String plain_text = new String(text_g);

                if (plain_text.equals(new String(clearText))) {

                    return mykey;

                }

            } catch (BadPaddingException ee) {

            }

            encoding_attack_key.clear();

        }
        return null;
    }

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

        String plaintext = "example";
        int nbyte = 4;
        byte[] b = new byte[] { 0x6, 0x7, 0x35, 0x09 };

        long startTime = System.nanoTime();

        byte[] ris = des_attack(plaintext, b, b.length);
        long estimatedTime = (System.nanoTime() - startTime);

        String kris = "";
        for (int i = 0; i < ris.length; i++)
            kris += " " + ris[i] + " ";
        System.out.println("PLAINTEXT : " + plaintext + "      ESTIMATED TIME: " + estimatedTime
                + "     NFREE_BYTE_KEY: " + nbyte + "    KEY: " + kris + "\n");

    }

}


Solution

  • Triple-DES or actually TDEA is a standard. It is just DES-EDE by the way, not any of the other. Of course the decryption is performed using the DED order. Triple-DES has a two-key and three-key mode (disregarding the one that simply reverts to DES for backwards compatibility).

    The two key Triple-DES mode (with a so called DES ABA key as the A key gets reused for the final DES cipher) has a strength of about 80 bits in the worst case. This is somewhat close to being brute forced, being only 2^16 times harder to brute force than the Shattered attack performed by using Google's infrastructure. Not something you'd do with Java and a PC in itself.

    The strength of three key Triple-DES with DES ABC keys is about 2^112, there is no way to brute force your way out of that. So the simple answer is: "you don't" with Java or without.