Search code examples
javades

Triple DES Java not working


I'm using this programs code Programs code full and i can't figure it out why wouldn't my Triple DES work. How and what did i try?

  1. I added 3key inputs (as well arrays for keybits) that i would use to encrypt the hex and converted them:

        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. Then i encrypted the hexa:

        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
    

Encryption is completely correct, even when i check with online DES encryption software results was the same.

  1. Then i saved the end encryption in outputBits6 and made a user enter 3 more keys to what decrypt that encrypted text. The same as first step.

     String keyy4 = new Scanner(System.in).nextLine();
     String keyy5 = new Scanner(System.in).nextLine();
     String keyy6 = new Scanner(System.in).nextLine();
    
  2. Then i took first user entered key to decrypt the hexa text. (outputbits6)

        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);
    

Every time decryption is wrong and i can't figure it out why but encryption is correct. MY FULL CODE

Thanks in advance.


Solution

  • One thing that is missing is that generally 3DES encryption is ede: encrypt with the 1st key, decrypt with the 2nd key and encrypt with the 3rd key. The reverse ded for decryption.