Chilkat HOME ASP Visual Basic VB.NET C# Visual C++ C MFC Delphi FoxPro Java Perl PHP Python Ruby SQL Server VBScript
AES Encrypt Byte ArrayPerl code to AES encrypt a byte array.
use chilkat; $crypt = new chilkat::CkCrypt2(); # Any string argument automatically begins the 30-day trial. $success = $crypt->UnlockComponent("30-day trial"); if ($success != 1) { print "Crypt component unlock failed" . "\n"; exit; } # Use 128-bit AES encryption, in CBC mode. $crypt->put_CryptAlgorithm("aes"); $crypt->put_CipherMode("cbc"); $crypt->put_KeyLength(128); # In Perl, strings are sequences of bytes with no contraints # on length and content. Any byte value can be included, # including null bytes. # Create a string of 256 bytes with each byte having # the value of its index. use bytes; $inBytes = ""; for ($i=0; $i < 256; $i++) { $inBytes = $inBytes . chr($i); } no bytes; # Create byte strings for our 128-bit secret key and # initialization vector: use bytes; $keyBytes = ""; for ($i=0; $i < 16; $i++) { $keyBytes = $keyBytes . chr($i); } $ivBytes = ""; for ($i=0; $i < 16; $i++) { $ivBytes = $ivBytes . chr($i); } no bytes; # The secret key is equal in length to the KeyLength. # In this case, the KeyLength = 128 bits, so the SecretKey # is 16 bytes (16 * 8 = 128) $crypt->SetSecretKey($keyBytes,16); # For AES encryption, the IV is always 16 bytes. # If omitted, it defaults to 16 null bytes. $crypt->SetIV($ivBytes,16); # The Chilkat Crypt module will work with CkByteData # objects. Copy the bytes into a CkByteData object: $inData = new chilkat::CkByteData(); $inData->append($inBytes,256); # Encrypt... $encryptedData = new chilkat::CkByteData(); $crypt->EncryptBytes($inData,$encryptedData); # Extract the encrypted bytes back into a Perl string. # (Remember, this isn't a printable string. It's a string # with binary byte data.) $encryptedBytes = $encryptedData->getBytes(); # How many bytes in the output? print "encrypted size = " . length($encryptedBytes) . " bytes\n"; # We encrypted 256 bytes and the output is 272 bytes. Why? # That's because padding is required to maintain a 16-octet (128-bit) blocksize. # PS> The IV is always equal to the blocksize in any symmetric # encryption algorithm. # PKCS#5 padding works as follows: the bytes remaining to fill a # block are assigned a number, which is the number of bytes that # were added to fill the block. For instance, if we have an 16-byte # block, and only 11 bytes are filled, then we have 5 bytes to pad. # Those 5 bytes are all assigned the value "5", for the 5 bytes of # padding. In the case where the data is already a multiple # of 16, an additional 16 bytes are added, each byte containing 0x10. # Thus, when 256 bytes are encrypted, the result is 272 bytes. # Save the encrypted data to a file. $encryptedData->saveFile("encryptedData.dat"); # Display the encrypted data as a hexidecimal string print $crypt->encodeBytes($encryptedBytes,length($encryptedBytes),"hex") . "\n"; # Decrypt... $decryptedData = new chilkat::CkByteData(); $crypt->DecryptBytes($encryptedData,$decryptedData ); # Get the bytes. $decryptedBytes = $decryptedData->getBytes(); # The size in bytes is 256. Chilkat Crypt automatically unpadded. print "Decrypted size = " . length($decryptedBytes) . " bytes\n"; # Display the decrypted data in hex format: print $crypt->encodeBytes($decryptedBytes,length($decryptedBytes),"hex") . "\n"; |
Need a specific example? Send a request to support@chilkatsoft.com
© 2000-2008 Chilkat Software, Inc. All Rights Reserved.