Carries out the AES transformation function on a hexadecimal string
according to the direction
and mode set up by an earlier call to AES128_Init()
or AES128_InitHex()
.
Public Declare Function AES128_UpdateHex Lib "diCryptoSys.dll"
(ByVal hContext As Long, ByVal strHexString As String) As Long
nRet = AES128_UpdateHex(hContext, strHexString)
long __stdcall AES128_UpdateHex(long hContext, char *szHexData);
AES128_Init()
or AES128_InitHex()
.If successful, the return value is 0; otherwise it returns a non-zero error code.
Aes128.Update Method (String)
The length of the input string szHexString must be a multiple of 32 hex characters long (i.e. representing a multiple of the block size of 16 bytes). If not, an error code will be returned. Valid hexadecimal characters are [0-9A-Fa-f]. Note that the output overwrites the input. szHexString must be a variable that can receive the output, not a constant.
This example carries out the third (I=2) [MOVS] Monte Carlo test in CBC encrypt mode (Ref: AES Candidate Algorithm Submissions, update 17 Feb 1998, file cbc_e_m.txt [RIJNVALS]).
Dim nRet As Long Dim hContext As Long Dim sBlock As String Dim strHexKey As String Dim strIV As String Dim sNext As String Dim sLast As String Dim sCorrect As String Dim j As Integer ' cbc_e_m.txt ' KEYSIZE=128 I=2 ' KEY=93286764A85146730E641888DB34EB47 ' IV=192D9B3AA10BB2F7846CCBA0085C657A ' PT=983BF6F5A6DFBCDAA19370666E83A99A ' CT=40D8DAF6D1FDA0A073B3BD18B7695D2E strHexKey = "93286764A85146730E641888DB34EB47" strIV = "192D9B3AA10BB2F7846CCBA0085C657A" sBlock = "983BF6F5A6DFBCDAA19370666E83A99A" sCorrect = "40D8DAF6D1FDA0A073B3BD18B7695D2E" Debug.Print "AES Monte Carlo CBC Mode Encrypt:" Debug.Print "KY=" & strHexKey Debug.Print "IV=" & strIV Debug.Print "PT=" & sBlock hContext = AES128_InitHex(strHexKey, ENCRYPT, "CBC", strIV) If hContext = 0 Then MsgBox "Failed to set context", vbCritical Exit Function End If ' Do 10,000 times sNext = sBlock For j = 0 To 9999 sBlock = sNext nRet = AES128_UpdateHex(hContext, sBlock) If j = 0 Then sNext = strIV Else sNext = sLast End If sLast = sBlock Next Debug.Print "CT=" & sBlock Debug.Print "OK=" & sCorrect nRet = AES128_Final(hContext) Debug.Assert (sCorrect = sBlock)
This should result in output as follows:
AES Monte Carlo CBC Mode Encrypt: KY=93286764A85146730E641888DB34EB47 IV=192D9B3AA10BB2F7846CCBA0085C657A PT=983BF6F5A6DFBCDAA19370666E83A99A CT=40D8DAF6D1FDA0A073B3BD18B7695D2E OK=40D8DAF6D1FDA0A073B3BD18B7695D2E
AES128_Init
AES128_InitHex
AES128_Update
AES128_Final