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
LVL 2
AdiFAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

KallikCommented:
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$

0
VbmasterCommented:
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.

0
vbWayneCommented:

'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


0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

AdiFAuthor Commented:
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.
0
AdiFAuthor Commented:
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.
0
hessCommented:
Hello adi...hope you figure it out
0
AdiFAuthor Commented:
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.
0
VbmasterCommented:
Input will read characters to the next comma, new-line and some other characters, this means that if you use the Input statement you will loose all those characters because VB thinks they are not important to you, they only work as a divider for the values. You can use "Line Input" instead of Input to be sure to fetch the whole line, and add vbCrLf (the new-line character) after every read line... like this...

  Do
    Line Input #1, InLine$                       '<-- using Line Input
    PassWord$ = "not-telling"
    Call Encrypt(InLine$, PassWord$)
    Result$=Result$ & InLine$ & vbCrLf  '<-- adding vbCrLf (new-line)
  Loop while not EOF(1)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jimkanCommented:
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
0
mikeTmikeCommented:
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
0
moterkCommented:
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
0
AdiFAuthor Commented:
mikeTmike, I rejected your answer because it still only read the first line of the encrypted text file. But Vbmaster's worked.

Thanks anyway! =)
0
AdiFAuthor Commented:
Thanks!

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


-- Adi
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.