Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Delphi DLL) Duplicate Java AES ResultsDemonstrates how to duplicate Java code for AES encryption.
uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Crypt2; ... procedure TForm1.Button1Click(Sender: TObject); var crypt: HCkCrypt2; encrypted: PWideChar; begin crypt := CkCrypt2_Create(); CkCrypt2_putCryptAlgorithm(crypt,'aes'); CkCrypt2_putCipherMode(crypt,'cbc'); CkCrypt2_putKeyLength(crypt,256); CkCrypt2_SetEncodedIV(crypt,'000102030405060708090A0B0C0D0E0F','hex'); CkCrypt2_SetEncodedKey(crypt,'uX1AytOx2bqKn1G1NiblFM3YrEMnvciC','ascii'); CkCrypt2_putEncodingMode(crypt,'hex'); encrypted := CkCrypt2__encryptStringENC(crypt,'tmpString'); Memo1.Lines.Add(encrypted); Memo1.Lines.Add(CkCrypt2__decryptStringENC(crypt,encrypted)); // The above Chilkat code duplicates the following Java code: // import javax.crypto.Cipher; // import javax.crypto.spec.IvParameterSpec; // import javax.crypto.spec.SecretKeySpec; // // public class Test { // private static final String KEY = "uX1AytOx2bqKn1G1NiblFM3YrEMnvciC"; // private static final String IV = "000102030405060708090A0B0C0D0E0F"; // // private static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray(); // public static String bytesToHex(byte[] bytes) { // char[] hexChars = new char[bytes.length * 2]; // for (int j = 0; j < bytes.length; j++) { // int v = bytes[j] & 0xFF; // hexChars[j * 2] = HEX_ARRAY[v >>> 4]; // hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F]; // } // return new String(hexChars); // } // public static byte[] hexToBytes(String s) { // int len = s.length(); // byte[] data = new byte[len / 2]; // for (int i = 0; i < len; i += 2) { // data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) // + Character.digit(s.charAt(i+1), 16)); // } // return data; // } // public static byte[] encrypt(String value) { // try { // System.out.println("---- encrypt ----"); // IvParameterSpec iv = new IvParameterSpec(hexToBytes(IV)); // SecretKeySpec skeySpec = new SecretKeySpec(KEY.getBytes("ASCII"), "AES"); // Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); // cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv); // byte[] encrypted = cipher.doFinal(value.getBytes("ASCII")); // String x = bytesToHex(encrypted); // System.out.println(x); // return encrypted; // } catch (Exception ex) { // ex.printStackTrace(); // return null; // } // } // // public static String decrypt(byte[] encrypted) { // try { // System.out.println("---- decrypt ----"); // IvParameterSpec iv = new IvParameterSpec(hexToBytes(IV)); // SecretKeySpec skeySpec = new SecretKeySpec(KEY.getBytes("ASCII"), "AES"); // Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); // cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv); // byte[] original = cipher.doFinal(encrypted); // String x = new String(original); // System.out.println(x); // return x; // } catch (Exception ex) { // ex.printStackTrace(); // return null; // } // // } // // public static void main(String[] args) { // byte[] encrypted = encrypt("tmpString"); // decrypt(encrypted); // } // } // CkCrypt2_Dispose(crypt); end; |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.