CryptoSys API examples VB6 to VB.NET

BLF_Update

Carries out the Blowfish transformation function on a byte array according to the direction and mode set up by an earlier call to BLF_Init or BLF_InitHex.

VB6/VBA

Debug.Print "Testing BLF_Update ..."
' Contrived example to encrypt sample blocks in CBC mode
Dim abBlock(7) As Byte
Dim abKey() As Byte
Dim nKeyLen As Long
Dim abInitV() As Byte
Dim hContext As Long
Dim nRet As Long
Dim i As Integer
Dim j As Integer

' Specify a key and its length
abKey = cnvBytesFromHexStr("FEDCBA9876543210")
nKeyLen = UBound(abKey) - LBound(abKey) + 1

' And an IV (length is assumed to be 8 bytes)
abInitV = cnvBytesFromHexStr("0123456789abcdef")

' Initialise the context
hContext = BLF_Init(abKey(0), nKeyLen, ENCRYPT, "CBC", abInitV(0))
If hContext = 0 Then
    nRet = BLF_InitError()
    Debug.Print "BFL_Init Failed: " & apiErrorLookup(nRet)
    Exit Sub
End If

Debug.Print "KY=", cnvHexStrFromBytes(abKey)
Debug.Print "IV=", cnvHexStrFromBytes(abInitV)

' Create some test blocks and encrypt them
For i = 1 To 4
    For j = 0 To 7
        abBlock(j) = CByte(i)
    Next
    Debug.Print "PT(" & i & ")=", cnvHexStrFromBytes(abBlock)
    nRet = BLF_Update(hContext, abBlock(0), 8)
    Debug.Print "CT(" & i & ")=", cnvHexStrFromBytes(abBlock)
Next

' Clear the context
BLF_Final (hContext)

Output

Testing BLF_Update ...
KY=   FEDCBA9876543210
IV=   0123456789ABCDEF
PT(1)=0101010101010101
CT(1)=46733BCD9C72C5E3
PT(2)=0202020202020202
CT(2)=F434DA62B6869A06
PT(3)=0303030303030303
CT(3)=2AE6DFE458559138
PT(4)=0404040404040404
CT(4)=DEEBF97D6F83A5F3

VB.NET

Console.WriteLine("Testing BLF_Update ...")
' Contrived example to encrypt sample blocks in CBC mode
Dim abBlock(7) As Byte
Dim abKey() As Byte
''Dim nKeyLen As Integer
Dim abInitV() As Byte
''Dim hContext As Integer
Dim nRet As Integer
Dim i As Integer
Dim j As Integer
Dim oBlowfish As Blowfish = Blowfish.Instance()

' Specify a key and its length
abKey = Cnv.FromHex("FEDCBA9876543210")
''nKeyLen = UBound(abKey) - LBound(abKey) + 1

' And an IV (length is assumed to be 8 bytes)
abInitV = Cnv.FromHex("0123456789abcdef")

' Initialise the context
nRet = oBlowfish.InitEncrypt(abKey, Mode.CBC, abInitV)
If nRet <> 0 Then
    nRet = oBlowfish.ErrCode
    Console.WriteLine("BFL_Init Failed: " & General.ErrorLookup(nRet))
    Exit Sub
End If

Console.WriteLine("KY=" & " " & Cnv.ToHex(abKey))
Console.WriteLine("IV=" & " " & Cnv.ToHex(abInitV))

' Create some test blocks and encrypt them
For i = 1 To 4
    For j = 0 To 7
        abBlock(j) = CByte(i)
    Next
    Console.WriteLine("PT(" & i & ")=" & " " & Cnv.ToHex(abBlock))
    abBlock = oBlowfish.Update(abBlock)
    Console.WriteLine("CT(" & i & ")=" & " " & Cnv.ToHex(abBlock))
Next

' Clear the context
oBlowfish.Dispose()

[Contents]

[HOME]   [NEXT: BLF_UpdateHex...]

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