Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1323
  • Last Modified:

Java DES Encryption to VB6 DES Decryption

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
StormRider01
Asked:
StormRider01
1 Solution
 
functionpointerCommented:
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
 
StormRider01Author Commented:
Thanks, just the info I needed.
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now