Title | Read and write encrypted files using Xor |
Keywords | cryptography, Xor, file, encrypt, decrypt, code, decode, cipher, decipher |
Categories | Files and Directories, Software Engineering |
|
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
|