【问题标题】:DES Brute Force (academic)DES蛮力(学术)
【发布时间】:2021-07-24 17:42:00
【问题描述】:

我正在学习计算机安全课程,我们的一项任务是暴力破解具有弱密钥的 DES。

我的代码:

    public static void main(String[] args) throws Exception {
        String temp;
        String current;
        String plaintext;
        
        //Generate key for DES
        String initkey = "00000006";
        byte[] Rawkey = initkey.getBytes();
        DESKeySpec dks =  new DESKeySpec(Rawkey);
        SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
        SecretKey desKey = skf.generateSecret(dks);
        
        //Text Enc & Dec
        Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
        
        //Declare wether to enc or dec
        cipher.init(Cipher.ENCRYPT_MODE,desKey);
        byte []message = "Decrypted Successfully".getBytes();
        byte []messageEnc = cipher.doFinal(message);
        plaintext = new String(message);
        System.out.println("Cipher Text: " + new String(messageEnc) );

        for(int i=0;i<10;i++){
            try{
                temp = padLeftZeros(Integer.toString(i),8);
                System.out.println(temp);
                byte []RawkeyTest = temp.getBytes();
                DESKeySpec dksTest =  new DESKeySpec(RawkeyTest);
                SecretKeyFactory skf2 = SecretKeyFactory.getInstance("DES");
                SecretKey desKeyTest = skf2.generateSecret(dksTest);
                cipher.init(Cipher.DECRYPT_MODE,desKeyTest);
                byte []dec = cipher.doFinal(messageEnc);
                current = new String(dec);
                if(current.equals(plaintext)){
                    System.out.println("Decrypted Text: " + current);
                    System.out.println("");
                    //break;
                }
                
            }catch (BadPaddingException ex){
                System.out.println("Wrong Key.");
                System.out.println("");
            }
            
        }
   
    }

    public static String padLeftZeros(String inputString, int length) {
        if (inputString.length() >= length) {
            return inputString;
        }
        StringBuilder sb = new StringBuilder();
        while (sb.length() < length - inputString.length()) {
            sb.append('0');
        }
        sb.append(inputString);
    
        return sb.toString();
    }
}

输出

Cipher Text: �
B��4#Ǡ�`=Π��H�č:�
00000000
Wrong Key.

00000001
Wrong Key.

00000002
Wrong Key.

00000003
Wrong Key.

00000004
Wrong Key.

00000005
Wrong Key.

00000006
Decrypted Text: Decrypted Successfully

00000007
Decrypted Text: Decrypted Successfully

00000008
Wrong Key.

00000009
Wrong Key.

00000010
Wrong Key.

00000011
Wrong Key.

00000012
Wrong Key.

00000013
Wrong Key.

00000014
Wrong Key.

00000015
Wrong Key.

00000016
Decrypted Text: Decrypted Successfully

00000017
Decrypted Text: Decrypted Successfully

00000018
Wrong Key.

00000019
Wrong Key.

密钥#6 是唯一应该成功解密的密钥。 所以我不明白为什么键:7、16、17 也可以。

任何帮助或建议将不胜感激。 提前致谢。

【问题讨论】:

  • 弱密钥在 DES 中具有特定含义,参见NIST Special Publication 800-67 Revision 2,3.3.2 弱密钥。从您的问题中不清楚您是否在整个关键空间中进行暴力破解,这可能超出了课堂作业的范围。如果没有密文的实际值(例如十六进制),您的结果将无法复制。

标签: java encryption cryptography brute-force des


【解决方案1】:

没问题,其实有一个。

DES 通常使用 64 位密钥,其中每个字节的最后一位 (lsb) 是奇偶校验位,总共有 56 位加密密钥。奇偶校验位对密钥调度没有贡献。这就是我们说 DES 具有 56 位密钥大小的原因。一旦检查了奇偶性,它们就会被丢弃。密钥的每个字节必须有奇校验。该库忽略奇偶校验问题并且不提供异常。

  • 0x0...6 = 0x..01107=0x..0111。如果您删除正确的位,它们是相同的。

  • 0x0..16 = 0x...0001 011017=0x...0001 0111。现在删除每个字节中的最后一位,然后密钥将与0x00000006相同

请注意,上述密钥都不是有效的奇偶校验。如果您想检查奇偶校验或使您的密钥有效,您可以使用来自Alejandro Revilla github的以下 Java 代码

public static void adjustDESParity (byte[] bytes) {
    for (int i = 0; i < bytes.length; i++) {
        int b = bytes[i];
        bytes[i] = (byte)((b & 0xfe) | ((((b >> 1) ^ (b >> 2) ^ (b >> 3) ^ (b >> 4) ^ (b >> 5) ^ (b >> 6) ^ (b >> 7)) ^ 0x01) & 0x01));
    }
}

public static boolean isDESParityAdjusted (byte[] bytes) {
    byte[] correct = (byte[])bytes.clone();
    adjustDESParity(correct);
    return  Arrays.equals(bytes, correct);
}

如果您正在寻找 DES 的弱密钥,那么这些是

0101 0101 0101 0101
1F1F 1F1F 0E0E 0E0E
E0E0 E0E0 F1F1 F1F1
FEFE FEFE FEFE FEFE

【讨论】:

  • 我现在明白为什么了,有什么想法可以解决它并使右侧的位不被忽略吗?
  • “没问题,其实有一个。” - 不知道你在说什么:-)
  • @StephenC both :) 如果你知道这不是问题,但是,这是一个问题,因为没有人使用奇偶校验。嗯,它是为旧时代设计的。如我们所见,库也不检查。编写一个测试并抛出异常但不在乎的东西并不难。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-11
  • 2021-02-22
相关资源
最近更新 更多