Removes the padding from an encryption block.
Public Declare Function PAD_UnpadBytes Lib "diCryptoSys.dll" (ByRef lpOutput As Byte, ByVal nOutputLen As Long, ByRef lpInput As Byte, ByVal nInputLen As Long, ByVal nBlockLen As Long, ByVal nOptions As Long) As Long
nRet = PAD_UnpadBytes(lpOutput(0), nOutputLen, abInput(0), nInputLen, nBlockLen, 0)
' Note the "(0)" after the byte array parameters
long __stdcall PAD_UnpadBytes(unsigned char *lpOutput, long nOutputLen, const unsigned char *lpInput, long nBytes, long nBlkLen, long nOptions);
If successful, the return value is the number of bytes in the output; otherwise it returns a negative error code.
Public Function padUnpadBytes
(lpInput() As Byte, nBlkLen As Long, Optional nOptions As Long = 0) As Byte()
Cipher.Unpad Method (Byte[], CipherAlgorithm, Padding)
or use the method associated with the relevant encryption algorithm class:
Aes128.Unpad Method (Byte[])
Blowfish.Unpad Method (Byte[])
Des.Unpad Method (Byte[])
Tdea.Unpad Method (Byte[])
static Cipher.unpad(data, alg, pad=Pad.PKCS5)
The padding is expected according to the convention in
PKCS#5, PKCS#7 and CMS.
If nOutBytes is set to zero or lpOutput set to 0 (or NULL
in C or ByVal 0&
in VBA),
then the required number of bytes will be returned. The output is always shorter 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.
Note that it is still possible to have a "valid" padding string at the end of incorrectly deciphered data - just by coincidence - so you may get a "valid" result from using the Unpad function, but the decrypted text is garbage.
See the example in PAD_BytesBlock
.
Dim lpInput() As Byte Dim lpBlock() As Byte Dim lpUnpadded() As Byte lpInput = cnvBytesFromHexStr("FFFFFFFFFF") Debug.Print "Input data = 0x" & cnvHexStrFromBytes(lpInput) lpBlock = padBytesBlock(lpInput, 8, 0) Debug.Print "Padded data = 0x" & cnvHexStrFromBytes(lpBlock) ' Unpad lpUnpadded = padUnpadBytes(lpBlock, 8, 0) Debug.Print "Unpadded data = 0x" & cnvHexStrFromBytes(lpUnpadded) ' Special corner case - output is the empty string lpBlock = cnvBytesFromHexStr("0808080808080808") Debug.Print "Padded data = 0x" & cnvHexStrFromBytes(lpBlock) lpUnpadded = padUnpadBytes(lpBlock, 8, 0) Debug.Print "Unpadded data = 0x" & cnvHexStrFromBytes(lpUnpadded)
PAD_BytesBlock
PAD_HexBlock
PAD_UnpadHex