DES暴力破解(学术)

6

我正在学习计算机安全课程,其中一项任务是对具有弱密钥的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节“弱密钥”。从您的问题中不清楚您是否正在对整个密钥空间进行暴力破解,这可能超出了课程作业的范围。如果没有实际的密文值(例如十六进制),则无法复制您的结果。 - user1155120
1个回答

5

其实并没有问题,但其实有一个问题。

DES通常使用64位密钥,其中每个字节的最后一位(最低有效位)是奇偶校验位,总共有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

我现在明白了,你有什么想法来修复它并使右侧的位不被忽略吗? - Anas Nagaty
“其实没有问题,但是似乎有一个。” - Stephen C
1
@StephenC 两者都可以 :) 如果你知道它,那就不是问题,但是现在没人使用奇偶校验了。嗯,它是为旧时代设计的。正如我们所看到的,库也不检查。编写一个测试并抛出异常的程序并不难,但是没有人关心它。 - kelalaka

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接