CryptoSys API Library Manual

PAD_UnpadHex

PAD_UnpadHex removes the padding from a hex-encoded encryption block.

VB6/VBA Syntax

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

Parameters

strOutput
[out] String to receive the hexadecimal-encoded output.
nMaxChars
[in] Long specifying the maximum number of characters in strOutput.
strInput
[in] String containing the hexadecimal-encoded padded data.
nBlockLen
[in] Long specifying the cipher block length in bytes (8 or 16).
nOptions
[in] Long option flags: not used in this release. Specify zero.

C/C++ Syntax

long _stdcall PAD_UnpadHex(char *szOutput, long nOutChars, const char *szInput, long blklen, long options);

Returns (VB6/C)

Long: 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.Unpad Method (String)
Blowfish.Unpad Method (String)
Des.Unpad Method (String)
Tdea.Unpad Method (String)

Remarks

The padding is expected 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 shorter than the input. Only block lengths of 8 or 16 bytes are supported. Note that it is a valid result for strOutput to be an empty string - see the example below for a technique to deal with that.

Example

A wrapper function:

Public Function unpadHexString(strInputHex As String, nBlockLen As Long) As String
' Strips padding from a hex string.
' Returns unpadded hex string or, on error, the original input string
' -- we do this because an empty string is a valid result.
' To check for error: a valid output string is *always* shorter than the input.

    Dim nOutChars As Long
    Dim strOutputHex As String
    
    ' 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
        ' Return unchanged input to indicate error
        unpadHexString = strInputHex
        Exit Function
    End If
    
    ' Re-dimension the output to the correct length
    strOutputHex = Left$(strOutputHex, nOutChars)
    Debug.Print "Unpadded data='" & strOutputHex & "'"
    
    unpadHexString = strOutputHex
    
End Function

Using the wrapper function:

    Dim strInputHex As String
    Dim strOutputHex As String
    strInputHex = "FFFFFFFFFF030303"
    Debug.Print "Input data= '" & strInputHex & "'"
    strOutputHex = unpadHexString(strInputHex, API_BLK_TDEA_BYTES)
    Debug.Print "Result=     '" & strOutputHex & "'"
    strInputHex = "0808080808080808"
    Debug.Print "Input data= '" & strInputHex & "'"
    strOutputHex = unpadHexString(strInputHex, API_BLK_TDEA_BYTES)
    Debug.Print "Result=     '" & strOutputHex & "'"
    ' Bad input data results in the same data being returned
    strInputHex = "FFFFFFFFFFFFFFFF"
    Debug.Print "Input data= '" & strInputHex & "'"
    strOutputHex = unpadHexString(strInputHex, API_BLK_TDEA_BYTES)
    Debug.Print "Result=     '" & strOutputHex & "'"
    If Len(strOutputHex) = Len(strInputHex) Then
        Debug.Print "DECRYPTION ERROR"
    End If

This should result in output as follows:

Input data= 'FFFFFFFFFF030303'
Result=     'FFFFFFFFFF'
Input data= '0808080808080808'
Result=     ''
Input data= 'FFFFFFFFFFFFFFFF'
Result=     'FFFFFFFFFFFFFFFF'
DECRYPTION ERROR

See Also

PAD_HexBlock PAD_BytesBlock PAD_UnpadBytes

[Contents] [Index]

[HOME]   [NEXT: PBE_Kdf2...]

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