Link to home
Start Free TrialLog in
Avatar of AdiF
AdiF

asked on

Reading Multiple Lines in a Text File

I encrypted text into a text file and I want to be able to make a kind of reader for the text file. By encrypting it again, it decrypts it. But it only decrypts the first line of the file. How would I be able to decrypt all of the lines in the text file? Here is my code:

Private Sub Command1_Click()
If Right(Text1.Text, 4) <> ".txt" Then Text1.Text = Text1.Text & ".txt"
Form1.Show

Open Text1.Text For Input As #1
        Input #1, file
        secret$ = file
        PassWord$ = "not-telling"
        Call Encrypt(secret$, PassWord$)
        Text2.Text = secret$
Close #1
End Sub
Sub Encrypt(secret$, PassWord$)
      L = Len(PassWord$)
      For X = 1 To Len(secret$)
         Char = Asc(Mid$(PassWord$, (X Mod L) - L * ((X Mod L) = 0), 1))
         Mid$(secret$, X, 1) = Chr$(Asc(Mid$(secret$, X, 1)) Xor Char)
      Next
   End Sub
Avatar of Kallik
Kallik

Open Text1.Text For Input As #1
                                            Input #1, file
                                            secret$ = file
                                            PassWord$ = "not-telling"
                                            Call Encrypt(secret$, PassWord$)
                                            Text2.Text = secret$
                                    Close #1

should read
Open Text1.Text For Input As #1
Result$=""
  do
   Input #1, InLine$
   PassWord$ = "not-telling"
   Call Encrypt(InLine$, PassWord$)
   Result$=Result$+InLine$
  loop while not EOF(1)
Close #1
Text2.Text=Result$

If your text file is small you can replace your line..

   Input #1, file

...with..

  file = Input(LOF(1), 1)


This will read the whole file into the string variable file, even multi-lined.


'Encrypt/Unencrypt a file
'
Option Explicit
'
Private Function EncryptFile(sFile As String, iKey As Integer)

    Dim iFake#
    Dim x As String * 1
     
    Dim lP As Long, z
    Dim intNum#
    intNum = FreeFile

    iFake = Rnd(-1)
    Randomize (iKey)
     
     
    lP = 1
    Open sFile For Binary As intNum
     
    While lP <= LOF(intNum)
     
    Get #intNum, lP, x
     
    z = Asc(x) + Int(Rnd * 256)
    If z > 255 Then z = z - 256
     
    x = Chr(z)
     
    Put #intNum, lP, x
     
    lP = lP + 1
     
     
    Wend
     
    Close #intNum

    MsgBox "Your file has been encrypted."

End Function


Private Function DecryptFile(sFile As String, iKey As Integer)

    Dim iFake As Integer
    Dim x As String * 1
     
    Dim lP As Long, z
    Dim intNum#
   
    intNum = FreeFile
 
    iFake = Rnd(-1)
    Randomize (iKey)
     
     
    lP = 1
    Open sFile For Binary As #intNum
     
    While lP <= LOF(intNum)
     
    Get #intNum, lP, x
     
    z = Asc(x) - Int(Rnd * 256)
    If z < 0 Then z = z + 256
     
    x = Chr(z)
     
    Put #intNum, lP, x
     
    lP = lP + 1
     
    Wend
     
    Close #intNum
   
    MsgBox "Your file has been unencrypted."
   
End Function

' <<<<   Form Event Code    >>>>


Private Sub Command1_Click()

    Call EncryptFile("a:\book1.xls", 44)

End Sub

Private Sub Command2_Click()

    Call DecryptFile("a:\book1.xls", 44)

End Sub


Avatar of AdiF

ASKER

Thanks all but I think I'll stay with Kallik's method. Although now it prints everything from all the lines into one full line.
Avatar of AdiF

ASKER

Thanks all but I think I'll stay with Kallik's method. Although now it prints everything from all the lines into one full line.
Hello adi...hope you figure it out
Avatar of AdiF

ASKER

Maybe I wasn't clear, the lines of the text file appear in the textbox all on the same line. Even though Multiline is set to true.
ASKER CERTIFIED SOLUTION
Avatar of Vbmaster
Vbmaster

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I would make an Reference to Microsoft Scripting RunTime. Then you can use following code:

Dim oObject     As New FileSystemObject
Dim oTextStream As TextStream
Dim strSecret   As String
Dim strPassword As String

Set oTextStream = oObject.OpenTextFile(Text1.Text, ForReading)

strSecret = oTextStream.ReadAll

oTextStream.Close

strPassword = "not-telling"
Call Encrypt(strSecret, strPassword)
Text2.Text = strSecret
use the following code:

Do
    Line Input #1, secret$
    PassWord$ = "not-telling"
    Call Encrypt(secret$, PassWord$)
    Text2.Text = secret$
Loop while not EOF(1)



this will but your decripted information in text2.text like you were trying in the begining
vbwayne's suggestion has the file opened binary insted of input.  No matter what method you use, you should do that.  Because with encryption, one of your charactors may encrypt to an end of file and reading with input would give you nothing after that charactor
Avatar of AdiF

ASKER

mikeTmike, I rejected your answer because it still only read the first line of the encrypted text file. But Vbmaster's worked.

Thanks anyway! =)
Avatar of AdiF

ASKER

Thanks!

Of all the people you got this to work =) :>


-- Adi