CryptoSys API Library Manual

CIPHER_UpdateHex

Encrypts or decrypts a chunk of hexadecimal-encoded input (in incremental mode).

VBA/VB6 Syntax

Public Declare Function CIPHER_UpdateHex Lib "diCryptoSys.dll" (ByVal hContext As Long, ByVal strOutput As String, ByVal nOutChars As Long, ByVal strDataHex As String) As Long

nRet = CIPHER_UpdateHex(hContext, strOutput, Len(strOutput), strDataHex)

C/C++ Syntax

long __stdcall CIPHER_UpdateHex(long hContext, char *szOutput, long nOutChars, const char *szDataHex);

Parameters

hContext
[in] Handle to the CIPHER context.
szOutput
[out] String buffer to receive output.
nOutChars
[in] Length of output buffer in bytes.
szDataHex
[in] Hex-encoded input data to be processed.

Returns (VBA/C)

If successful, the return value is 0; otherwise it returns a non-zero error code.

VBA Wrapper Syntax

Public Function cipherUpdateHex(hContext As Long, szInputHex As String) As String

.NET Equivalent

Cipher.Update Method (String)

Remarks

This function is the equivalent of CIPHER_Update except the input and output are hexadecimal-encoded strings. The functions CIPHER_Update and CIPHER_UpdateHex (and their wrappers cipherUpdate and cipherUpdateHex) may be used interchangeably. For more information, see the remarks section in CIPHER_Update.

Example (VBA core function)

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 = CIPHER_InitHex(ENCRYPT, "aes128/cbc", strHexKey, strIV, 0)
If hContext = 0 Then
    MsgBox "Failed to set context", vbCritical
    Exit Sub
End If
' Do 10,000 times
sNext = sBlock
For j = 0 To 9999
    sBlock = String(Len(sNext), " ")
    nRet = CIPHER_UpdateHex(hContext, sBlock, Len(sBlock), sNext)
    If j = 0 Then
        sNext = strIV
    Else
        sNext = sLast
    End If
    sLast = sBlock
Next
Debug.Print "CT=" & sBlock
Debug.Print "OK=" & sCorrect
nRet = CIPHER_Final(hContext)

Debug.Assert (sCorrect = sBlock)

Example (VBA wrapper function)

' Using the wrapper functions
Dim keyhex As String
Dim ivhex  As String
Dim pthex As String
Dim cthex As String
Dim algstr As String
Dim hContext As Long
Dim nRet As Long

keyhex = "0123456789ABCDEFF0E1D2C3B4A59687"
ivhex = "FEDCBA9876543210FEDCBA9876543210"
Debug.Print "KY=" & keyhex
Debug.Print "IV=" & ivhex

' TEST 4 - Decrypt in CTR mode
algstr = "Aes128/ctr"
Debug.Print algstr
Debug.Print "DECRYPTING..."
' Initialize context for repeated chunks
hContext = cipherInitHex(DECRYPT, algstr, keyhex, ivhex, 0)
Debug.Assert hContext <> 0

' Pass data in chunks of 16 bytes (or multiples of)
' Part 1...
cthex = "3FAC68CBAE6D774151306E9DB16CE0191C51E91959DA4F082B7CE3498C2D20D7"
Debug.Print "CT=" & cthex
' NB switched pt <=> ct
pthex = cipherUpdateHex(hContext, cthex)
Debug.Print "PT=" & pthex
Debug.Print "'" & cnvStringFromHexStr(pthex) & "'"
' Part 2...
cthex = "8437EC92088FE4C19FB49BDF2BADF7C7FD6FB9A7D52A"
Debug.Print "CT=" & cthex
pthex = cipherUpdateHex(hContext, cthex)
Debug.Print "PT=" & pthex
Debug.Print "'" & cnvStringFromHexStr(pthex) & "'"

' We are done
nRet = cipherFinal(hContext)
Debug.Assert nRet = 0
Aes128/ctr
DECRYPTING...
CT=3FAC68CBAE6D774151306E9DB16CE0191C51E91959DA4F082B7CE3498C2D20D7
PT=4E6F77206973207468652074696D6520666F7220616C6C20676F6F64206D656E
'Now is the time for all good men'
CT=8437EC92088FE4C19FB49BDF2BADF7C7FD6FB9A7D52A
PT=20746F20636F6D6520746F2074686520616964206F66
' to come to the aid of'

See Also

CIPHER_Update

[Contents] [Index]

[PREV: CIPHER_Update...]   [Contents]   [Index]   
   [NEXT: CNV_B64Filter...]

Copyright © 2001-24 D.I. Management Services Pty Ltd. All rights reserved. Generated 2024-01-07T07:42:00Z.