CryptoSys API examples VB6 to VB.NET

BLF_File

Encrypts or decrypts a file using a specified mode.

VB6/VBA

Debug.Print "Testing BLF_File ..."
Const MY_PATH As String = ".\"
Dim abKey() As Byte
Dim strFileOut As String
Dim strFileIn As String
Dim strFileChk As String
Dim nRet As Long
Dim nBytes As Long

' Construct full path names to files
strFileIn = MY_PATH & "hello.txt"
strFileOut = MY_PATH & "hello.blf.enc.dat"
strFileChk = MY_PATH & "hello.blf.chk.txt"

' Create the key as an array of bytes
' This creates an array of 8 bytes {&HFE, &HDC, ... &H10}
abKey = cnvBytesFromHexStr("fedcba9876543210")
nBytes = 8

' Encrypt plaintext file to cipher
' WARNING: output file is just clobbered
nRet = BLF_File(strFileOut, strFileIn, abKey(0), nBytes, ENCRYPT, "ECB", 0)
Debug.Print nRet
' Output file should be a 16-byte file hello.enc
' containing the following values in hexadecimal:
' 1A A1 51 B7 7A 5A 33 5C 4E 7E DC 84 A3 86 DC 96

' Now decrypt it
nRet = BLF_File(strFileChk, strFileOut, abKey(0), nBytes, DECRYPT, "ECB", 0)
Debug.Print nRet

Output

Testing BLF_File ...
 0 
 0

VB.NET

Console.WriteLine("Testing BLF_File ...")
Const MY_PATH As String = ".\"
Dim abKey() As Byte
Dim strFileOut As String
Dim strFileIn As String
Dim strFileChk As String
Dim nRet As Integer
''Dim nBytes As Integer

' Construct full path names to files
strFileIn = MY_PATH & "hello.txt"
strFileOut = MY_PATH & "hello.blf.enc.dat"
strFileChk = MY_PATH & "hello.blf.chk.txt"

' Create the key as an array of bytes
' This creates an array of 8 bytes {&HFE, &HDC, ... &H10}
abKey = Cnv.FromHex("fedcba9876543210")
''nBytes = 8

' Encrypt plaintext file to cipher
' WARNING: output file is just clobbered
nRet = Blowfish.FileEncrypt(strFileOut, strFileIn, abKey, Mode.ECB, Nothing)
Console.WriteLine(nRet)
' Output file should be a 16-byte file hello.enc
' containing the following values in hexadecimal:
' 1A A1 51 B7 7A 5A 33 5C 4E 7E DC 84 A3 86 DC 96

' Now decrypt it
nRet = Blowfish.FileDecrypt(strFileChk, strFileOut, abKey, Mode.ECB, Nothing)
Console.WriteLine(nRet)

[Contents]

[HOME]   [NEXT: BLF_FileHex...]

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