Zip Component, Email Component, Encryption Component ActiveX Control for Zip Compression .NET Components for ASP.NET
ActiveX and .NET Components for Zip Compression, Encryption, Email, XML, S/MIME, HTML Email, Character Encoding, Digital Certificates, FTP, and more ASP Email ActiveX Component


Index of Chilkat Blog Posts

January 17, 2008

PHP 128-bit AES CBC Encryption to Match Chilkat ActiveX Encryption

Here is a sample PHP 5.2.5 script using 128-bit AES encryption (Cipher-Block-Chaining Mode) and the corresponding VBScript code using the Chilkat Encryption ActiveX.
Both scripts produce the same output:

encrypted: DzWDlPXZDE4Gu4NdBtcMSA==
decrypted: my secret text

PHP AES Script

<?php
	
$cc = 'my secret text';
$key = '1234567890123456';
$iv =  '1234567890123456';
$length = strlen($cc);
	
$cipher = mcrypt_module_open(MCRYPT_RIJNDAEL_128,'','cbc','');
	
mcrypt_generic_init($cipher, $key, $iv);
$encrypted = base64_encode(mcrypt_generic($cipher,$cc));
mcrypt_generic_deinit($cipher);
	
mcrypt_generic_init($cipher, $key, $iv);
$decrypted = mdecrypt_generic($cipher,base64_decode($encrypted));
mcrypt_generic_deinit($cipher);
	
echo "encrypted: " . $encrypted;
echo "\n";
echo "decrypted: " . substr($decrypted, 0, $length) . "\n";
	
?>

VBScript AES Script

Dim fso, outFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set outFile = fso.CreateTextFile("outputCbc.txt", True)
	
set crypt = CreateObject("Chilkat.Crypt2")
	
success = crypt.UnlockComponent("Anything for 30-day trial")
If (success <> 1) Then
    MsgBox "Crypt component unlock failed"
    WScript.Quit
End If
	
crypt.CryptAlgorithm = "aes"
crypt.CipherMode = "cbc"
crypt.KeyLength = 128
crypt.PaddingScheme = 3
	
crypt.SetEncodedKey "1234567890123456","ascii"
crypt.SetEncodedIV "1234567890123456","ascii"
	
crypt.EncodingMode = "base64"
text = "my secret text"
	
'  Encrypt a string and return the binary encrypted data
'  in a base-64 encoded string.
encText = crypt.EncryptStringENC(text)
	
outFile.WriteLine(encText)
	
'  Decrypt and show the original string:
decryptedText = crypt.DecryptStringENC(encText)
' This gets rid of the NULL padding chars...
decryptedText = crypt.TrimEndingWith(decryptedText," ")
	
outFile.WriteLine(decryptedText)
outFile.Close


Privacy Statement. Copyright 2000-2011 Chilkat Software, Inc. All rights reserved.
Send feedback to support@chilkatsoft.com

Components for Microsoft Windows XP, 2000, 2003 Server, Vista, Windows 7, and Windows 95/98/NT4.