CryptoSys API examples VB6 to VB.NET

AES128_File

Encrypts or decrypts a file using a specified mode.

VB6/VBA

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

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

' Convert key to byte array
abKey = cnvBytesFromHexStr("0123456789ABCDEFF0E1D2C3B4A59687")
' Encrypt plaintext file to cipher
nRet = AES128_File(strFileOut, strFileIn, abKey(0), ENCRYPT, "ECB", 0)
If nRet <> 0 Then
    Debug.Print "Error " & nRet & ": " & apiErrorLookup(nRet)
End If

' Now decrypt it
nRet = AES128_File(strFileChk, strFileOut, abKey(0), DECRYPT, "ECB", 0)
If nRet <> 0 Then
    Debug.Print "Error " & nRet & ": " & apiErrorLookup(nRet)
End If

Output

Testing AES128_File ...

VB.NET

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

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

' Convert key to byte array
abKey = Cnv.FromHex("0123456789ABCDEFF0E1D2C3B4A59687")
' Encrypt plaintext file to cipher
nRet = Aes128.FileEncrypt(strFileOut, strFileIn, abKey, Mode.ECB, Nothing)
If nRet <> 0 Then
    Console.WriteLine("Error " & nRet & ": " & General.ErrorLookup(nRet))
End If

' Now decrypt it
nRet = Aes128.FileDecrypt(strFileChk, strFileOut, abKey, Mode.ECB, Nothing)
If nRet <> 0 Then
    Console.WriteLine("Error " & nRet & ": " & General.ErrorLookup(nRet))
End If

[Contents]

[HOME]   [NEXT: AES128_FileHex...]

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