Encrypts/decrypts a file using specified block cipher algorithm and mode.
VB6/VBA
Debug.Print "Testing CIPHER_File ..." 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
Output
Testing CIPHER_File ... IV=33BE3982D3537B8AD891E703F88AE902 CIPHER_File(ENCRYPT) returns 0 CIPHER_File(DECRYPT) returns 0
VB.NET
Console.WriteLine("Testing CIPHER_File ...")
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 Integer
' 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 = Cnv.FromHex("fedcba9876543210fedcba9876543210")
' Create the IV at random
''ReDim abIV(PKI_BLK_AES_BYTES - 1)
'abIV = Rng.Bytes(Tdea.BlockSize) ' WRONG!
abIV = Rng.Bytes(Cipher.BlockBytes(CipherAlgorithm.Aes128)) ' OR ``Rng.Bytes(16)``
' Display the IV (this needs to be communicated separately to the recipient)
Console.WriteLine("IV=" & Cnv.ToHex(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.FileEncrypt(strFileEnc, strFileIn, abKey, abIV, CipherAlgorithm.Aes128, Mode.CTR)
Console.WriteLine("CIPHER_File(ENCRYPT) returns " & nRet)
' Now decrypt it
nRet = Cipher.FileDecrypt(strFileChk, strFileEnc, abKey, abIV, CipherAlgorithm.Aes128, Mode.CTR)
Console.WriteLine("CIPHER_File(DECRYPT) returns " & nRet)
Remarks
[Contents]