PAD_HexBlock 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 "diCryptoSys.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, 0)
String to receive the hexadecimal-encoded padded encryption block.Long specifying the maximum number of characters in strOutput.String containing the hexadecimal-encoded data to be padded.Long specifying the cipher block length in bytes (8 or 16).Long option flags: not used in this release. Specify zero.
long _stdcall PAD_HexBlock(char *szOutput, long nOutChars, const char *szInput, long blklen, long options);
Long: If successful, the return value is the number of characters in the output string;
otherwise it returns a negative error code.
Use the method associated with the relevant encryption algorithm class:
Aes128.Pad Method (String)
Blowfish.Pad Method (String)
Des.Pad Method (String)
Tdea.Pad Method (String)
The output will be padded according to the convention in
PKCS#5, PKCS#7 and CMS.
If strOutput is set to "" or nMaxChars set to zero,
then the required number of characters in the output string will be returned.
C/C++ users should add one to this value.
The output is always longer than the input.
Only block lengths of 8 or 16 bytes are supported.
VB6 users should note that an empty, uninitialised string is actually a NULL which will return an error if
used for strInput
- see the example below for a technique to cope with this.
A wrapper function:
Public Function padHexString(ByVal strInputHex As String, nBlockLen As Long) As String ' Adds padding to a hex string up to next multiple of block length. ' Returns a padded hex string or, on error, an empty string. Dim nOutChars As Long Dim strOutputHex As String ' In VB6 an uninitialised empty string is passed to a DLL as a NULL, ' so we append a non-null empty string! strInputHex = strInputHex & "" 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) If (nOutChars <= 0) Then Exit Function Debug.Print "Padded data='" & strOutputHex & "'" padHexString = strOutputHex End Function
Using the wrapper function:
Dim strInputHex As String
Dim strOutputHex As String
' Pad an empty string
Debug.Print "Input data= '" & strInputHex & "'"
' Pad for Triple DES block
strOutputHex = padHexString(strInputHex, API_BLK_TDEA_BYTES)
Debug.Print "Padded data='" & strOutputHex & "'"
' Prepare test input 5 bytes long
strInputHex = "FFFFFFFFFF"
Debug.Print "Input data= '" & strInputHex & "'"
' Pad for Triple DES block
strOutputHex = padHexString(strInputHex, API_BLK_TDEA_BYTES)
Debug.Print "Padded data='" & strOutputHex & "'"
This should result in output as follows:
Input data= '' Padded data='0808080808080808' Input data= 'FFFFFFFFFF' Padded data='FFFFFFFFFF030303'
PAD_UnpadHex
PAD_BytesBlock
PAD_UnpadBytes