PAD_UnpadHex removes the padding from a hex-encoded encryption block.
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)
String to receive the hexadecimal-encoded output.Long specifying the maximum number of characters in strOutput.String containing the hexadecimal-encoded padded data.Long specifying the cipher block length in bytes (8 or 16).Long option flags: not used in this release. Specify zero.
long _stdcall PAD_UnpadHex(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.Unpad Method (String)
Blowfish.Unpad Method (String)
Des.Unpad Method (String)
Tdea.Unpad Method (String)
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.
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
PAD_HexBlock
PAD_BytesBlock
PAD_UnpadBytes