CryptoSys API Library Manual

PAD_HexBlock

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

VBA/VB6 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);

Parameters

szOutput
[out] to receive the hexadecimal-encoded padded encryption block.
nMaxChars
[in] specifying the maximum number of characters in szOutput.
szInput
[in] containing the hexadecimal-encoded data to be padded.
nBlkLen
[in] specifying the cipher block length in bytes (8 or 16).
nOptions
[in] Option flags. Select one of:
API_PAD_DEFAULT (0) to use the default PKCS5 padding
API_PAD_1ZERO to use OneAndZeroes padding
API_PAD_AX923 to use ANSIX923 padding
API_PAD_W3C to use W3C padding

Returns (VBA/C)

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

VBA Wrapper Syntax

Public Function padHexBlock(szInput As String, nBlkLen As Long, Optional nOptions As Long = 0) As String

.NET Equivalent

Cipher.Pad Method (String, CipherAlgorithm, Padding)

or 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)

Python Equivalent

static Cipher.pad_hex(datahex, alg, pad=Pad.PKCS5)

Remarks

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 of the output string. C/C++ users must add one to this value when allocating memory.

The output is always longer than the input. Only block lengths of 8 or 16 bytes are supported. For more details of the supported padding schemes, see Padding schemes for block ciphers.

Examples

An example showing the different padding schemes.

Dim strInputHex As String
Dim strOutputHex As String
Dim nOutChars As Long
Dim nBlockLen As Long

' Prepare test input 3 bytes long
strInputHex = "FFFFFF"
Debug.Print "Input data='" & strInputHex & "'"
' Find out the required number of characters in output (the same for all schemes)
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, " ")
' Add default padding (PKCS5Padding)
nOutChars = PAD_HexBlock(strOutputHex, Len(strOutputHex), strInputHex, nBlockLen, 0)
Debug.Print "Padded data='" & strOutputHex & "'"
' Add OneAndZeroesPadding
nOutChars = PAD_HexBlock(strOutputHex, Len(strOutputHex), strInputHex, nBlockLen, API_PAD_1ZERO)
Debug.Print "Padded data='" & strOutputHex & "'"
' Add ANSIX9.23 Padding
nOutChars = PAD_HexBlock(strOutputHex, Len(strOutputHex), strInputHex, nBlockLen, API_PAD_AX923)
Debug.Print "Padded data='" & strOutputHex & "'"	
Required length is 16 characters
Padded data='FFFFFF0505050505'
Padded data='FFFFFF8000000000'
Padded data='FFFFFF0000000005'

Example (VBA wrapper function)

Dim strInputHex As String
Dim strOutputHex As String
strInputHex = "FFFFFF"
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, API_PAD_1ZERO)
Debug.Print "Padded data=" & strOutputHex
Debug.Print "Unpadded   =" & padUnpadHex(strOutputHex, 8, API_PAD_1ZERO)

strOutputHex = padHexBlock(strInputHex, 8, API_PAD_AX923)
Debug.Print "Padded data=" & strOutputHex
Debug.Print "Unpadded   =" & padUnpadHex(strOutputHex, 8, API_PAD_AX923)

See Also

PAD_UnpadHex PAD_BytesBlock PAD_UnpadBytes

[Contents] [Index]

[PREV: PAD_BytesBlock...]   [Contents]   [Index]   
   [NEXT: PAD_UnpadBytes...]

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