Results 1 to 16 of 16
  1. #1
    Join Date
    Jan 2009
    Posts
    7
    Rep Power
    0

    Default Search a text file

    Hi All, I'm new to Java. I'm doing a Computer Security course and part of the course involves encryption and decryption. I was given a program which simulates a hack tool to find 6 passwords. The programs prompts for a 4 character input. It then runs this input against an algorithm and if the word is one of the passwords then it reports a successful attempt, if not then an error.

    I have to modify the program to instead of inputting words at random I want the program to search a dictionary file. The dictionary file is basically a text file containing words in the dictionary. The program should be able to take the first word in the file and run it against the decryption algorithm and return a successful or failed value. It would then move on to second word and so on..

    Any ideas on what method I should use? Below is the original code:

    /* This is the main program. */
    public static void main (String[] args) throws IOException
    {
    BufferedReader in = new BufferedReader (new InputStreamReader (System.in));
    String password = "";
    do
    {
    System.out.print ("Enter your password: ");
    password = in.readLine ();
    // continue looping until a correct password is entered
    } while (check_password (password) != true);
    }
    }
    Last edited by javanewbie1979; 02-09-2009 at 05:18 PM.

  2. #2
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    please edit your OP to use code tags like this
    XML Code:
    [code]
    codes here
    ...
    [/code]
    Last edited by angryboy; 01-28-2009 at 09:17 PM. Reason: feel like it
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  3. #3
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Just change your main to read from the text file. Use a stream that reads a line of text at a time, assuming the words are separated by new lines. Read a word, test, report the result, loop.

  4. #4
    Join Date
    Jan 2009
    Posts
    7
    Rep Power
    0

    Default

    Thanks Steve11235. I edited my main but I'm getting some errors:

    CIS3_1.java:81: illegal escape character
    File dict = new File("C:\Java\bin\list.txt");
    ^
    CIS3_1.java:81: illegal escape character
    File dict = new File("C:\Java\bin\list.txt");
    ^
    CIS3_1.java:86: 'try' without 'catch' or 'finally'
    try {
    ^
    CIS3_1.java:107: class, interface, or enum expected
    }→
    ^
    4 errors

    This is my edited version:


    /*This is the main program.*/
    public static void main(String[] args) throws IOException
    {

    File dict = new File("C:\Java\bin\list.txt");
    FileInputStream fis = null;
    BufferedInputStream bis = null;
    DataInputStream dis = null;

    try {
    fis = new FileInputStream(file);

    bis = new BufferedInputStream(fis);
    dis = new DataInputStream(bis);

    while (dis.available() !=0) {

    password=dis.readline();

    }

    fis.close();
    bis.close();
    dis.close();

    }


    }
    }
    }

  5. #5
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    double slashes like c:\\
    Java Code:
    File f = new File("c:/filename.txt");
    [I]or[/I]
    File f = new File("c:\\filename.txt");
    [I]or[/I]
    File f = new File("path","filename");
    [I]or (better, but crazy)[/I]
    File f = new File("c:"+File.separator+"filename");
    Last edited by angryboy; 01-28-2009 at 09:39 PM. Reason: feel like it
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  6. #6
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default try-catch blocks

    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  7. #7
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    AngryBoy pointed out that you have to escape a backslash with another backslash, if you want to use Windows file separators. I also believe that forward slashes will work with XP and Vista.

  8. #8
    Join Date
    Jan 2009
    Posts
    7
    Rep Power
    0

    Default

    /*This is the main program.*/

    public static void main (String[] args) {


    //--------------------------------------------< readMyFile >--------//

    String password = null;
    int recCount = 0;

    try
    {

    FileReader fr = new FileReader("c:\\list.txt");
    BufferedReader br = new BufferedReader(fr);

    password = new String();
    while ((password = br.readLine()) != null)

    {
    recCount++;

    }

    }

    catch (IOException e)

    {

    // catch possible io errors from readLine()
    System.out.println("Uh oh, got an IOException error!");
    e.printStackTrace();

    }

    } // end of main program



    }
    Last edited by javanewbie1979; 02-09-2009 at 05:18 PM.

  9. #9
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    I got the following to compile but when I try to run it nothing happens. My modifications are in red below. Any thoughts?
    huh... what are you expecting to happen? From what I can tell you added a main method... which does:
    Java Code:
    Read file
    loop
    read a line and assign to password variable
    increment a counter
    end loop
    There's no printlns (outputs) and no calls to the other class methods?

    So... what do you want it to do?

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  10. #10
    Join Date
    Jan 2009
    Posts
    7
    Rep Power
    0

    Default

    It was suppsoed to read through all the lines of the text file until it found a correct password. The list.txt file is basically a dictionary file. It's supposed to take each word and parse it in the above methods. The original program initially posted asks for an input and then takes that input and applies it to the other methods. In this case instead of asking for an input, it reads the dictionary file.

  11. #11
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default hhhmmm...

    Java Code:
    The list.txt file is basically a dictionary file. It's supposed to take each word and parse it in the above methods.
    OK... but you have to tell the program (in the main method) to do that. If you don't call the other methods from your main program, nothing is going to happen. Currently, your main method isn't calling any of the other class methods.
    I'm guessing that you would call the other methods from within the "while" statement in the main method.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  12. #12
    Join Date
    Jan 2009
    Posts
    7
    Rep Power
    0

    Default

    But how is the main method in the original code calling the other class methods?

  13. #13
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default That is not the way to answer on a forum...

    You know... posting a snotty answer on the forum and being a newbie is NOT a very good way to get help.
    If you notice your original code (which obviously you didn't) you'll see the following line:
    Java Code:
    while ([COLOR="blue"][B]check_password (password)[/B][/COLOR] != true);
    Guess what? check_password() is a method from your class !!!

    Figure the rest out yourself...

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  14. #14
    Join Date
    Jan 2009
    Posts
    7
    Rep Power
    0

    Default

    Eureka....

    Yes I eventually realised that check_password was the link.

    I modified the loop accordingly and it worked.

    do

    {
    password=br.readLine();

    }

    while

    (check_password(password)!=true);

    {
    recCount++;

    }


    Thanks again everyone for your help...

  15. #15
    Join Date
    Jan 2009
    Posts
    7
    Rep Power
    0

    Default

    Quote Originally Posted by CJSLMAN View Post
    You know... posting a snotty answer on the forum and being a newbie is NOT a very good way to get help.
    If you notice your original code (which obviously you didn't) you'll see the following line:

    CJSL
    Sorry but this was not the impression I was trying to convey. I know it was a dumb question, but I only realised it's stupidity after the post was made. I do appreciate your help. Thanks again.

  16. #16
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default OK...

    Apology accepted. Glad you found the problem.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. Search in text file
    By mark-mlt in forum New To Java
    Replies: 6
    Last Post: 04-03-2009, 05:33 PM
  2. Replies: 7
    Last Post: 11-19-2008, 03:09 PM
  3. find and replace text from a text file
    By gezzel in forum New To Java
    Replies: 2
    Last Post: 09-19-2008, 05:04 PM
  4. Replies: 2
    Last Post: 08-18-2008, 03:59 PM
  5. File Search
    By Juggler in forum New To Java
    Replies: 2
    Last Post: 07-19-2008, 05:09 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •