我将如何实现使用Java攻击TripleDES的蛮力?

问题描述 投票:0回答:1

我一直在从事我的科学项目,当时我想进行一个实验,通过蛮力测试TripleDES的安全性。 TripleDES由三个密钥组成,每个密钥为56位(加上8个奇偶校验位),其中每个密钥可以是解密或加密两种模式。因此,有八种可能的组合(如此处所列):

enter image description here

使用此原理,我将如何设计一个Java程序,该程序将暴力破解TripleDES的所有可能键并可能找到原始的纯文本?

这是我尝试使用的单个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");

    }

}

java encryption brute-force des tripledes
1个回答
1
投票

Triple-DES or actually TDEA is a standard。顺便说一下,它只是DES-EDE,而不是其他任何一种。当然,decryption是使用DED顺序执行的。 Triple-DES具有两键和三键模式(不考虑为了向后兼容而简单地还原为DES的模式)。

两密钥三重DES模式(在最终的DES密码重用A密钥时使用了所谓的DES ABA密钥)在最坏的情况下具有约80位的强度。这有点接近被强行使用,比使用Google的基础架构所进行的“破碎”攻击硬2到16倍。您不能使用Java和PC本身来做任何事情。

具有DES ABC密钥的三键Triple-DES的

[大约为2 ^ 112,没有办法强行解决。因此,简单的答案是:无论是否使用Java,“您都不会”。

© www.soinside.com 2019 - 2024. All rights reserved.