Chilkat
HOME
Android™
ASP
Visual Basic
VB.NET
C#
iOS (IPhone)
Objective-C
C++
C
MFC
Delphi
FoxPro
Java
Perl
PHP Extension
PHP ActiveX
Python
PowerShell
Ruby
SQL Server
VBScript
Match Java JCE AES Encryption Results (ECB mode)
Shows sample code in Visual Basic that matches Java JCE encryption results for 128-bit AES encryption using ECB mode. Dim crypt As New ChilkatCrypt2 ' Any string argument automatically begins the 30-day trial. Dim success As Integer success = crypt.UnlockComponent("30-day trial") If (success <> 1) Then MsgBox "Crypt component unlock failed" Exit Sub End If Dim password As String password = "secretPassphrase" ' Use the Digest MD5 algorithm to get a 16-byte binary secret key. crypt.HashAlgorithm = "md5" crypt.EncodingMode = "hex" Dim secretKeyHex As String secretKeyHex = crypt.HashStringENC(password) crypt.CryptAlgorithm = "aes" crypt.CipherMode = "ecb" crypt.KeyLength = 128 crypt.SetEncodedKey secretKeyHex,"hex" Dim text As String text = "The quick brown fox jumped over the lazy dog." Dim encText As String encText = crypt.EncryptStringENC(text) ' Displays: ' C960892077D566258D38DF4BBAE8548E6827BF8C27A9D2EE ' 6F563FC75003F8F6A05B54CFD28CDC8833BD073F7FC6D031 Text1.Text = Text1.Text & encText & vbCrLf Text1.Refresh ' This Java (JCE) code produces the same results: ' String password = "secretPassphrase"; ' ' MessageDigest md = MessageDigest.getInstance("MD5"); ' md.update(password.getBytes("UTF-8"), 0, password.length()); ' byte[] rawKey = md.digest(); ' ' String text = "The quick brown fox jumped over the lazy dog."; ' ' SecretKeySpec skeySpec = new SecretKeySpec(rawKey, "AES"); ' Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); ' cipher.init(Cipher.ENCRYPT_MODE, skeySpec); ' ' 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(); ' } |
© 2000-2012 Chilkat Software, Inc. All Rights Reserved.