Creates a hex-encoded input block suitably padded for encryption by a block cipher in ECB or CBC mode.
Public Declare Function PAD_HexBlock Lib "diCrPKI.dll" (ByVal strOutput As String, ByVal nMaxChars As Long, ByVal strInputHex As String, ByVal nBlockLen As Long, ByVal nOptions As Long) As Long
nRet = PAD_HexBlock(strOutput, nMaxChars, strInput, nBlockLen, nOptions)
long __stdcall PAD_HexBlock(char *szOutput, long nOutChars, const char *szInput, long nBlkLen, long nOptions);
If successful, the return value is the number of characters in or required for the output string; otherwise it returns a negative error code.
Public Function padHexBlock
(szInput As String, nBlkLen As Long, Optional nOptions As Long = 0) As String
Use the method associated with the relevant encryption algorithm class:
Cipher.Pad Method (String, CipherAlgorithm, Padding)
static Cipher.pad_hex(datahex, alg, pad=Pad.PKCS5)
For the "raw" VBA/C function, the user must allocate an output string buffer szOutput of the required length. Specify a zero nOutChars or an empty string for szOutput to find the required length. ANSI C users must add one to this value when allocating memory.
The output will be padded according to the convention specified in nOptions. The output is always longer than the input. Only block lengths of 8 or 16 bytes are supported.
Dim strInputHex As String Dim strOutputHex As String Dim nOutChars As Long Dim nBlockLen As Long Dim i As Long ' Prepare test input 5 bytes long strInputHex = "FFFFFFFFFF" Debug.Print "Input data='" & strInputHex & "'" ' Find out the required number of characters in output nBlockLen = 8 ' NB block length is in bytes nOutChars = PAD_HexBlock("", 0, strInputHex, nBlockLen, 0) Debug.Print "Required length is " & nOutChars & " characters" ' Check for error If (nOutChars <= 0) Then Exit Function ' Pre-dimension output strOutputHex = String(nOutChars, " ") nOutChars = PAD_HexBlock(strOutputHex, Len(strOutputHex), strInputHex, nBlockLen, 0) Debug.Print "Padded data='" & strOutputHex & "'" ' Now set input as padded output and remove padding strInputHex = strOutputHex ' Remove padding... ' No need to query for length because we know the output will be shorter than input ' so make sure output is as long as the input strOutputHex = String(Len(strInputHex), " ") nOutChars = PAD_UnpadHex(strOutputHex, Len(strOutputHex), strInputHex, nBlockLen, 0) Debug.Print "Unpadded length is " & nOutChars & " characters" ' Check for error If (nOutChars <= 0) Then Exit Function ' Re-dimension the output to the correct length strOutputHex = Left$(strOutputHex, nOutChars) Debug.Print "Unpadded data='" & strOutputHex & "'"
This should result in output as follows:
Input data='FFFFFFFFFF' Required length is 16 characters Padded data='FFFFFFFFFF030303' Unpadded length is 10 characters Unpadded data='FFFFFFFFFF'
Dim strInputHex As String Dim strOutputHex As String strInputHex = "FDFDFD" Debug.Print "Hex input =" & strInputHex strOutputHex = padHexBlock(strInputHex, 8, 0) Debug.Print "Padded data=" & strOutputHex Debug.Print "Unpadded =" & padUnpadHex(strOutputHex, 8, 0) strOutputHex = padHexBlock(strInputHex, 8, PKI_PAD_1ZERO) Debug.Print "Padded data=" & strOutputHex Debug.Print "Unpadded =" & padUnpadHex(strOutputHex, 8, PKI_PAD_1ZERO) strOutputHex = padHexBlock(strInputHex, 8, PKI_PAD_AX923) Debug.Print "Padded data=" & strOutputHex Debug.Print "Unpadded =" & padUnpadHex(strOutputHex, 8, PKI_PAD_AX923)
PAD_UnpadHex
PAD_BytesBlock
PAD_UnpadBytes