?
Solved

Java DES Encryption to VB6 DES Decryption

Posted on 2003-03-05
2
Medium Priority
?
1,274 Views
Last Modified: 2007-12-19
I need to be able to decrypt data in VB6 that was encrypted by this code chunk in Java 1.4.0

The issue seems to be that I need the HEX value of the key used by Java, and I can't figure out how to extract it.

JAVA CODE:
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;

public class DES
{
    Cipher ecipher;
    Cipher dcipher;
   
    public DES(String KEY)
    {
        try
        {
            SecretKeyFactory keyGen = SecretKeyFactory.getInstance("DES");
            SecretKey key = keyGen.generateSecret(new DESKeySpec(KEY.getBytes()));
            ecipher = Cipher.getInstance("DES");
            dcipher = Cipher.getInstance("DES");
            ecipher.init(Cipher.ENCRYPT_MODE, key);
            dcipher.init(Cipher.DECRYPT_MODE, key);
                 System.out.println(key.getkey());
        }
        catch (Exception e)
        {
            e.printStackTrace();            
        }
    }
   
   
    public String encrypt(String str)
    {
        try
        {
            byte[] utf8 = str.getBytes("UTF8");
            byte[] enc = ecipher.doFinal(utf8);
            return new sun.misc.BASE64Encoder().encode(enc);
        }
        catch (Exception e)
        {
            e.printStackTrace();            
            return null;
        }
    }
   
    public String decrypt(String str)
    {
        try
        {
            byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str);
            byte[] utf8 = dcipher.doFinal(dec);
            return new String(utf8, "UTF8");
        }
        catch (Exception e)
        {
            e.printStackTrace();            
            return null;
        }
    }
}
0
Comment
Question by:StormRider01
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 2

Accepted Solution

by:
functionpointer earned 300 total points
ID: 8077526
Since the way you are using the DESKeySpec, only the first 8 bytes of the String are going to become key material. If you want the key in ASCIIHEX, try these modifications:

package untitled1;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2002</p>
 * <p>Company: </p>
 * @author unascribed
 * @version 1.0
 */
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;

public class DES
{
  private final String ASCIIHEX = "0123456789ABCDEF";
  private Cipher ecipher;
  private Cipher dcipher;

  public DES(String KEY)
  {
    try
    {
      byte[] keyMaterial = KEY.getBytes();
      SecretKeyFactory keyGen = SecretKeyFactory.getInstance("DES");
      SecretKey key = keyGen.generateSecret(new DESKeySpec( keyMaterial ));
      ecipher = Cipher.getInstance("DES");
      dcipher = Cipher.getInstance("DES");
      ecipher.init(Cipher.ENCRYPT_MODE, key);
      dcipher.init(Cipher.DECRYPT_MODE, key);

      String asciiHexKey = "";
      for ( int i = 0; i < 8; i++ ) // since it only uses first 8
      {
        asciiHexKey += toAsciiHex( keyMaterial[i] );
      }

      System.out.println( asciiHexKey );
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }

  private String toAsciiHex( byte b )
  {
    String s = "";
    int i;
    if( b >= 0 ) i = (int)b;
    else i = (int)(Byte.MAX_VALUE) + Math.abs((int)(b - Byte.MIN_VALUE)) + 1;
    s += ASCIIHEX.charAt( i / 16 );
    s += ASCIIHEX.charAt( i % 16 );
    return s;
  }


  public String encrypt(String str)
  {
    try
    {
      byte[] utf8 = str.getBytes("UTF8");
      byte[] enc = ecipher.doFinal(utf8);
      return new sun.misc.BASE64Encoder().encode(enc);
    }
    catch (Exception e)
    {
      e.printStackTrace();
      return null;
    }
  }

  public String decrypt(String str)
  {
    try
    {
      byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str);
      byte[] utf8 = dcipher.doFinal(dec);
      return new String(utf8, "UTF8");
    }
    catch (Exception e)
    {
      e.printStackTrace();
      return null;
    }
  }
}

Hope it helps.
0
 

Author Comment

by:StormRider01
ID: 8095105
Thanks, just the info I needed.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn different types of Android Layout and some basics of an Android App.
The viewer will learn how to implement Singleton Design Pattern in Java.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month10 days, 8 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question