Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code required for Triple DES Encryption #14

Open
raihan-ink opened this issue Jul 31, 2023 · 2 comments
Open

Code required for Triple DES Encryption #14

raihan-ink opened this issue Jul 31, 2023 · 2 comments

Comments

@raihan-ink
Copy link

raihan-ink commented Jul 31, 2023

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
Copy link
Owner

cbanor commented Aug 1, 2023 via email

@raihan-ink
Copy link
Author

raihan-ink commented Aug 1, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants