CryptoSys API Library Manual

PAD_UnpadBytes

Removes the padding from an encryption block.

VBA/VB6 Syntax

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

C/C++ Syntax

long __stdcall PAD_UnpadBytes(unsigned char *lpOutput, long nOutputLen, const unsigned char *lpInput, long nBytes, long nBlkLen, long nOptions);

Parameters

lpOutput
[out] array to be filled with the output.
nOutputLen
[in] specifying the size of the output array in bytes.
lpInput
[in] array containing the padded data.
nBytes
[in] specifying the length of the input in bytes.
nBlkLen
[in] specifying the cipher block length in bytes (8 or 16).
nOptions
[in] Option flags. Select one of:
API_PAD_DEFAULT (0) to use the default PKCS5 padding
API_PAD_1ZERO to use OneAndZeroes padding
API_PAD_AX923 to use ANSIX923 padding
API_PAD_W3C to use W3C padding

Returns (VBA/C)

If successful, the return value is the number of bytes in the output; otherwise it returns a negative error code.

VBA Wrapper Syntax

Public Function padUnpadBytes(lpInput() As Byte, nBlkLen As Long, Optional nOptions As Long = 0) As Byte()

.NET Equivalent

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[])

Python Equivalent

static Cipher.unpad(data, alg, pad=Pad.PKCS5)

Remarks

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.

Example (VBA core function)

See the example in PAD_BytesBlock.

Example (VBA wrapper function)

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)

See Also

PAD_BytesBlock PAD_HexBlock PAD_UnpadHex

[Contents] [Index]

[PREV: PAD_HexBlock...]   [Contents]   [Index]   
   [NEXT: PAD_UnpadHex...]

Copyright © 2001-24 D.I. Management Services Pty Ltd. All rights reserved. Generated 2024-01-07T07:42:00Z.