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
Duplicate Java's PBEWithMD5AndDESDemonstrate's how to duplicate Java's PBEWithMD5AndDES.
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, CHILKATCRYPT2Lib_TLB, OleCtrls; ... procedure TForm1.Button1Click(Sender: TObject); var crypt: TChilkatCrypt2; success: Integer; plainText: String; encryptedText: String; decryptedText: String; begin crypt := TChilkatCrypt2.Create(Self); success := crypt.UnlockComponent('Anything for 30-day trial'); if (success <> 1) then begin ShowMessage(crypt.LastErrorText); Exit; end; crypt.CryptAlgorithm := 'pbes1'; crypt.PbesPassword := 'secret'; crypt.PbesAlgorithm := 'des'; crypt.KeyLength := 64; // The salt for PBKDF1 is always 8 bytes: crypt.SetEncodedSalt('C773218C7EC8EE99','hex'); crypt.IterationCount := 20; // A hash algorithm needs to be set for PBES1: crypt.HashAlgorithm := 'md5'; // Indicate that the encrypted bytes should be returned // as a hex string: crypt.EncodingMode := 'hex'; plainText := 'This is another example'; encryptedText := crypt.EncryptStringENC(plainText); // The output should be: E729548140D20B14E9140F832A7E980AA449A84B295C75CE Memo1.Lines.Add(encryptedText); // Now decrypt: decryptedText := crypt.DecryptStringENC(encryptedText); Memo1.Lines.Add(decryptedText); // ---------------------------------------------------------------- // ---------------------------------------------------------------- // The code above duplicates this Java JCE example: public static void main(String args[]) throws Exception { PBEKeySpec pbeKeySpec; PBEParameterSpec pbeParamSpec; SecretKeyFactory keyFac; // Salt byte[] salt = { (byte)0xc7, (byte)0x73, (byte)0x21, (byte)0x8c, (byte)0x7e, (byte)0xc8, (byte)0xee, (byte)0x99 }; // Iteration count int count = 20; // Create PBE parameter set pbeParamSpec = new PBEParameterSpec(salt, count); String password = "secret"; pbeKeySpec = new PBEKeySpec(password.toCharArray()); keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES"); SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec); // Create PBE Cipher Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES"); // Initialize PBE Cipher with key and parameters pbeCipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParamSpec); // Our cleartext byte[] cleartext = "This is another example".getBytes(); // Encrypt the cleartext byte[] ciphertext = pbeCipher.doFinal(cleartext); System.out.print("ciphertext: "); System.out.println(toHex(ciphertext)); } end; |
© 2000-2010 Chilkat Software, Inc. All Rights Reserved.