DES_File encrypts or decrypts a file
using a specified mode. The key and initialization vector
are passed as arrays of bytes.
Public Declare Function DES_File Lib "diCryptoSys.dll"
(ByVal strFileOut As String, ByVal strFileIn As String,
ByRef abKey As Byte, ByVal bEncrypt As Boolean,
ByVal strMode As String, ByRef abInitV As Byte) As Long
nRet = DES_File(strFileOut, strFileIn, abKey(0), bEncrypt, strMode, abInitV(0))
String with the full path name of the output
file to be created.String with the full path name of the input file
to be processed.Byte array containing the key.Boolean direction flag:
set as True to encrypt or False
to decrypt.String specifying the confidentiality mode:Byte array containing the initialization vector (IV),
or zero (0) for ECB mode.
long _stdcall DES_File(const char *lpszFileOut, const char *lpszFileIn,
const unsigned char *key, int bEncrypt,
const char *lpszMode, const unsigned char *iv);
Long: If successful, the return value is 0;
otherwise it returns a non-zero error code.
Des.FileEncrypt Method (String, String, Byte[], Mode, Byte[])
Des.FileDecrypt Method (String, String, Byte[], Mode, Byte[])
The key abKey must be exactly 8 bytes long.
The initialization vector
byte array abInitV must be exactly the block size of 8 bytes long,
except for ECB mode, where it is ignored (use 0).
The output file strFileOut will be overwritten without warning.
If there is an error [new in version 3.3], the output file will not exist.
The input and output filepaths must not be the same.
In ECB and CBC modes, a padding string will be added or assumed according to the method outlined in Section 6.3 of
[CMS],
which is the same as the padding method in [PKCS7] and [PKCS5].
Const MY_PATH As String = "C:\Test\"
Dim abKey() As Byte
Dim strFileOut As String, strFileIn As String, strFileChk As String
Dim nRet As Long
' Construct full path names to files
strFileIn = MY_PATH & "hello.txt"
strFileOut = MY_PATH & "hello.des.enc.dat"
strFileChk = MY_PATH & "hello.des.chk.txt"
' Create the key as an array of bytes
' This creates an array of 8 bytes {&HFE, &HDC, ... &H10}
abKey = cnvBytesFromHexStr("fedcba9876543210")
' Encrypt plaintext file to cipher
' Output file = 16-byte ciphertext file hello.enc
nRet = DES_File(strFileOut, strFileIn, abKey(0), ENCRYPT, "ECB", 0)
Debug.Print nRet
' Now decrypt it
nRet = DES_File(strFileChk, strFileOut, abKey(0), DECRYPT, "ECB", 0)
Debug.Print nRet