{"id":7,"date":"2008-05-05T08:25:41","date_gmt":"2008-05-05T15:25:41","guid":{"rendered":"http:\/\/cknotes.com\/?p=7"},"modified":"2008-05-09T06:19:20","modified_gmt":"2008-05-09T13:19:20","slug":"matching-des-encryption-in-java-and-vbscript","status":"publish","type":"post","link":"https:\/\/cknotes.com\/matching-des-encryption-in-java-and-vbscript\/","title":{"rendered":"Matching DES Encryption in Java and VBScript"},"content":{"rendered":"<p>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:<\/p>\n<p>The string: <strong>ABC123<\/strong> encrypts to <strong>6DA02B6AE1EA32D8<\/strong>   (as a hexidecimalized string)<br \/>\n<strong><br \/>\nJava bytes-to-hex Encoding Utility Function<\/strong><\/p>\n<pre>\r\n \tpublic static String toHex (byte buf[]) {\r\n \t\t\r\n      StringBuffer strbuf = new StringBuffer(buf.length * 2);\r\n      int i;\r\n\r\n      for (i = 0; i &lt; buf.length; i++) {\r\n       if (((int) buf[i] & 0xff) < 0x10)\r\n\t    strbuf.append(\"0\");\r\n\r\n       strbuf.append(Long.toString((int) buf[i] &#038; 0xff, 16));\r\n      }\r\n\r\n      return strbuf.toString();\r\n     }\r\n<\/pre>\n<p><strong>56-bit DES Encryption Java JCE Example 1<\/strong><\/p>\n<pre>\r\n        String password = \"12345678\";\r\n        byte[] keyBytes = password.getBytes();\r\n\t\t\t\t\r\n        SecretKeySpec skeySpec = new SecretKeySpec(keyBytes, \"DES\");\r\n        Cipher cipher = Cipher.getInstance(\"DES\");\r\n        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);\r\n        \r\n        String original = \"ABC123\";\r\n        byte[] inBytes = original.getBytes();\r\n        byte[] encrypted = cipher.doFinal(inBytes);\r\n     \r\n     \tSystem.out.println(\"Java:\");\r\n        System.out.println(toHex(encrypted));\r\n        \r\n        String dStr = decrypt(\"12345678\",encrypted);\r\n      \tSystem.out.println(dStr);\r\n<\/pre>\n<p><strong>56-bit DES Encryption Java JCE Example 2<\/strong><\/p>\n<pre>\r\n        DESKeySpec ks = new DESKeySpec( keyBytes );\r\n        SecretKeyFactory kf = SecretKeyFactory.getInstance( \"DES\" );\r\n        SecretKey ky = kf.generateSecret( ks );\r\n        Cipher cf = Cipher.getInstance( \"DES\" );\r\n        cf.init( Cipher.ENCRYPT_MODE, ky );\r\n        byte[] encrypted2 = cipher.doFinal(inBytes);\r\n\t \r\n        System.out.println(\"Java(2):\");\r\n        System.out.println(toHex(encrypted2));\r\n<\/pre>\n<p><strong>56-bit DES Encryption Java w\/ Chilkat<\/strong><\/p>\n<pre>\r\n    <a href=\"http:\/\/www.chilkatsoft.com\/refdoc\/javaCkCrypt2Ref.html\">CkCrypt2<\/a> crypt = new CkCrypt2();\r\n    crypt.UnlockComponent(\"30-day Trial\");\r\n    \r\n\tcrypt.put_CryptAlgorithm(\"des\");\r\n\tcrypt.put_CipherMode(\"ecb\");\r\n\t\/\/crypt.put_PaddingScheme(3);\r\n\tcrypt.put_KeyLength(64);\r\n\tcrypt.put_EncodingMode(\"hex\");\r\n\t\/\/crypt.put_Charset(\"windows-1252\");\r\n\t\r\n\tcrypt.SetEncodedKey(\"12345678\",\"ascii\");\r\n\t\r\n\tCkByteData inData = new CkByteData();\r\n\tinData.appendStr(original);\r\n\t    \r\n\tCkString encryptedStr = new CkString();\r\n\tcrypt.EncryptBytesENC(inData,encryptedStr);\r\n\t\r\n\tSystem.out.println(\"Chilkat:\");    \r\n\tSystem.out.println(encryptedStr.getString());\r\n\r\n<\/pre>\n<p><strong>56-bit DES Encryption VBScript w\/ Chilkat<\/strong><\/p>\n<pre>\r\nset crypt = CreateObject(\"<a href=\"http:\/\/www.chilkatsoft.com\/refdoc\/xChilkatCrypt2Ref.html\">Chilkat.Crypt2<\/a>\")\r\n\r\ncrypt.UnlockComponent \"30-day Trial\"\r\n\r\ncrypt.CryptAlgorithm = \"DES\"\r\ncrypt.CipherMode = \"ECB\"\r\ncrypt.KeyLength = 64\r\ncrypt.EncodingMode = \"hex\"\r\ncrypt.SetEncodedKey \"12345678\",\"ascii\"\r\n\r\nMsgBox crypt.EncryptStringENC(\"ABC123\")\r\n<\/pre>\n<p><strong>56-bit DES Decryption in Java JCE<\/strong><\/p>\n<pre>\r\n    public static String decrypt(String password, byte[] encodedData)\r\n      throws Exception\r\n    {\r\n\r\n        byte[] originalData = null;\r\n\r\n        SecretKeySpec key = new SecretKeySpec(password.getBytes (), \"DES\");\r\n        Cipher cipher = Cipher.getInstance(\"DES\");\r\n        cipher.init(Cipher.DECRYPT_MODE, key);\r\n        originalData = cipher.doFinal(encodedData);\r\n\r\n        String s = new String(originalData);\r\n        return s;\r\n    }     \r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>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 = [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[433,11,431,430,12,432],"class_list":["post-7","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-activex","tag-des","tag-encryption","tag-java","tag-jce","tag-vbscript"],"_links":{"self":[{"href":"https:\/\/cknotes.com\/wp-json\/wp\/v2\/posts\/7","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cknotes.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cknotes.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cknotes.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cknotes.com\/wp-json\/wp\/v2\/comments?post=7"}],"version-history":[{"count":0,"href":"https:\/\/cknotes.com\/wp-json\/wp\/v2\/posts\/7\/revisions"}],"wp:attachment":[{"href":"https:\/\/cknotes.com\/wp-json\/wp\/v2\/media?parent=7"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cknotes.com\/wp-json\/wp\/v2\/categories?post=7"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cknotes.com\/wp-json\/wp\/v2\/tags?post=7"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}