Sub FileEncrypt() Code:
Sub FileEncrypt()
'
' This module encrypts the selected file using either
' DES or TDEA as selected by the user on frmMain
'
Dim OutFile As Integer
Dim count As Long
Dim loopcount As Long
Dim Total As Double
Dim Percentage As Long
Dim InBuff() As Byte
Dim FileHeader As EncryptHeader
Dim outfilename As String
Dim extension As String
'
Total = 0 ' Total number of bytes processed
'
' Open the input file
'
FileNum = FreeFile
Open filename For Binary As FileNum
OutFile = FreeFile
Randomize
'
' Generate a random number file extension (0-999)
'
extension = Str(Int(Rnd * 1000))
'
' Create the output file name
'
outfilename = "enc$$." & LTrim(extension)
'
' Open the output file
'
Open outfilename For Binary As OutFile
'
' Determine the number of whole buffers that are in
' the file = number of loops
'
loopcount = LOF(FileNum) \ BUFF_SIZE%
frmGauge.Caption = "Encrypting "
frmGauge!Label1.Caption = "0%"
frmGauge.Refresh
processing = True
'
' Disable the main form controls
'
Call DisableMain '--> disable frmMain
'
' Show the Gauge form
'
frmGauge.Left = frmMain.Left + 50
frmGauge.Top = frmMain.Top + 50
frmGauge.Show
'
' Create the file header
'
Call InitHeader(FileHeader)
'
' Output the file header
'
Put #OutFile, , FileHeader
'
' Set the TDEScipher/DLL properties based on user's selection
'
DESstatus = DESReset(DEScontext)
DESstatus = DESSetBlocksize(DEScontext, 8)
DESstatus = DESSetCipherKey(DEScontext, keybytes(0))
If HDRENCTYPE = 1 Then '--> user selected DES
strEncType = "DES"
DESstatus = DESSetCipherMode(DEScontext, MODE_ECB) ' Use ECB Mode
Else '--> user selected TDEA
strEncType = "TDEA"
DESstatus = DESSetCipherMode(DEScontext, MODE_TECB)
DESstatus = DESSetCipherKey2(DEScontext, key2bytes(0))
DESstatus = DESSetCipherKey3(DEScontext, key3bytes(0))
End If
If loopcount > 0 Then
'
' At least one whole buffer to process
'
ReDim InBuff(0 To BUFF_SIZE - 1)
For count = 1 To loopcount
'
' Read a buffer of data from input file
'
Get #FileNum, , InBuff
'
' Encrypt the buffer
'
DESstatus = DESEncrypt(DEScontext, InBuff(0), InBuff(0), BUFF_SIZE)
'
' Update the number of bytes processed
'
Total = Total + BUFF_SIZE%
If Not processing Then
GoTo GetOut '--> user selected "Cancel" from Gauge form
End If
'
' Update the gauge
'
Percentage = Total * 100 / LOF(FileNum)
frmGauge!Label1.Caption = Percentage & "%"
frmGauge.Refresh
'
' Output the encrypted buffer
'
Put #OutFile, , InBuff
If Not processing Then
GoTo GetOut
End If
Next count
End If
'
' Determine how many eight-byte blocks are left in input file
'
count = LOF(FileNum) Mod BUFF_SIZE%
count = count \ 8
count = count * 8
If count > 0 Then
'
' There is at least one eight-byte block in input file, so...
' Read a buffer from the input file
'
ReDim InBuff(0 To count - 1)
Get #FileNum, , InBuff
'
' Encrypt buffer
'
DESstatus = DESEncrypt(DEScontext, InBuff(0), InBuff(0), count)
If Not processing Then
GoTo GetOut
End If
Total = Total + count
Percentage = Total * 100 / LOF(FileNum)
frmGauge!Label1.Caption = Percentage & "%"
frmGauge.Refresh
'
' Output the buffer
'
Put #OutFile, , InBuff
If Not processing Then
GoTo GetOut
End If
End If
'
' If there are any remaining bytes in input file, handle them as
follows:
'
count = LOF(FileNum) Mod 8
If count > 0 Then
'
' There is at least one byte left, so read the remaining byte(s)
'
ReDim InBuff(0 To count - 1)
Get #FileNum, , InBuff
'
' Set the TDEScipher/DLL Properties to allow single
' byte input blocks (use CFB or TCFB mode)
DESstatus = DESSetCipherKey(DEScontext, keybytes(0))
If HDRENCTYPE = 1 Then
DESstatus = DESSetCipherMode(DEScontext, MODE_CFB)
Else
DESstatus = DESSetCipherMode(DEScontext, MODE_TCFB)
DESstatus = DESSetCipherKey2(DEScontext, key2bytes(0))
DESstatus = DESSetCipherKey3(DEScontext, key3bytes(0))
End If
'
' The same value as used for the key is used here for
' simplicity - you must use different values for the key and
' initialization vector
'
DESstatus = DESSetInitVector(DEScontext, keybytes(0))
DESstatus = DESSetBlocksize(DEScontext, 1)
'
' Encrypt the buffer
'
DESstatus = DESEncrypt(DEScontext, InBuff(0), InBuff(0),
count)
If Not processing Then
GoTo GetOut
End If
Total = Total + count
Percentage = Total * 100 / LOF(FileNum)
frmGauge!Label1.Caption = Percentage & "%"
frmGauge.Refresh
'
' Output the encrypted buffer
'
Put #OutFile, , InBuff
End If
GetOut:
Close FileNum
Close OutFile
Call EnableMain
If processing Then
'
' Replace the original file with the encrypted file, and ...
' set the frmMain controls to the appropriate values
'
FileCopy outfilename, filename
frmMain!btnDecrypt.Enabled = True
frmMain!btnEncrypt.Enabled = False
frmMain!btnTDEncrypt.Enabled = False
fileencrypted = True
Beep
Else
frmMain!btnDecrypt.Enabled = False
frmMain!btnTDEncrypt.Enabled = True
frmMain!btnEncrypt.Enabled = True
End If
'
' Delete the output file
'
Kill outfilename
frmGauge!Label1.Caption = ""
processing = False
'
' Outa' here
'
Unload frmGauge
Exit Sub
End Sub
|