EnDeCryptFile

Sub EnDeCryptFile(InFile As String, OutFile As String, fEncrypt As Boolean, ByVal KeyValue As String)

Dim hInFile As Long, hOutFile As Long, Blocks As Long, LastBlockSize As Long
Dim CurBlockSize As Long, i As Long, Buffer As String, Buffer2 As String, KeyValue2 As String

'//Perform encryption on strings of size <BLOCKSIZE> for efficiency
'//WARNING: because of key cycling, a file that has been ENcoded with a block size of
'// X, can NOT be DEcoded using any other block size!
Const BLOCKSIZE = 8192

'//Remove output file if it exists; initialize input and output files
If Len(Dir(OutFile)) Then Kill OutFile
hOutFile = FreeFile
Open OutFile For Binary As #hOutFile

hInFile = FreeFile
Open InFile For Binary As #hInFile

'//Calculate number of blocks, regular and last block sizes
Blocks = LOF(hInFile) \ BLOCKSIZE
CurBlockSize = BLOCKSIZE
LastBlockSize = LOF(hInFile) Mod BLOCKSIZE

'//Perform operation on each block in the file
For i = 1 To Blocks + 1
If i = Blocks + 1 Then CurBlockSize = LastBlockSize
Buffer = Space(CurBlockSize)
Buffer2 = ""

If Len(Buffer) Then '//If file size MOD block size = 0, last block will be empty...
Get #hInFile, , Buffer

If fEncrypt Then
SimpleCrypt Buffer, Buffer2, KeyValue
Else
SimpleCrypt Buffer2, Buffer, KeyValue
End If

Put #hOutFile, , Buffer2

'//Modify key value for next block, based on block number
SimpleCrypt KeyValue, KeyValue2, Hex(i) & Format(i) & Oct(i)
KeyValue = KeyValue2
End If
Next

Close #hInFile, #hOutFile

End Sub

 

[Home] [Publications] [Code] [Contact Us] [Legal]