Class: Encryption in Category General VBA/VB6 : Encryption and Compression from Total Visual SourceBook

Encrypt and decrypt files and strings with Unicode support using XOR password encryption in VBA and VB6.

The password is also encrypted as the string or file is processed, providing an additional level of security. For simple string encryption, consider using modEncryption instead.

Procedure Name Type Description
(Declarations) Declarations Declarations and private variables for the CEncryption class.
InputFileName Property Get the name of the input file.
OutputFileName Property Get the name of the output file.
Password Property Get the password.
Class_Initialize Initialize Set initial values to defaults which may be overridden with property settings.
EncryptFile Method Encrypt the file specified in m_strInputFileName to the file specified in m_strOutputFileName. This procedure uses buffered file reads and writes for better performance. The encrypted file produced by the method is reversible. This means that if you encrypt a file, reverse the InputFileName and OutputFileName properties, and call this method again, the original file is restored.
Note: The same password must be supplied to successfully decrypt the file. This method also raises an event called file progress. This event can be used to track the progress of the file being processed. See the example tab for an example of how to use this event. This method also uses a file buffer. Using a file buffer significantly speeds up the performance of this method.
EncryptString Method Encrypt/Decrypt the passed string with XOR encryption. The encrypted string produced by the method is reversible. This means that if you encrypt a string, then encrypt the results of that operation, the original string is restored.
Note: The same password must be supplied to successfully decrypt the string. See the example tab for a demonstration of this. If you need an ASCII version of this function, use the EncryptStringAscii method.
EncryptStringAscii Method Encrypt/Decrypt the passed string with XOR encryption, returning the result in ASCII format. The encrypted string produced by the method is reversible. This means that if you encrypt a string, then encrypt the results of that operation, the original string is restored.
Note: The same password must be supplied to successfully decrypt the string. See the example tab for a demonstration of this. The EncryptStringAscii method returns the result in ASCII format. If you need to pass the results of the EncryptString function on the command line, or use it external to your program, it is recommended that you use the ASCII version of this function.
EncryptByte Private Encrypt one byte, and modify the password. Modifying the password as we encrypt makes the encryption slightly harder to break.
ReadFile Private Read the specified number of bytes from the file. This function significantly increases the speed of processing files. The alternative to using a file buffer is reading a byte at a time from the file.
' Example of CEncryption
'
' To try this example, do the following:
' 1. Create a new form
' 2. Create a command button named 'cmdTest'
' 3. Paste all the code from this example to the new form's module.
' 4. Run the form

' This example assumes that the sample files are located in the folder named by the following constant.
Private Const mcstrSamplePath As String = "C:\TVSBSamp"

Private WithEvents mclsEncryption As CEncryption

Private Sub cmdTest_Click()
  ' Comments: Examples of encrypting and unencrypting files and strings

  Const cstrOriginal As String = mcstrSamplePath & "\sample.txt"
  Const cstrEncrypt As String = mcstrSamplePath & "\encrypt.enc"
  Const cstrDecrypt As String = mcstrSamplePath & "\deencrypt.txt"
  Const cstrExample As String = "One small step for [a] man; one giant leap for mankind"

  Dim dblCompressionRatio As Double
  Dim strString As String

  ' Initialize class
  Set mclsEncryption = New CEncryption

  mclsEncryption.Password = "password"

  ' Example of encrypting a file
  mclsEncryption.InputFileName = cstrOriginal
  mclsEncryption.OutputFileName = cstrEncrypt
  mclsEncryption.EncryptFile

  ' Display the size change with encryption achieved
  dblCompressionRatio = FileLen(cstrEncrypt) / FileLen(cstrOriginal)
  MsgBox "File is " & Format$(dblCompressionRatio, "Percent") & " of its original size"

  ' Decrypt the file into a new one. Notice the same EncryptFile procedure is used to convert the encrypted file back to its unencrypted state.
  mclsEncryption.InputFileName = cstrEncrypt
  mclsEncryption.OutputFileName = cstrDecrypt
  mclsEncryption.EncryptFile
  MsgBox cstrDecrypt & " created by decrypting " & cstrEncrypt

  ' Example of Unicode encrypting and decrypting strings. Results in the Immediate Window.
  Debug.Print "Original String: " & cstrExample

  strString = mclsEncryption.EncryptString(cstrExample)
  Debug.Print "Encrypted String: " & strString

  strString = mclsEncryption.EncryptString(strString)
  Debug.Print "Un-Encrypted String: " & strString

  ' Example of ASCII encrypting and decrypting strings. Results in the Immediate Window.
  strString = mclsEncryption.EncryptStringAscii(cstrExample)
  Debug.Print "ASCII Encrypted String: " & strString

  strString = mclsEncryption.EncryptStringAscii(strString)
  Debug.Print "ASCII Un-Encrypted String: " & strString

  Set mclsEncryption = Nothing

End Sub

Private Sub mclsEncryption_FileProgress(dblPercentage As Double)
  ' Comments: This procedure captures the Progress Event and reveals the percentage completion for the file that's being compressed
  '           This example puts the information on the Immediate Window. You could show the information on a form as text or display a graphic.
  ' Params  : dblPercentage       Percent of completion between 0 and 1

  Debug.Print "Percent done: " & Format$(dblPercentage, "Percent")
End Sub

Total Visual SourceBook The source code in Total Visual Sourcebook includes modules and classes for Microsoft Access, Visual Basic 6 (VB6), and Visual Basic for Applications (VBA) developers. Easily add this professionally written, tested, and documented royalty-free code into your applications to simplify your application development efforts.

Total Visual SourceBook is written for the needs of a developer using a source code library covering the many challenges you face. Countless developers over the years have told us they learned some or much of their development skills and tricks from our code. You can too!

Additional Resources

Total Visual SourceBook CD and Printed Manual

Microsoft Access/ Office 2016, 2013, 2010, and 2007 Version
is Shipping!

New features in Total Visual SourceBook for Access, Office and VB6

Supports Access/Office 2016, 2013, 2010 and 2007, and Visual Basic 6.0!


View all FMS products for Microsoft Access All Our Microsoft Access Products

Reviews

Reader Choice Award for MS Access Source Code Library
Reader Choice

"The code is exactly how I would like to write code and the algorithms used are very efficient and well-documented."

Van T. Dinh, Microsoft MVP

SourceBook Info

Additional Info

Question

 

 

Free Product Catalog from FMS