【问题标题】:Triple DES Java not working三重 DES Java 不工作
【发布时间】:2026-01-09 08:25:08
【问题描述】:

我正在使用这个程序代码Programs code full,但我不明白为什么我的三重 DES 不能工作。我是如何尝试的?

  1. 我添加了 3 个密钥输入(以及密钥位数组),我将使用它们来加密十六进制并转换它们:

        String keyy1 = new Scanner(System.in).nextLine();
        int keyBitstriple1[] = new int[64];
            for(int i=0 ; i < 14 ; i++) {
            String s = Integer.toBinaryString(Integer.parseInt(keyy1.charAt(i) + "", 16));
                    while(s.length() < 4) {
                        s = "0" + s;
                    }
                    for(int j=0 ; j < 4 ; j++) {
                        keyBitstriple1[(4*i)+j] = Integer.parseInt(s.charAt(j) + "");
                    }
                }    
    
  2. 然后我加密了六进制:

        System.out.println("1st encrypt");
        int outputBits[] = permute(inputBits, keyBitstriple1, false);
        int outputBits2[] = Arrays.copyOf(outputBits, outputBits.length);
        System.out.println("-----------------------------------------------------------");
        System.out.println("2nd enrypt");
        int outputBits3[] = permute(outputBits2, keyBitstriple2, false);
        int outputBits4[] = Arrays.copyOf(outputBits3, outputBits3.length);
        System.out.println("-----------------------------------------------------------");
        System.out.println("3rd enrypt");
        int outputBits5[] = permute(outputBits4, keyBitstriple3, false);
     );
     int outputBits6[] = Arrays.copyOf(outputBits5, outputBits5.length); 
    //a copy of end encrypt
    

加密完全正确,即使我用在线DES加密软件检查结果也是一样的。

  1. 然后我将最终加密保存在 outputBits6 中,并让用户再输入 3 个密钥来解密该加密文本。 同第一步。

     String keyy4 = new Scanner(System.in).nextLine();
     String keyy5 = new Scanner(System.in).nextLine();
     String keyy6 = new Scanner(System.in).nextLine();
    
  2. 然后我使用第一个用户输入的密钥来解密十六进制文本。 (输出位6)

        System.out.println("-----------------------------------------------------------");
        System.out.println("First decrypt");
        int outputBits7[] =  permute(outputBits6, keyBitstriple4, true);
        System.out.println("-----------------------------------------------------------");
        System.out.println("2nd decrypt");
        int outputBits8[] = permute(outputBits7, keyBitstriple5, true);
        System.out.println("-----------------------------------------------------------");
        System.out.println("3rd decrypt");
        int outputBits9[] = permute(outputBits8, keyBitstriple6, true);
    

每次解密都是错误的,我不知道为什么,但加密是正确的。 MY FULL CODE

提前致谢。

【问题讨论】:

  • 三重 DES 不是很安全(168 位密钥的 112 位安全性),不应该用于新工作,当前的加密标准是 AES。
  • @zaph 谢谢,我只是想了解如何使它工作,AES 是我列表中的下一个。 :) 干杯有美好的一天!
  • 1.您缺少的一件事是,通常 3DES 加密是ede:使用第一个密钥加密,使用第二个密钥解密并使用第三个密钥加密。反向解密。 2. 3DES 是一种让 DES 更安全的 hack,ede 是为了升级与 DES 的兼容性。
  • @zaph 谢谢其实这就是问题所在。
  • @zaph 知道为什么在十六进制中它不允许 F 或 f 吗?

标签: java des


【解决方案1】:

缺少的一件事是,一般 3DES 加密是ede:用第一个密钥加密,用第二个密钥解密,用第三个密钥加密。反向ded 用于解密。

【讨论】: