AES192_BytesMode 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 abOutput As Byte, ByRef abData As Byte, ByVal nDataLen As Long, ByRef abKey As Byte,
ByVal bEncrypt As Boolean,
ByVal strMode As String, ByRef abInitV As Byte) As Long
nRet = AES192_Bytes(abOutput(0), abData(0), nDataLen, abKey(0),
bEncrypt, strMode, abInitV(0))
Byte array of sufficient length to receive the output.Byte array containing the input data.Long equal to length of the input data in bytes.Byte array containing the key.Boolean direction flag:
set as True to encrypt or False
to decrypt.String specifying the confidentiality mode:Byte containing the
initialization vector (IV), or zero (0) for ECB mode.
long _stdcall AES192_BytesMode(unsigned char *output, const unsigned char *input,
long nbytes, const unsigned char *key, int bEncrypt,
const char *lpszMode, const unsigned char *iv);
Long: 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 abData
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 abOutput must be at least as long as the input array.
abOutput and abData may be the same.
Note the (0) after the byte array variables.
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