Encrypts or decrypts data represented as a hexadecimal string using a specified mode. The key and initialization vector are represented as a hexadecimal string.
Public Declare Function AES256_HexMode Lib "diCryptoSys.dll"
(ByVal strOutput As String, ByVal strInput As String, ByVal strHexKey As String,
ByVal bEncrypt As Boolean,
ByVal strMode As String, ByVal strHexIV As String) As Long
AES256_HexMode(strOutput, strInput, strHexKey, bEncrypt, strMode, strHexIV)
long __stdcall AES256_HexMode(char *szOutput, const char *szInput, const char *szKey, int fEncrypt, const char *szMode, const char *szIV);
If successful, the return value is 0; otherwise it returns a non-zero error code.
Aes256.Encrypt Method (String, String, Mode, String)
Aes256.Decrypt Method (String, String, Mode, String)
The length of the input string szInput
must be a multiple of 32 hex characters long (i.e. representing a multiple of the block size of 16 bytes).
The key string szHexKey
must be exactly 64 hex characters long (i.e. representing exactly 32 bytes/256 bits).
The initialization vector szHexIV
must be exactly 32 hex characters long (i.e. representing exactly the block size of 16 bytes),
except for ECB mode, where it is ignored (use ""
).
The output string szOutput must be set up with at least the
same number of characters as the input
string before calling.
The variables szOutput and szInput should be different.
Valid hexadecimal characters are [0-9A-Fa-f].
The examples are from RFC 3602 The AES-CBC Cipher Algorithm and Its Use with IPsec
Dim nRet As Long Dim strOutput As String Dim strInput As String Dim strHexKey As String Dim sPlain As String Dim sCipher As String Dim strHexIV As String ' Encrypting 64 bytes (4 blocks) using AES-CBC with 256-bit key strHexKey = "56e47a38c5598974bc46903dba29034906a9214036b8a15b512e03d534120006" strHexIV = "8ce82eefbea0da3c44699ed7db51b7d9" sPlain = "a0a1a2a3a4a5a6a7a8a9aaabacadaeaf" _ & "b0b1b2b3b4b5b6b7b8b9babbbcbdbebf" _ & "c0c1c2c3c4c5c6c7c8c9cacbcccdcecf" _ & "d0d1d2d3d4d5d6d7d8d9dadbdcdddedf" sCipher = "28409A2982BD2CF7CE343A7D43F6927F" _ & "DB9EC532BBA569EEC92E57A209C4FDBA" _ & "59ADBA05A5C854694DDC9F7991C01634" _ & "E72BEB4FE0236CB3B119A463891E346F" strInput = sPlain ' Set strOutput to be same length as strInput strOutput = String(Len(strInput), " ") Debug.Print "KY=" & strHexKey Debug.Print "IV=" & strHexIV Debug.Print "PT=" & strInput ' Encrypt in one-off process nRet = AES256_HexMode(strOutput, strInput, strHexKey, ENCRYPT, "CBC", strHexIV) Debug.Print "CT=" & strOutput; nRet Debug.Print "OK=" & sCipher Debug.Assert (strOutput = sCipher) ' Decrypt to check strInput = strOutput nRet = AES256_HexMode(strOutput, strInput, strHexKey, DECRYPT, "CBC", strHexIV) Debug.Print "P'=" & strOutput; nRet Debug.Print "OK=" & sPlain Debug.Assert (strOutput = sPlain)
This should result in output as follows:
KY=56e47a38c5598974bc46903dba29034906a9214036b8a15b512e03d534120006 IV=8ce82eefbea0da3c44699ed7db51b7d9 PT=a0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebf c0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedf CT=28409A2982BD2CF7CE343A7D43F6927FDB9EC532BBA569EEC92E57A209C4FDBA 59ADBA05A5C854694DDC9F7991C01634E72BEB4FE0236CB3B119A463891E346F 0 OK=28409A2982BD2CF7CE343A7D43F6927FDB9EC532BBA569EEC92E57A209C4FDBA 59ADBA05A5C854694DDC9F7991C01634E72BEB4FE0236CB3B119A463891E346F P'=A0A1A2A3A4A5A6A7A8A9AAABACADAEAFB0B1B2B3B4B5B6B7B8B9BABBBCBDBEBF C0C1C2C3C4C5C6C7C8C9CACBCCCDCECFD0D1D2D3D4D5D6D7D8D9DADBDCDDDEDF 0 OK=a0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebf c0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedf