CryptoSys API examples VB6 to VB.NET

PAD_BytesBlock

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

VB6/VBA

Debug.Print "Testing PAD_BytesBlock ..."
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 Sub

' 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 Sub

' Re-dimension the output to the correct length
ReDim Preserve abOutput(nOutputLen - 1)
Debug.Print "Unpadded data=0x" & cnvHexStrFromBytes(abOutput)

Output

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

VB.NET

Console.WriteLine("Testing PAD_BytesBlock ...")
Dim abInput() As Byte
Dim abOutput() As Byte
''Dim nOutputLen As Integer
Dim nInputLen As Integer
''Dim nBlockLen As Integer
Dim i As Integer

' Prepare test input 5 bytes long
nInputLen = 5
ReDim abInput(nInputLen - 1)
For i = 0 To nInputLen - 1
    abInput(i) = &HFF
Next
Console.WriteLine("Input data=0x" & Cnv.ToHex(abInput))

' [VB.NET] In .NET, if we know the cipher algorithm, we can pad directly
abOutput = Tdea.Pad(abInput)
Console.WriteLine("Padded data=0x" & Cnv.ToHex(abOutput))

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

' Remove padding...
abOutput = Tdea.Unpad(abInput)
Console.WriteLine("Unpadded data=0x" & Cnv.ToHex(abOutput))

[Contents]

[HOME]   [NEXT: PAD_HexBlock...]

Copyright © 2009-20 D.I. Management Services Pty Ltd. All rights reserved.