CryptoSys PKI Pro Manual

PAD_BytesBlock

Creates an input block suitably padded for encryption by a block cipher in ECB or CBC mode.

VBA/VB6 Syntax

Public Declare Function PAD_BytesBlock Lib "diCrPKI.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_BytesBlock(lpOutput(0), nOutputLen, lpInput(0), nInputLen, nBlockLen, nOptions) ' Note the "(0)" after the byte array parameters

C/C++ Syntax

long __stdcall PAD_BytesBlock(unsigned char *lpOutput, long nOutBytes, const unsigned char *lpInput, long nInputLen, long nBlkLen, long nOptions);

Parameters

lpOutput
[out] array to be filled with padded encryption block.
nOutBytes
[in] specifying the size of the output array in bytes.
lpInput
[in] array containing the plaintext bytes to be padded.
nInputLen
[in] specifying the length of the plaintext in bytes.
nBlkLen
[in] specifying the cipher block length in bytes (8 or 16).
nOptions
[in] option flags. Select one of:
PKI_PAD_DEFAULT (0) to use the default PKCS5 padding
PKI_PAD_PKCS5 to use Pkcs5Padding (same as default)
PKI_PAD_1ZERO to use OneAndZeroesPadding
PKI_PAD_AX923 to use the padding scheme described in ANSI X9.23
PKI_PAD_W3C to use the padding scheme described in W3C <https://www.w3.org/TR/xmlenc-core1/#sec-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 padBytesBlock (lpInput() As Byte, nBlkLen As Long, Optional nOptions As Long = 0) As Byte()

.NET Equivalent

Cipher.Pad Method

C++ (STL) Equivalent

static bvec_t dipki::Cipher::Pad (const bvec_t &input, Alg alg, Padding pad=Padding::Pkcs5)

Python Equivalent

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

Remarks

The output will be padded according to the convention specified. 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 longer than the input. Only block lengths of 8 or 16 bytes are supported.

Example (VBA core function)

Note the test when unpadding to cope with a zero-length byte array.

Dim abInput() As Byte
Dim abOutput() As Byte
Dim nOutputLen As Long
Dim nInputLen As Long
Dim nBlockLen As Long
Dim i As Long

' Prepare test input 5 bytes long
nInputLen = 5
ReDim abInput(nInputLen - 1)
For i = 0 To nInputLen - 1
    abInput(i) = &HFF
Next
Debug.Print "Input data=0x" & cnvHexStrFromBytes(abInput)

' Find out the required length
nBlockLen = 8
nOutputLen = PAD_BytesBlock(vbNull, 0, abInput(0), nInputLen, nBlockLen, 0)
Debug.Print "Required length is " & nOutputLen & " bytes"
' Check for error
If (nOutputLen <= 0) Then Exit Function

' Pre-dimension output
ReDim abOutput(nOutputLen - 1)

nOutputLen = PAD_BytesBlock(abOutput(0), nOutputLen, abInput(0), nInputLen, nBlockLen, 0)
Debug.Print "Padded data=0x" & cnvHexStrFromBytes(abOutput)

' Now set input as padded output and remove padding
abInput = abOutput
nInputLen = nOutputLen

' Remove padding...
' 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
nOutputLen = nInputLen
ReDim abOutput(nOutputLen - 1)
nOutputLen = PAD_UnpadBytes(abOutput(0), nOutputLen, abInput(0), nInputLen, nBlockLen, 0)
Debug.Print "Unpadded length is " & nOutputLen & " bytes"
' Check for error
If (nOutputLen < 0) Then Exit Function
' Truncate the output to the correct length
If nOutputLen > 0 Then
    ReDim Preserve abOutput(nOutputLen - 1)
Else
    ' Catch zero-length output
    abOutput = vbNullString
End If
Debug.Print "Unpadded data=0x" & cnvHexStrFromBytes(abOutput)

This should result in output as follows:

Input data=0xFFFFFFFFFF
Required length is 8 bytes
Padded data=0xFFFFFFFFFF030303
Unpadded length is 5 bytes
Unpadded data=0xFFFFFFFFFF

Example (VBA wrapper function)

Dim lpInput() As Byte
Dim lpBlock() As Byte
Dim lpUnpadded() As Byte
lpInput = cnvBytesFromHexStr("FDFDFDFDFD")
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_UnpadBytes PAD_HexBlock PAD_UnpadHex

[Contents] [Index]

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

Copyright © 2004-24 D.I. Management Services Pty Ltd. All rights reserved. Generated 2024-09-23T07:52:09Z.