After very hard working for two days, the FAST AES UDF finally here. These functions use the embedding DLL technique and the codes are almost all written in low-level language, so it is PURE SCRIPT but run very fast. For general user, the interface is quite simple: ; To encrypt/decrypt memory block or string:
$Encrypted = _AesEncrypt("Key", "Plantext")
$Decrypted = _AesDecrypt("Key", $Encrypted)
$Result = BinaryToString($Decrypted)
; To encrypt/decrypt file
_AesEncryptFile("Key", $PlantextFilename, $ChipertextFilename)
_AesDecryptFile("Key", $ChipertextFilename, $PlantextFilename) For advanced user, here are some technical details. The exactly key type of the functions should be 16, 24, or 32 bytes binary. If a string is used, the script just convert it into binary and pad 0x00. To use the binary keys, my MD5 and SHA1/SHA2 hash UDF may be helpful (here and here). For both memory and file functions, there are three block cipher modes include "CBC", "CFB", "OFB" can be specified. For example: (See "What is block cipher modes". BTW, CBC mode use the ciphertext stealing method internally.)
_AesEncrypt("Key", "Plantext", "CFB")
_AesDecrypt("Key", $Encrypted, "CFB")
_AesEncryptFile("Key", $PlantextFilename, $ChipertextFilename, "CFB")
_AesDecryptFile("Key", $ChipertextFilename, $PlantextFilename, "CFB")
_AesEncrypt/_AesDecrypt operate on only one block of memory. If the data to encrypt/decrypt are not continuous, you have to handle all the work by yourself. For example:
$IV = Binary("0x00000000000000000000000000000000")
$Ctx = _AesEncryptKey($Key)
$Ret = _AesCryptOFB($Ctx, $IV, "The quick brown fox ")
$Ret &= _AesCryptOFB($Ctx, $IV, "jumps over the lazy dog")
MsgBox(0, '', $Ret)
$IV = Binary("0x00000000000000000000000000000000")
$Ctx = _AesEncryptKey($Key)
$Ret = _AesCryptOFB($Ctx, $IV, $Ret)
MsgBox(0, '', BinaryToString($Ret))
CBC/CFB/OFB are different. To use the correct CTX generator and set the correct IV are important. Dig the source in AESTest.au3 may get some inspiration. Here is a summary:
CBC mode uses _AesEncryptKey/_AesEncryptCBC to encrpyt and _AesDecryptKey/_AesDecryptCBC to decrypt. But if the length of source < 16 bytes, the IV returned by _AesEncryptCBC should be used. Otherwise, use the same IV as _AesEncryptCBC.CFB mode always uses _AesEncryptKey to generate a CTX, but uses _AesEncryptCFB/_AesDecryptCFB to encrypt/decrypt. However, it always uses the same IV to start.OFB mode is the simplest one. Encryption and decryption are exactly the same. See the example.Notice: Although CFB/OFB only use the _AesEncryptKey() to generate a CTX of key, but always regenerate a new CTX before starting to encrypt or decrypt. This library should pass both Known Answer Test and Monte Carlo Test. To try it, download the test vectors from here and remove the comment in AESTest.au3. Have fun!AES.zip 2008/12/06 Update Note: Update MemoryDllCall.au3 AES.zip ECB encrypt/decrypt code in asm by Brian Gladman, other parts by Ward.