[VB6/VBA6] Encrypted I/O, RC4-VBForums
Results 1 to 2 of 2

Thread: [VB6/VBA6] Encrypted I/O, RC4

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Feb 2006
    Posts
    10,422

    [VB6/VBA6] Encrypted I/O, RC4

    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

    Code:
    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.
    Attached Files Attached Files
    Last edited by dilettante; Nov 12th, 2010 at 04:35 PM.

  2. #2

    Thread Starter
    PowerPoster
    Join Date
    Feb 2006
    Posts
    10,422

    Re: [VB6/VBA6] Encrypted I/O, RC4

    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.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.