0

I have coded DES in Java using builtin Libraries but I am not getting the right Encryption Result. Please explain me where I am making a mistake

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.xml.bind.DatatypeConverter;

public class MainClass {

    public static void main(String[] args) {

        String l = "0e329232ea6d0d73";

        byte[] a = DatatypeConverter.parseHexBinary(l);

        try{
            DESKeySpec dks = new DESKeySpec(a);
            SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
            SecretKey sk = skf.generateSecret(dks);
        Cipher c = Cipher.getInstance("DES");
        c.init(Cipher.ENCRYPT_MODE, sk);
        String M = "8787878787878787";
        byte[] b = c.doFinal(M.getBytes());

        System.out.println(new String(b));
        c.init(Cipher.DECRYPT_MODE, sk);
        System.out.println(new String(c.doFinal(b)));
        }
        catch(Exception e)

        {
            System.out.println(e.getMessage());
        }   
    }

}

HexaDecimal 16 Digit Key: 0e329232ea6d0d73
Plain Text: 8787878787878787
Encryption : –m^MúÊ'+–m^MúÊ'+©ôËÓ—

Desired Encryption: 0000000000000000

This is what I am saying the answer in the encrypted output in online calculator is 0000, and mine is completely different:

Image

17
  • What do you mean by desired result? Is it desired encrypted message or desired decrypted message? Commented Oct 15, 2016 at 23:24
  • desired encrypted message Commented Oct 15, 2016 at 23:26
  • 5
    The desired result of decryption is 87878787..., not 00000000... and you don't state any basis for your apparent expectation that the result of encryption will be all zeros either, which is most improbable. Unclear what you're asking. Commented Oct 15, 2016 at 23:27
  • 2
    1. Don't use DES, it is no longer considered secure, it has been superseded byAES. 2. The result of encryption is an array of bytes, not characters, if you need to display encrypted data use hexadecimal. 3. How do you expect 0000000000000000 as the encrypted result. 4. Do not use ECB mode, it is insecure, see ECB mode, scroll down to the Penguin. Commented Oct 15, 2016 at 23:33
  • 1
    For the third time, why do you think the result should be all zeros? I repeat that this is most improbable. And are these binary zeroes or ASCII zeros? Commented Oct 15, 2016 at 23:37

3 Answers 3

1

Change

byte[] b = c.doFinal(M.getBytes());

to

byte[] b = c.doFinal(DatatypeConverter.parseHexBinary(M));

Besides, use mode ECB in your code (because you marked ECB in your picture). Like:

Cipher c = Cipher.getInstance("DES/ECB/NoPadding");

The String.getBytes() encodes the String into a sequence of bytes using the platform's default charset, storing the result into a new byte array. In your case which is an array of length 16 containing values 56 55 56 55... ASCII representation of 8787...

What you need is converting the Hex 8787... to binary.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks man for saving my life .. it worked for me . ok i have another question. how would i convert normal string to hexa
@user2747596 And "platform's default charset" is probably not what you want, especially since it can change and if you don't know what it is you can't communicate that the person who has to decrypt your text.
1

The data is: hex 8787878787878787 and needs to be converted to binary. `

3 Comments

Convert the hax characters strig to byte[] data.
did you mean Converting M to hexa binary ?
can you please explain what you are trying to say ?Because i am not getting you on this
0

The input to that online calculator is stated to be hex, which implies a data convertion. You aren't doing any hex conversion in your code: you're just providing base-10 digits in ASCII.

4 Comments

Actually hex-ASCII not base-10 digits and binary is needed.
@zaph It's only 'hex-ASCII', whatever that may be, if the recipient so treats it, which it doesn't. Binary is needed as an input to the encryption, but hex is a representation of binary. Base-10 digits in ASCII are also a representation of binary, but of the wrong binary value in this case.
It turns out that both the data and key are in hex-ascii.
The above answer just solved my issue you were right it was the issue of M.getbytes[] it wasn't in hexa. Thanks for the answers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.