Encrypts or decrypts an array of Bytes using a specified mode. The key and IV data are in byte arrays.
Public Declare Function AES192_BytesMode Lib "diCryptoSys.dll"
(ByRef lpOutput As Byte, ByRef lpData As Byte, ByVal nDataLen As Long, ByRef lpKey As Byte,
ByVal bEncrypt As Boolean,
ByVal strMode As String, ByRef lpInitV As Byte) As Long
nRet = AES192_Bytes(lpOutput(0), abData(0), nDataLen, abKey(0), bEncrypt, strMode, abInitV(0))
' Note the "(0)" after the byte array parameters
long __stdcall AES192_BytesMode(unsigned char *lpOutput, const unsigned char *lpInput, long nBytes, const unsigned char *lpKey, int fEncrypt, const char *szMode, const unsigned char *lpIV);
If successful, the return value is 0; otherwise it returns a non-zero error code.
Aes192.Encrypt Method (Byte[], Byte[], Mode, Byte[])
Aes192.Decrypt Method (Byte[], Byte[], Mode, Byte[])
The input array lpData
must be a multiple of the block size of 16 bytes long.
If not, an error code will be returned.
The key array abKey()
must be exactly 24 bytes long (i.e. 192 bits).
The initialization vector abInitV()
must be exactly the block size of 16 bytes long, except for ECB mode, where it is ignored (use 0).
The output array lpOutput must be at least as long as the input array.
lpOutput and lpData may be the same.
Dim nRet As Long
Dim strOutput As String
Dim strInput As String
Dim sCorrect As String
Dim abKey() As Byte
Dim abInitV() As Byte
Dim abResult() As Byte
Dim abData() As Byte
Dim nDataLen As Long
' Set up input in byte arrays
strInput = "Now is the time for all good men"
sCorrect = "4BABDC251410549C57390F7FA3F26366F9AC1FABD1354ECA2265F475A2C3D5AF"
abKey = cnvBytesFromHexStr("000102030405060708090a0b0c0d0e0f1011121314151617")
abInitV = cnvBytesFromHexStr("FEDCBA9876543210FEDCBA9876543210")
abData = StrConv(strInput, vbFromUnicode)
nDataLen = UBound(abData) - LBound(abData) + 1
' Pre-dimension output array
ReDim abResult(nDataLen - 1)
Debug.Print "KY=" & cnvHexStrFromBytes(abKey)
Debug.Print "IV=" & cnvHexStrFromBytes(abInitV)
Debug.Print "PT=" & cnvHexStrFromBytes(abData)
' Encrypt in one-off process
nRet = AES192_BytesMode(abResult(0), abData(0), nDataLen, abKey(0), _
ENCRYPT, "CBC", abInitV(0))
Debug.Print "CT=" & cnvHexStrFromBytes(abResult)
Debug.Print "OK=" & sCorrect
Debug.Assert (sCorrect = cnvHexStrFromBytes(abResult))
' Now decrypt back
nRet = AES192_BytesMode(abData(0), abResult(0), nDataLen, abKey(0), _
DECRYPT, "CBC", abInitV(0))
strOutput = StrConv(abData(), vbUnicode)
Debug.Print "P'=" & strOutput
' Check
Debug.Assert (strOutput = strInput)
This should result in output as follows:
KY=000102030405060708090A0B0C0D0E0F1011121314151617 IV=FEDCBA9876543210FEDCBA9876543210 PT=4E6F77206973207468652074696D6520666F7220616C6C20676F6F64206D656E CT=4BABDC251410549C57390F7FA3F26366F9AC1FABD1354ECA2265F475A2C3D5AF OK=4BABDC251410549C57390F7FA3F26366F9AC1FABD1354ECA2265F475A2C3D5AF P'=Now is the time for all good men