CryptoSys PKI examples VB6 to VB.NET

TDEA_File

Encrypts/decrypts a file using Triple DES and PKCS-5/7 padding.

VB6/VBA

Debug.Print "Testing TDEA_File ..."
Const MY_PATH As String = ""
Dim aKey() As Byte
Dim strFileOut As String
Dim strFileIn As String
Dim strFileChk As String
Dim nRet As Long

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

' Create the key as an array of bytes
' This creates an array of 24 bytes {&HFE, &HDC, ... &H10}
aKey = cnvBytesFromHexStr("fedcba9876543210fedcba9876543210fedcba9876543210")

' Encrypt plaintext file to ciphertext
' Output file = 16-byte ciphertext file hello.enc
nRet = TDEA_File(strFileOut, strFileIn, aKey(0), ENCRYPT, "ECB", 0)
Debug.Print "TDEA_File(ENCRYPT) returns " & nRet; ""

' Now decrypt it
nRet = TDEA_File(strFileChk, strFileOut, aKey(0), DECRYPT, "ECB", 0)
Debug.Print "TDEA_File(DECRYPT) returns " & nRet; ""

Output

Testing TDEA_File ...
TDEA_File(ENCRYPT) returns 0
TDEA_File(DECRYPT) returns 0

VB.NET

Console.WriteLine("Testing TDEA_File ...")
Const MY_PATH As String = ""
Dim aKey() As Byte
Dim strFileOut As String
Dim strFileIn As String
Dim strFileChk As String
Dim nRet As Integer

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

' Create the key as an array of bytes
' This creates an array of 24 bytes {&HFE, &HDC, ... &H10}
aKey = Cnv.FromHex("fedcba9876543210fedcba9876543210fedcba9876543210")

' Encrypt plaintext file to ciphertext
' Output file = 16-byte ciphertext file hello.enc
nRet = Tdea.FileEncrypt(strFileOut, strFileIn, aKey, Mode.ECB, Nothing)
Console.WriteLine("TDEA_File(ENCRYPT) returns " & nRet & "")

' Now decrypt it
nRet = Tdea.FileDecrypt(strFileChk, strFileOut, aKey, Mode.ECB, Nothing)
Console.WriteLine("TDEA_File(DECRYPT) returns " & nRet & "")

[Contents]

[HOME]   [NEXT: TDEA_HexMode...]

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