Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use 3DES encryption/decryption in Java?

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;     } } 
like image 960
Kyle Boon Avatar asked Aug 21 '08 15:08

Kyle Boon


People also ask

Can 3DES be decrypted?

Without the key, the 112-bit 3DES cannot be decrypted. Unless you have a means to steal the shared key, even the most powerful brute-force engines currently available will take longer than the planet's expected lifespan to find the key.


2 Answers

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");     } } 
like image 191
Boris Terzic Avatar answered Sep 19 '22 11:09

Boris Terzic


Here is a solution using the javax.crypto library and the apache commons codec library for encoding and decoding in Base64:

import java.security.spec.KeySpec; import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESedeKeySpec; import org.apache.commons.codec.binary.Base64;  public class TrippleDes {      private static final String UNICODE_FORMAT = "UTF8";     public static final String DESEDE_ENCRYPTION_SCHEME = "DESede";     private KeySpec ks;     private SecretKeyFactory skf;     private Cipher cipher;     byte[] arrayBytes;     private String myEncryptionKey;     private String myEncryptionScheme;     SecretKey key;      public TrippleDes() throws Exception {         myEncryptionKey = "ThisIsSpartaThisIsSparta";         myEncryptionScheme = DESEDE_ENCRYPTION_SCHEME;         arrayBytes = myEncryptionKey.getBytes(UNICODE_FORMAT);         ks = new DESedeKeySpec(arrayBytes);         skf = SecretKeyFactory.getInstance(myEncryptionScheme);         cipher = Cipher.getInstance(myEncryptionScheme);         key = skf.generateSecret(ks);     }       public String encrypt(String unencryptedString) {         String encryptedString = null;         try {             cipher.init(Cipher.ENCRYPT_MODE, key);             byte[] plainText = unencryptedString.getBytes(UNICODE_FORMAT);             byte[] encryptedText = cipher.doFinal(plainText);             encryptedString = new String(Base64.encodeBase64(encryptedText));         } catch (Exception e) {             e.printStackTrace();         }         return encryptedString;     }       public String decrypt(String encryptedString) {         String decryptedText=null;         try {             cipher.init(Cipher.DECRYPT_MODE, key);             byte[] encryptedText = Base64.decodeBase64(encryptedString);             byte[] plainText = cipher.doFinal(encryptedText);             decryptedText= new String(plainText);         } catch (Exception e) {             e.printStackTrace();         }         return decryptedText;     }       public static void main(String args []) throws Exception     {         TrippleDes td= new TrippleDes();          String target="imparator";         String encrypted=td.encrypt(target);         String decrypted=td.decrypt(encrypted);          System.out.println("String To Encrypt: "+ target);         System.out.println("Encrypted String:" + encrypted);         System.out.println("Decrypted String:" + decrypted);      }  } 

Running the above program results with the following output:

String To Encrypt: imparator Encrypted String:FdBNaYWfjpWN9eYghMpbRA== Decrypted String:imparator 
like image 26
oneiros Avatar answered Sep 21 '22 11:09

oneiros