cbanor / X-Website

Multilingual translation and error feedback and fixes
13 stars 0 forks source link

Code required for Triple DES Encryption #14

Open raihan-ink opened 1 year ago

raihan-ink commented 1 year ago

Hello there, I am using one of the APIs for my client and they are using Triple DES encryption. I have written the corresponding code for it as well and it gives an output. However, the output is different, and your code perfectly gives the output which is accepted by the API. I would request you to please share the code for TripleDES:

  1. Mode - ECB
  2. Padding - PKCS7
  3. Key Length - Twice
  4. -- I have my own secret key --

Can you please share the code?

cbanor commented 1 year ago

Perhaps you are using hex's key, but you mistakenly mistook it for a string key There is currently no time to organize code for open source.

Message ID: @.***>

raihan-ink commented 1 year ago

I am first computing the MD5 hash of my key (which is in string) and then converting it into byte array. This is how my code looks like. I have generated Md5 hash using my cloud software.

byte[] digestOfPassword = "generated-MD5-hash".getBytes("utf-8");

   final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);

    int k = 16;
    for (int j = 0; j < 8;) {
    keyBytes[k++] = keyBytes[j++];
    } 

    SecretKeySpec secretKeySpec = new SecretKeySpec(digestOfPassword, "DESede");

    String secretMessage = "message-to-be-encrypted";

    Cipher encryptCipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
    encryptCipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);

    byte[] secretMessagesBytes = secretMessage.getBytes("utf-8");
    byte[] encryptedMessageBytes = encryptCipher.doFinal(secretMessagesBytes);

    String encodedMessage = Base64.getEncoder().encodeToString(encryptedMessageBytes);

    println encodedMessage

Can you atleast tell me what I am doing different than you? Maybe some mode change, padding change or any other?

Regards, Raihan