Creates an input block suitably padded for encryption by a block cipher in ECB or CBC mode.
Public Declare Function PAD_BytesBlock Lib "diCryptoSys.dll" (ByRef abOutput As Byte, ByVal nOutputLen As Long, ByRef abInput As Byte, ByVal nInputLen As Long, ByVal nBlockLen As Long, ByVal nOptions As Long) As Long
nRet = PAD_BytesBlock(abOutput(0), nOutputLen, abInput(0), nInputLen, nBlockLen, 0)
Byte array to be filled with padded encryption block.Long specifying the size of the output array in bytes.Byte array containing the plaintext bytes to be padded.Long specifying the length of the plaintext in bytes.Long specifying the cipher block length in bytes (8 or 16).Long option flags: not used in this release. Specify zero.
long _stdcall PAD_BytesBlock(unsigned char *output, long outlen, const unsigned char *input, long inlen, long blklen, long options);
Long: If successful, the return value is the number of bytes in the output;
otherwise it returns a negative error code.
Use the method associated with the relevant encryption algorithm class:
Aes128.Pad Method (Byte[])
Blowfish.Pad Method (Byte[])
Des.Pad Method (Byte[])
Tdea.Pad Method (Byte[])
The output will be padded according to the convention in
PKCS#5, PKCS#7 and CMS.
If abOutput is set to vbNull or nOutputLen set to zero,
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.
VB6/VBA users: Note the '(0)' in abOutput(0) and abInput(0).
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 ' Re-dimension the output to the correct length ReDim Preserve abOutput(nOutputLen - 1) 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
PAD_UnpadBytes
PAD_HexBlock
PAD_UnpadHex