# VBForums UtilityBank > UtilityBank - Components >  [VB6/VBA6] Encrypted I/O, RC4

## dilettante

*Title*

CryptFile and RC4 Classes

*Description*

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

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

*Feature List*

RC4 has an FMSSkip property that can be used to "tune" the algorithm for straight RC4 encryption 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.  CryptFile accepts a passphrase String value and performs a simple key generation operation to create the Key used with RC4.

_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.

----------


## dilettante

Ideas for enhancements:
More sophisticated key generation in CryptFile.Open text files for append.Enhance ReadBytes/WriteBytes for partial-line text I/O (read/write n characters with no newline).Forward seek for binary reading, line skip for text reading.Rewind to beginning for text and binary reading.UTF-8 text encoding support.Alternative encryption algorithms.

----------

