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
(PowerBuilder) AES 256-bit CBC using PBKDF2 Generated Secret KeySee more Encryption ExamplesFirst generates a 32-byte secret key using PBKDF2 (with HMAC-SHA256), and then uses the secret key to do 256-bit AES CBC mode decryption. (Duplicates the following Java code) public String decrypt(String strToDecrypt) { try { // Read the initialization vector from the first bytes for the data byte [] rawData = Base64.getDecoder().decode(strToDecrypt); byte [] iv = new byte[Config.get().encryptionIVLength()]; byte [] encryptedData = new byte[rawData.length - iv.length]; System.arraycopy(rawData, 0, iv, 0, iv.length); System.arraycopy(rawData, iv.length, encryptedData, 0, encryptedData.length); IvParameterSpec ivspec = new IvParameterSpec(iv); SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256"); KeySpec spec = new PBEKeySpec(secretKey.toCharArray(), salt.getBytes(), 65536, 256); SecretKey tmp = factory.generateSecret(spec); SecretKeySpec secretKey = new SecretKeySpec(tmp.getEncoded(), "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING"); cipher.init(Cipher.DECRYPT_MODE, secretKey, ivspec); return new String(cipher.doFinal(encryptedData)); } catch (Exception e) { log.error("Could not decrypt the string.", e); } return null; }
integer li_rc oleobject loo_Crypt string ls_Password string ls_SaltHex integer li_IterationCount integer li_OutputKeyBitLen string ls_SecretKeyHex string ls_Iv string ls_StrToDecrypt string ls_DecryptedStr // This example requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. loo_Crypt = create oleobject // Use "Chilkat_9_5_0.Crypt2" for versions of Chilkat < 10.0.0 li_rc = loo_Crypt.ConnectToNewObject("Chilkat.Crypt2") if li_rc < 0 then destroy loo_Crypt MessageBox("Error","Connecting to COM object failed") return end if ls_Password = "some arbitrary length password" // We have 8 bytes of salt encoded using hex. // (The salt can be any number of bytes, in any desired encoding such as base64, hex, etc.) ls_SaltHex = "0102030405060708" // Generate the 256-bit (32-byte) AES secret key we'll use to decrypt. li_IterationCount = 65536 li_OutputKeyBitLen = 256 ls_SecretKeyHex = loo_Crypt.Pbkdf2(ls_Password,"utf-8","sha256",ls_SaltHex,li_IterationCount,li_OutputKeyBitLen,"hex") // Setup for 256-bit AES CBC decryption. loo_Crypt.CryptAlgorithm = "aes" loo_Crypt.KeyLength = 256 loo_Crypt.CipherMode = "cbc" loo_Crypt.PaddingScheme = 0 // The IV for AES is 16 bytes. ls_Iv = "000102030405060708090A0B0C0D0E0F" loo_Crypt.SetEncodedIV(ls_Iv,"hex") // Set the secret key for 256-bit AES. loo_Crypt.SetEncodedKey(ls_SecretKeyHex,"hex") // AES decrypt // assume our string to decrypt is base64 ls_StrToDecrypt = "...." loo_Crypt.EncodingMode = "base64" ls_DecryptedStr = loo_Crypt.DecryptStringENC(ls_StrToDecrypt) Write-Debug ls_DecryptedStr destroy loo_Crypt |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.