Encrypt or decrypt data in a file using the specified block cipher algorithm and mode.
@deprecated
Use CIPHER_FileEncrypt() or CIPHER_FileDecrypt() instead.
Public Declare Function CIPHER_File Lib "diCrPKI.dll"
(ByVal fEncrypt As Long, ByVal strFileOut As String, ByVal strFileIn As String,
ByRef lpKey As Byte, ByRef lpIV As Byte, ByVal strAlgAndMode As String, ByVal nOptions As Long) As Long
nRet = CIPHER_File(fEncrypt, strFileOut, strFileIn, lpKey(0),
lpIV(0), strAlgAndMode, nOptions)
long __stdcall CIPHER_File(long fEncrypt, const char *szFileOut, const char *szFileIn, const unsigned char *lpKey, const unsigned char *lpIV, const char *szAlgAndMode, long nOptions);
If successful, the return value is zero; otherwise it returns a nonzero error code.
Cipher.FileEncrypt Method
Cipher.FileDecrypt Method
The key and initialization vector are passed as bytes arrays. PKCS5 padding is used, if required. The algorithm and mode must be specified using either the szAlgAndMode or nOptions parameter (see Specifying the algorithm and mode for generic block cipher functions). The length of key lpKey must be exactly the required key size, and the length of the IV, if required, exactly the block size. See Valid key and block sizes. The output file szFileOut will be overwritten without warning. If there is an error, the output file will not exist. The input and output files 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] section 10.3 and [PKCS5] section 6.1.1 and [RFC1423] para 1.1.
Const MY_PATH As String = "" Dim abKey() As Byte Dim abIV() As Byte Dim strFileEnc 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" strFileEnc = MY_PATH & "hello.aes128.enc.dat" strFileChk = MY_PATH & "hello.aes128.chk.txt" ' Create the key as an array of bytes ' This creates an array of 16 bytes {&HFE, &HDC, ... &H10} abKey = cnvBytesFromHexStr("fedcba9876543210fedcba9876543210") ' Create the IV at random ReDim abIV(PKI_BLK_AES_BYTES - 1) Call RNG_Bytes(abIV(0), PKI_BLK_AES_BYTES, "", 0) ' Display the IV (this needs to be communicated separately to the recipient) Debug.Print "IV=" & cnvHexStrFromBytes(abIV) ' Encrypt plaintext file to ciphertext using AES-128 in counter (CTR) mode ' (This will create a file of exactly the same size as the input) nRet = CIPHER_File(ENCRYPT, strFileEnc, strFileIn, abKey(0), abIV(0), "aes128-ctr", 0) Debug.Print "CIPHER_File(ENCRYPT) returns " & nRet ' Now decrypt it nRet = CIPHER_File(DECRYPT, strFileChk, strFileEnc, abKey(0), abIV(0), "aes128-ctr", 0) Debug.Print "CIPHER_File(DECRYPT) returns " & nRet