![]() |
Chilkat HOME Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi DLL Go Java Node.js Objective-C PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
Match Java JCE AES CBC Encryption Results
Shows sample code in ASP that matches Java JCE encryption results for 128-bit AES encryption using CBC mode. The equivalent Java JCE code is provided as commented-out code following the ASP code. <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> </head> <body> <% set crypt = Server.CreateObject("Chilkat_9_5_0.Crypt2") ' Any string argument automatically begins the 30-day trial. success = crypt.UnlockComponent("30-day trial") If (success <> 1) Then Response.Write "Crypt component unlock failed" & "<br>" End If password = "secretPassphrase" ' Use the Digest MD5 algorithm to get a 16-byte binary secret key. crypt.HashAlgorithm = "md5" crypt.EncodingMode = "hex" secretKeyHex = crypt.HashStringENC(password) crypt.CryptAlgorithm = "aes" crypt.CipherMode = "cbc" crypt.KeyLength = 128 crypt.SetEncodedKey secretKeyHex,"hex" crypt.SetEncodedIV secretKeyHex,"hex" text = "Though this be madness, yet there is method in 't" encText = crypt.EncryptStringENC(text) ' Displays: ' 79c357db030ac9ae66a84528a2f387717c5f10b48ef4385e5fe7b9227b3eeb8a ' 77f24900890e72e35758b1f79f8f78d51ebe94536c30c57fd7b2c17b927715d7 Response.Write encText & "<br>" ' ' This is the equivalent code using Java JCE: ' ' String password = "secretPassphrase"; ' ' MessageDigest md = MessageDigest.getInstance("MD5"); ' md.update(password.getBytes("UTF-8"), 0, password.length()); ' byte[] rawKey = md.digest(); ' String text = "Though this be madness, yet there is method in 't"; ' ' SecretKeySpec skeySpec = new SecretKeySpec(rawKey, "AES"); ' IvParameterSpec ivSpec = new IvParameterSpec(rawKey); ' ' Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); ' cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivSpec); ' ' byte[] encrypted = cipher.doFinal(text.getBytes("UTF-8")); ' ' System.out.println(toHex(encrypted)); ' This is the toHex method in Java: ' 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(); ' } %> </body> </html> |
||||
© 2000-2026 Chilkat Software, Inc. All Rights Reserved.