CryptoSys API Library Manual


Creates a hex-encoded input block suitably padded for encryption by a block cipher in ECB or CBC mode.

VB6/VBA Syntax

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)

C/C++ Syntax

long _stdcall PAD_HexBlock(char *szOutput, long nMaxChars, const char *szInput, long nBlkLen, long nOptions);


[out] to receive the hexadecimal-encoded padded encryption block.
[in] specifying the maximum number of characters in szOutput.
[in] containing the hexadecimal-encoded data to be padded.
[in] specifying the cipher block length in bytes (8 or 16).
[in] option flags: not used in this release. Specify zero.

Returns (VB6/C)

If successful, the return value is the number of characters in the output string; otherwise it returns a negative error code.

.NET Equivalent

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 szOutput 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/VBA users should note that an empty, uninitialised string is actually a NULL which will return an error if used for szInput - 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'

See Also

PAD_UnpadHex PAD_BytesBlock PAD_UnpadBytes

[Contents] [Index]

[HOME]   [NEXT: PAD_UnpadBytes...]

Copyright © 2001-12 D.I. Management Services Pty Ltd. All rights reserved.