?
Solved

Reading Multiple Lines in a Text File

Posted on 1999-12-29
13
Medium Priority
?
232 Views
Last Modified: 2010-05-02
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
0
Comment
Question by:AdiF
13 Comments
 
LVL 1

Expert Comment

by:Kallik
ID: 2313510
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
 
LVL 7

Expert Comment

by:Vbmaster
ID: 2313550
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
 
LVL 1

Expert Comment

by:vbWayne
ID: 2313571

'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 new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
LVL 2

Author Comment

by:AdiF
ID: 2313603
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
 
LVL 2

Author Comment

by:AdiF
ID: 2313633
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
 

Expert Comment

by:hess
ID: 2313777
Hello adi...hope you figure it out
0
 
LVL 2

Author Comment

by:AdiF
ID: 2313905
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
 
LVL 7

Accepted Solution

by:
Vbmaster earned 280 total points
ID: 2314421
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
 

Expert Comment

by:jimkan
ID: 2314564
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
 
LVL 1

Expert Comment

by:mikeTmike
ID: 2315145
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
 

Expert Comment

by:moterk
ID: 2315148
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
 
LVL 2

Author Comment

by:AdiF
ID: 2315199
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
 
LVL 2

Author Comment

by:AdiF
ID: 2315202
Thanks!

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


-- Adi
0

Featured Post

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question