*Use wordwrap to read this*

Title

CryptFile/RC4 Classes

Description

The RC4 Class implements standard RC4 and RC4-drop(n) stream encryption.

The CryptFile Class wraps the RC4 Class and VB6 native I/O to provide sequential text and binary file I/O with data encryption, as well as simple file copying with encryption/decryption.

Feature List

RC4 has a FMSSkip property that can be used to "tune" the algorithm for straight RC4 encrytion or any degree of RC4-drop(n) encryption.  It accepts a binary Key of 1 to 256 bytes, which allows you to derive your own Keys from passphrases or other sources and apply your own salts, etc.

CryptFile can create or read binary files with encryption sequentially, i.e. without any seeking or random operations.  It can also create or read encrypted text files line by line in ANSI or Unicode (UTF-16LE), with CRLF or LF newlines sequentially.  In Unicode mode the BOM is skipped on read, and on write you can select whether to write the BOM or not.  Text access does not support partial line reads or writes.

RC4 Properties:

FMSSkip

RC4 Methods:

CryptBlock, SetSecret

CryptFile Properties:

EOF, IsOpen, Loc, LOF

CryptFile Methods:

CloseFile, CryptCopy, OpenFile, ReadBytes, ReadLine, WriteBytes, WriteLine

Author

Bob Riemersma.  RC4 is based on published algorithms.

System Requirements

Should work on any system supporting VB6 programs or even VBA6 hosts.

License

This source code is released into the public domain.  There are no restrictions on its use or modification, and no warranty of support.

Discussion

RC4 can be used by itself as desired for other purposes.  Just add RC4.cls to your Project.  RC4 instances can be reused, but if you are working with multiple streams at the same time you'll need an instance for each stream being processed.

The RC4/RC4-drop(n) algorithm is symmetric.  A pass over plaintext provides encryption and a pass over ciphertext produces the original plaintext.  There is thus no need for separate encryption and decryption code.

RC4 is a well known and fairly well understood encryption algorithm.  It has known weaknesses, some of which can be remediated by using the -drop(n) technique.  However the known exploits also require tens of thousands of samples with the same Key for an effective crack.  This makes RC4 less suitable in applications like SSL or WEP but much less of a problem for encrypting files.

CryptFile is used by adding CryptFile.cls and RC4.cls to your project.  CryptFile instances can be reused, but only can be either open to a file or running CryptCopy at any given time.  For multiple open files or copying create separate instances of CryptCopy.

Reading an unencrypted file with CryptFile results in garbage, as does reading using the wrong Key value.

A file encrypted using CryptCopy can be read with ReadLine as long as the original file was a text file and you use matching parameters with OpenFile (LfNewLine and Unicode values).  A file encrypted using CryptCopy can be read using ReadBytes.

EOF works like the VBA.EOF() function in VB6 for text input.  It also works similarly to VB6 text I/O for binary input: EOF becomes True after the last data has been read.  This is unlike the behavior of VBA.EOF() when used with VB6's Get # statements, where EOF becomes true after the last read fails to return data.

The code has been tested but there may still be bugs, partcularly in the buffering of text I/O which can get tricky.

Example

Dim cfInput As New CryptFile
Dim strLine As String

With cfInput
    .OpenFile "encrypted.dat", "this is a simple passphrase", Access:=cfaReadText
    Do Until .EOF
        Debug.Print .ReadLine()
    Loop
    .CloseFile
End With

In the Archive

The attached .zip archive contains the Class modules as well as sample VB6 programs that use them.
