Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help ! How do I encrypt text file ? 2

Status
Not open for further replies.

hiptino

Programmer
May 27, 2000
18
0
0
NG
I'm currently developing an application.
The application writes users' activities to a log file (text file). To view the log file, admins will click a command button, which then fills a listbox control with the content of the log file.

But I'm afraid smart users would recognize a log file if they see the content.They could then open the text file via windows explorer and overwrite text file with
"YOU ARE A DUMMY".

Next time the admin view the log file, he would see
"YOU ARE A DUMMY".
I think encrypting the log file makes the file meaningless to the smart users.

How do I encrypt the text file on saving, and decrypt it
on filling the listbox?

Or you could suggest any other solutions.
 
I could give you a simple encryption function but I think you will enjoy this much more. It is beyond *sneaky*.

Place a bitmap in the path of your application (say "clouds.bmp") then place a text box, a command button and a timer control on a form. Set the multiline property on the text box to true and the interval on the timer to 1000 (one second). The timer is used to demonstrate how you will write to an invisible log.

I use a text box here instead of a list box because it handles the data somewhat better (in reality, I would use a Rich Text control because it has built-in methods for printing, etc.)

Use this code in the timer control: [tt]
Private Sub Timer1_Timer()
Activity$ = "The time is: " & Time$ & vbCrLf
ff = FreeFile
Open "clouds.bmp" For Binary As #ff
Put #ff, LOF(ff) + 1, Activity$
Close #ff
End Sub
[/tt]

And place this in the command button:[tt]
Private Sub Command1_Click()
BMPsize& = FileLen("clouds.bmp")
ff = FreeFile
Open "clouds.bmp" For Binary As #ff
[tab]'get the size of the original BMP
Get #ff, 3, EndOfBMP&
G$ = String$(BMPsize& - EndOfBMP&, 32)
[tab]'retrieve the contents of the log
Get #ff, EndOfBMP& + 1, G$
Close #ff
[tab]'place the log in the text box
Text1.Text = G$
End Sub
[/tt]

Simple enough, right? Run the app and then click the button after a few seconds. The text box will be populated with a list similar to this:
[tt]
The Time is: 03:57:20
The Time is: 03:57:21
The Time is: 03:57:22
The Time is: 03:57:23
[/tt]
Now close the app, open explorer and click on "clouds.bmp". Win 98 will show you a normal preview to the left. Double click on the bitmap and Paint Brush (or whatever might be the associated application) will open it. This is just a normal image file with a little hidden data tacked on to the end of it.

All this makes it very hard for a user to stumble across your log file and insert obnoxious comments.

Have fun!

VCA.gif

Alt255@Vorpalcom.Intranets.com

"What this country needs is more free speech worth listening to."[tt]
Hansell B. Duckett[/tt]​
 
Okay, that isn't what you asked for. Here is one of the simplest and most effective encryption schemes known to man:
[tt]
Public Function XORcrypt(Cry$) As String
Password$ = "bsDjkwFEQW4"
Dim PassKey As Currency
[/tt]
' Create a randomizing seed
' from the password[tt]
For Re = 1 To Len(Password$)
PassKey = PassKey + _
Asc(Mid$(Password$, Re, 1))
Next
Randomize PassKey
aa = Rnd(-1)
[/tt]
' XOR the data[tt]
For Re = 1 To Len(Cry$)
Mid$(Cry$, Re, 1) = _
Chr$(Asc(Mid$(Cry$, Re, 1)) _
Xor Int(256 * Rnd))
Next
XORcrypt = Cry$
End Function
[/tt]
You call it like this:[tt]
EncryptedText$ = XORcrypt(Text1.Text) '
[/tt]to encrypt
or[tt]
Text1.Text = XORcrypt(EncryptedText$) '
[/tt]to unencrypt.

The only problem with this approach is that it doesn't lend itself to reading and writing to text files or use in a text box. The extended characters, carriage returns and line feeds play hell with any efforts to read and write lines delimited with CrLf.

The following scheme is more suitable for your needs since it only transforms the ordinary text characters.
[tt]
Public Function Subst(Cry$) As String
[/tt]
'Build clear key consisting of
'only the normal text characters
'Chr$(32) (SPACE) to Chr$(126) "~"[tt]
For Re = 32 To 126
G$ = G$ + Chr$(Re)
Next
G1$ = G$
[/tt] 'G$ will be destroyed
'Set a password.
'Normally, this would be obtained
'through a dialog with the user.[tt]
Password$ = "bsDjkwFEQW4"
Dim PassKey As Currency
[/tt]
'Create a randomizing seed from the password[tt]
For Re = 1 To Len(Password$)
PassKey = PassKey + Asc(Mid$(Password$, Re, 1))
Next
Randomize PassKey
aa = Rnd(-1)
G2$ = String$(Len(G1$), 255)
G3$ = String$(Len(G1$), 255)
Re = 1
[/tt]
'Create a random substitution string in G2$[tt]
Do Until G$ = ""
P = Int(Len(G$) * Rnd + 1)
ip$ = Mid$(G$, P, 1)
Mid$(G2$, Re, 1) = ip$
Re = Re + 1
E = Len(G$)
Select Case P
Case 1
G$ = Right$(G$, E - 1)
Case E
G$ = Left$(G$, E - 1)
Case Else
G$ = Left$(G$, P - 1) + Right$(G$, E - P)
End Select
Loop
For Re = 1 To Len(G1$)
T1$ = Mid$(G1$, Re, 1)
L1 = 0
If InStr(G3$, T1$) < 1 Then
For Re2 = Len(G1$) To 1 Step -1
If Mid$(G3$, Re2, 1) = Chr$(255) Then
L1 = Re2
Exit For
End If
Next
Mid$(G3$, L1, 1) = T1$
T2$ = Mid$(G2$, L1, 1)
L2 = InStr(G2$, T1$)
Mid$(G3$, L2, 1) = T2$
End If
Next
[/tt]
'Perform the actual substitution encryption[tt]
For Re = 1 To Len(Cry$)
F = InStr(G2$, Mid$(Cry$, Re, 1))
If F > 0 Then
Mid$(Cry$, Re, 1) = Mid$(G3$, F, 1)
End If
Next
Subst = Cry$
End Function
[/tt]
You can call it like this:[tt]
Text1.Text = Subst(Text1.Text) '
[/tt]to encrypt
or[tt]
Text1.Text = Subst(Text1.Text) '
[/tt]to unencrypt.

Hey... That was kind of fun!

VCA.gif

Alt255@Vorpalcom.Intranets.com

&quot;What this country needs is more free speech worth listening to.&quot;[tt]
Hansell B. Duckett[/tt]​
 
I thought your first answer was interesting! I would find it very useful if I could do the same thing with a .jpg image instead. I tried using your above example but it only works with .bmp files. Is what I want impossible?

elziko
 
I don't know of any, at this point, but you might be able to create a method if you could obtain the structure of the JPG file header. Look here for additional information:
BTW, the practice of embedding data in graphics files isn't widespread but it is common enough to have a name. It's called steganography. The method works with a number of graphic formats and even other common file formats (EXE, WAV, etc). You just have to find the offset in the file header that contains the original size of the file.

Have fun.
VCA.gif

Alt255@Vorpalcom.Intranets.com

&quot;If you can get people to ask the wrong questions, they'll never find the right answers.&quot;[tt]
Thomas Pynchon[/tt]

Perhaps the reverse is also true....
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top