Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleRead and write encrypted files using Xor
Keywordscryptography, Xor, file, encrypt, decrypt, code, decode, cipher, decipher
CategoriesFiles and Directories, Software Engineering
 
Thanks to Marc van Steijn.

Xor the plaintext letters and the keyword letters to make the ciphertext. This version is nice because it can handle non-printable characters.

 
Private Sub cmdCrypt_Click()
Dim ihl As Integer

    ihl = FreeFile
    Open txtFname For Binary Access Write As #ihl
        'Write the length of the string to the file as an
        ' integer
        Put #ihl, , CInt(Len(txtText.Text))

        'Write the content of the textbox encrypted to the
        ' file
        Put #ihl, , CryptString(txtText.Text, _
            txtPassword.Text)
    Close #ihl
End Sub

Private Sub cmdRead_Click(Index As Integer)
Dim ihl As Integer
Dim itLength As Integer
Dim tBuffer As String

    'Get an handle
    ihl = FreeFile
    Open txtFname For Binary Access Read As #ihl
        'Read de textlength
        Get #ihl, , itLength

        'Create a buffer big enough to read the text
        tBuffer = String$(itLength, 32)

        'Read the text
        Get #ihl, , tBuffer

        'Do we want to encrypt it
        If Index = 1 Then
            tBuffer = CryptString(tBuffer, txtPassword.Text)
        End If

        'Let me see wat we read
        txtResult.Text = tBuffer
    Close #ihl
End Sub

' Call this function to encrypt and decrypt.
Public Function CryptString(ptSource As String, ptPassword _
    As String) As String
Dim tdest As String
Dim lteller As Long
Dim lPasswTeller As Long

   tdest = ptSource
   For lteller = 1 To Len(ptSource)
      lPasswTeller = lPasswTeller - 1
      If lPasswTeller < 1 Then lPasswTeller = _
          Len(ptPassword)

      Mid$(tdest, lteller, 1) = _
         Chr$(Asc(Mid$(ptSource, lteller, 1)) Xor _
         Asc(Mid$(ptPassword, lPasswTeller, 1)))
   Next lteller
   CryptString = tdest
End Function
 
For information on serious encryption routines, see Bruce Schneier's book "Applied Cryptography" (Amazon.com - Amazon.co.uk)
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated