Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 34103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T14:01:33+00:00 2026-05-10T14:01:33+00:00

Every method I write to encode a string in Java using 3DES can’t be

  • 0

Every method I write to encode a string in Java using 3DES can’t be decrypted back to the original string. Does anyone have a simple code snippet that can just encode and then decode the string back to the original string?

I know I’m making a very silly mistake somewhere in this code. Here’s what I’ve been working with so far:

** note, I am not returning the BASE64 text from the encrypt method, and I am not base64 un-encoding in the decrypt method because I was trying to see if I was making a mistake in the BASE64 part of the puzzle.

public class TripleDESTest {      public static void main(String[] args) {          String text = 'kyle boon';          byte[] codedtext = new TripleDESTest().encrypt(text);         String decodedtext  = new TripleDESTest().decrypt(codedtext);          System.out.println(codedtext);         System.out.println(decodedtext);     }      public byte[] encrypt(String message) {         try {             final MessageDigest md = MessageDigest.getInstance('md5');             final byte[] digestOfPassword = md.digest('HG58YZ3CR9'.getBytes('utf-8'));             final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);             for (int j = 0,  k = 16; j < 8;)             {                 keyBytes[k++] = keyBytes[j++];             }              final SecretKey key = new SecretKeySpec(keyBytes, 'DESede');             final IvParameterSpec iv = new IvParameterSpec(new byte[8]);             final Cipher cipher = Cipher.getInstance('DESede/CBC/PKCS5Padding');             cipher.init(Cipher.ENCRYPT_MODE, key, iv);              final byte[] plainTextBytes = message.getBytes('utf-8');             final byte[] cipherText = cipher.doFinal(plainTextBytes);             final String encodedCipherText = new sun.misc.BASE64Encoder().encode(cipherText);              return cipherText;             }         catch (java.security.InvalidAlgorithmParameterException e) { System.out.println('Invalid Algorithm'); }         catch (javax.crypto.NoSuchPaddingException e) { System.out.println('No Such Padding'); }         catch (java.security.NoSuchAlgorithmException e) { System.out.println('No Such Algorithm'); }         catch (java.security.InvalidKeyException e) { System.out.println('Invalid Key'); }         catch (BadPaddingException e) { System.out.println('Invalid Key');}         catch (IllegalBlockSizeException e) { System.out.println('Invalid Key');}         catch (UnsupportedEncodingException e) { System.out.println('Invalid Key');}          return null;     }      public String decrypt(byte[] message) {         try         {             final MessageDigest md = MessageDigest.getInstance('md5');             final byte[] digestOfPassword = md.digest('HG58YZ3CR9'.getBytes('utf-8'));             final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);             for (int j = 0,  k = 16; j < 8;)             {                 keyBytes[k++] = keyBytes[j++];             }              final SecretKey key = new SecretKeySpec(keyBytes, 'DESede');             final IvParameterSpec iv = new IvParameterSpec(new byte[8]);             final Cipher decipher = Cipher.getInstance('DESede/CBC/PKCS5Padding');             decipher.init(Cipher.DECRYPT_MODE, key, iv);              //final byte[] encData = new sun.misc.BASE64Decoder().decodeBuffer(message);             final byte[] plainText = decipher.doFinal(message);              return plainText.toString();                     }         catch (java.security.InvalidAlgorithmParameterException e) { System.out.println('Invalid Algorithm'); }         catch (javax.crypto.NoSuchPaddingException e) { System.out.println('No Such Padding'); }         catch (java.security.NoSuchAlgorithmException e) { System.out.println('No Such Algorithm'); }         catch (java.security.InvalidKeyException e) { System.out.println('Invalid Key'); }         catch (BadPaddingException e) { System.out.println('Invalid Key');}         catch (IllegalBlockSizeException e) { System.out.println('Invalid Key');}         catch (UnsupportedEncodingException e) { System.out.println('Invalid Key');}              catch (IOException e) {             // TODO Auto-generated catch block             e.printStackTrace();         }          return null;     } } 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T14:01:33+00:00Added an answer on May 10, 2026 at 2:01 pm

    Your code was fine except for the Base 64 encoding bit (which you mentioned was a test), the reason the output may not have made sense is that you were displaying a raw byte array (doing toString() on a byte array returns its internal Java reference, not the String representation of the contents). Here’s a version that’s just a teeny bit cleaned up and which prints ‘kyle boon’ as the decoded string:

    import java.security.MessageDigest; import java.util.Arrays;  import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec;  public class TripleDESTest {      public static void main(String[] args) throws Exception {          String text = 'kyle boon';          byte[] codedtext = new TripleDESTest().encrypt(text);         String decodedtext = new TripleDESTest().decrypt(codedtext);          System.out.println(codedtext); // this is a byte array, you'll just see a reference to an array         System.out.println(decodedtext); // This correctly shows 'kyle boon'     }      public byte[] encrypt(String message) throws Exception {         final MessageDigest md = MessageDigest.getInstance('md5');         final byte[] digestOfPassword = md.digest('HG58YZ3CR9'                 .getBytes('utf-8'));         final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);         for (int j = 0, k = 16; j < 8;) {             keyBytes[k++] = keyBytes[j++];         }          final SecretKey key = new SecretKeySpec(keyBytes, 'DESede');         final IvParameterSpec iv = new IvParameterSpec(new byte[8]);         final Cipher cipher = Cipher.getInstance('DESede/CBC/PKCS5Padding');         cipher.init(Cipher.ENCRYPT_MODE, key, iv);          final byte[] plainTextBytes = message.getBytes('utf-8');         final byte[] cipherText = cipher.doFinal(plainTextBytes);         // final String encodedCipherText = new sun.misc.BASE64Encoder()         // .encode(cipherText);          return cipherText;     }      public String decrypt(byte[] message) throws Exception {         final MessageDigest md = MessageDigest.getInstance('md5');         final byte[] digestOfPassword = md.digest('HG58YZ3CR9'                 .getBytes('utf-8'));         final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);         for (int j = 0, k = 16; j < 8;) {             keyBytes[k++] = keyBytes[j++];         }          final SecretKey key = new SecretKeySpec(keyBytes, 'DESede');         final IvParameterSpec iv = new IvParameterSpec(new byte[8]);         final Cipher decipher = Cipher.getInstance('DESede/CBC/PKCS5Padding');         decipher.init(Cipher.DECRYPT_MODE, key, iv);          // final byte[] encData = new         // sun.misc.BASE64Decoder().decodeBuffer(message);         final byte[] plainText = decipher.doFinal(message);          return new String(plainText, 'UTF-8');     } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 74k
  • Answers 74k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Make a third table called CarOwners with a field for… May 11, 2026 at 2:26 pm
  • added an answer The obvious answer is no. BItrot does not have any… May 11, 2026 at 2:26 pm
  • added an answer You don't need to pre-compile if you don't want to.… May 11, 2026 at 2:26 pm

Related Questions

Every method I write to encode a string in Java using 3DES can't be
Recently our site has been deluged with the resurgence of the Asprox botnet SQL
Good morning, afternoon, evening or night (depending on your timezone). This is just a
I am using the getResponseBody() method of the org.apache.commons.httpclient.methods.PostMethod class. However, I am always
Every time I need to do something N times inside an algorithm using C#

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.