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)
long __stdcall PAD_UnpadHex(char *szOutput, long nMaxChars, const char *szInput, long nBlkLen, long nOptions);
If successful, the return value is the number of characters in the output string; otherwise it returns a negative error code.
Public Function padUnpadHex
(szInput As String, nBlkLen As Long, Optional nOptions As Long = 0) As String
Cipher.Unpad Method (String, CipherAlgorithm, Padding)
or 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)
static Cipher.unpad_hex(datahex, alg, pad=Pad.PKCS5)
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 more details of the supported padding schemes, see Padding schemes for block ciphers.
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.
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.
See the example in PAD_HexBlock
.
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)
Dim strInputHex As String Dim strOutputHex As String strInputHex = "FFFFFFFFFF030303" Debug.Print "Hex input =" & strInputHex strOutputHex = padUnpadHex(strInputHex, API_BLK_TDEA_BYTES, 0) Debug.Print "Unpadded =" & strOutputHex ' Output is empty string strInputHex = "0808080808080808" Debug.Print "Hex input =" & strInputHex strOutputHex = padUnpadHex(strInputHex, API_BLK_TDEA_BYTES, 0) Debug.Print "Unpadded =" & strOutputHex ' Bad input data results in the same data being returned strInputHex = "FFFFFFFFFFFFFFFF" ' Invalid! Debug.Print "Hex input =" & strInputHex strOutputHex = padUnpadHex(strInputHex, API_BLK_TDEA_BYTES, 0) Debug.Print "Unpadded =" & strOutputHex If Len(strOutputHex) = Len(strInputHex) Then Debug.Print "DECRYPTION ERROR (<= expected)" End If
PAD_HexBlock
PAD_BytesBlock
PAD_UnpadBytes