CryptoSys PKI Pro Manual

PAD_UnpadHex

Removes the padding from a hex-encoded encryption block.

VBA/VB6 Syntax

Public Declare Function PAD_UnpadHex 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_UnpadHex(strOutput, nMaxChars, strInput, nBlockLen, nOptions)

C/C++ Syntax

long __stdcall PAD_UnpadHex(char *szOutput, long nOutChars, const char *szInput, long nBlkLen, long nOptions);

Parameters

szOutput
[out] to receive the hexadecimal-encoded output.
nOutChars
[in] specifying the maximum number of characters in szOutput.
szInput
[in] containing the hexadecimal-encoded padded data.
nBlkLen
[in] specifying the cipher block length in bytes (8 or 16).
nOptions
[in] option flags. Select one of:
PKI_PAD_DEFAULT (0) to use the default PKCS5 padding
PKI_PAD_PKCS5 to use Pkcs5Padding (same as default)
PKI_PAD_1ZERO to use OneAndZeroesPadding
PKI_PAD_AX923 to use the padding scheme described in ANSI X9.23
PKI_PAD_W3C to use the padding scheme described in W3C <https://www.w3.org/TR/xmlenc-core1/#sec-Padding>

Returns (VBA/C)

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

VBA Wrapper Syntax

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

.NET Equivalent

Use the method associated with the relevant encryption algorithm class:

Cipher.Unpad Method (String, CipherAlgorithmPadding)

Python Equivalent

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

Remarks

The padding is expected according to the convention specified in nOptions. The output is always shorter than the input. Only block lengths of 8 or 16 bytes are supported. Note that it is a valid result for szOutput to be an empty string.

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.

When using the VBA wrapper function padUnpadHex and there is an error, then the input is returned unchanged. The user should check for this and signal an error. See the VBA wrapper example below.

Example (VBA core function)

See the example in PAD_HexBlock.

Example (VBA wrapper function)

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)

Dim strInputHex As String
Dim strOutputHex As String
strInputHex = "FFFFFFFFFF030303"
Debug.Print "Hex input  =" & strInputHex
strOutputHex = padUnpadHex(strInputHex, PKI_BLK_TDEA_BYTES, 0)
Debug.Print "Unpadded   =" & strOutputHex
' Output is empty string
strInputHex = "0808080808080808"
Debug.Print "Hex input  =" & strInputHex
strOutputHex = padUnpadHex(strInputHex, PKI_BLK_TDEA_BYTES, 0)
Debug.Print "Unpadded   =" & strOutputHex
' Bad input data results in the same data being returned
strInputHex = "FFFFFFFFFFFFFFFF"
Debug.Print "Hex input  =" & strInputHex
strOutputHex = padUnpadHex(strInputHex, PKI_BLK_TDEA_BYTES, 0)
Debug.Print "Unpadded   =" & strOutputHex
If Len(strOutputHex) = Len(strInputHex) Then
    Debug.Print "DECRYPTION ERROR"
End If
Debug.Assert Len(strOutputHex) = Len(strInputHex)

See Also

PAD_HexBlock PAD_BytesBlock PAD_UnpadBytes

[Contents] [Index]

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

Copyright © 2004-24 D.I. Management Services Pty Ltd. All rights reserved. Generated 2024-09-23T07:52:09Z.