Matching DES Encryption in Java and VBScript

This page shows both Java JCE DES encryption examples, as well as examples using the Chilkat Encryption component in both Java and VBScript. Each of these examples produces identical output:

The string: ABC123 encrypts to 6DA02B6AE1EA32D8 (as a hexidecimalized string)

Java bytes-to-hex Encoding Utility Function

 	public static String toHex (byte buf[]) {
 		
      StringBuffer strbuf = new StringBuffer(buf.length * 2);
      int i;

      for (i = 0; i < buf.length; i++) {
       if (((int) buf[i] & 0xff) < 0x10)
	    strbuf.append("0");

       strbuf.append(Long.toString((int) buf[i] & 0xff, 16));
      }

      return strbuf.toString();
     }

56-bit DES Encryption Java JCE Example 1

        String password = "12345678";
        byte[] keyBytes = password.getBytes();
				
        SecretKeySpec skeySpec = new SecretKeySpec(keyBytes, "DES");
        Cipher cipher = Cipher.getInstance("DES");
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
        
        String original = "ABC123";
        byte[] inBytes = original.getBytes();
        byte[] encrypted = cipher.doFinal(inBytes);
     
     	System.out.println("Java:");
        System.out.println(toHex(encrypted));
        
        String dStr = decrypt("12345678",encrypted);
      	System.out.println(dStr);

56-bit DES Encryption Java JCE Example 2

        DESKeySpec ks = new DESKeySpec( keyBytes );
        SecretKeyFactory kf = SecretKeyFactory.getInstance( "DES" );
        SecretKey ky = kf.generateSecret( ks );
        Cipher cf = Cipher.getInstance( "DES" );
        cf.init( Cipher.ENCRYPT_MODE, ky );
        byte[] encrypted2 = cipher.doFinal(inBytes);
	 
        System.out.println("Java(2):");
        System.out.println(toHex(encrypted2));

56-bit DES Encryption Java w/ Chilkat

    CkCrypt2 crypt = new CkCrypt2();
    crypt.UnlockComponent("30-day Trial");
    
	crypt.put_CryptAlgorithm("des");
	crypt.put_CipherMode("ecb");
	//crypt.put_PaddingScheme(3);
	crypt.put_KeyLength(64);
	crypt.put_EncodingMode("hex");
	//crypt.put_Charset("windows-1252");
	
	crypt.SetEncodedKey("12345678","ascii");
	
	CkByteData inData = new CkByteData();
	inData.appendStr(original);
	    
	CkString encryptedStr = new CkString();
	crypt.EncryptBytesENC(inData,encryptedStr);
	
	System.out.println("Chilkat:");    
	System.out.println(encryptedStr.getString());

56-bit DES Encryption VBScript w/ Chilkat

set crypt = CreateObject("Chilkat.Crypt2")

crypt.UnlockComponent "30-day Trial"

crypt.CryptAlgorithm = "DES"
crypt.CipherMode = "ECB"
crypt.KeyLength = 64
crypt.EncodingMode = "hex"
crypt.SetEncodedKey "12345678","ascii"

MsgBox crypt.EncryptStringENC("ABC123")

56-bit DES Decryption in Java JCE

    public static String decrypt(String password, byte[] encodedData)
      throws Exception
    {

        byte[] originalData = null;

        SecretKeySpec key = new SecretKeySpec(password.getBytes (), "DES");
        Cipher cipher = Cipher.getInstance("DES");
        cipher.init(Cipher.DECRYPT_MODE, key);
        originalData = cipher.doFinal(encodedData);

        String s = new String(originalData);
        return s;
    }