0

I need some instruction on how to make it prompt the user for the a key file with the password in it. I've been working on this for a few days, it's for my college finals project and I think it's a great Java beginner step into security encryption, because if I keep building on this one program, I could create a whole series of keys just to get to one main file. Ideas appreciated, and I am going to add a prompt before successful login that asks if the user is sure they want to log in at some point.

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.Scanner;  // Needed for the Scanner class

public class TxtKeyVerifier {

public static void main(String[] args) throws FileNotFoundException {

    File keyfile = new File("key2.txt");


    Scanner sc = new Scanner(keyfile);


    Scanner keyboard = new Scanner(System.in); //<<<---
    String input; 

    System.out.print("Please enter your password: "); //<<---
    input = sc.nextLine();

if (authenticate1(input)) {

        System.out.println("This program is working if this text is found within outputfile.txt.");

        File outputfile = new File("outputfile.txt");
        FileOutputStream fos = new FileOutputStream(outputfile);
        PrintStream ps = new PrintStream(fos);
        System.setOut(ps);
        System.out.println("This program is working if this text is found within outputfile.txt.");

}else if (authenticate2(input)) {

        System.out.println("It works.");

}else{
System.out.println("Error: Wrong password.");
}
}

private static boolean authenticate1(String password1) {

    return ((password1.length() == 6)
            && (password1.matches("beep11"))
            && (password1.matches("beep11"))
            && (password1.matches("beep11")));
}

private static boolean authenticate2(String password2) {

            return ((password2.length() == 6)
            && (password2.matches("beep22"))
            && (password2.matches("beep22"))
            && (password2.matches("beep22")));
}
}
NumaNuma
  • 39
  • 4
  • Not sure what you are asking? You want to have a user input with the file path? – Murat Karagöz Dec 01 '16 at 09:40
  • Yes, I want the user input to be a .txt file with my password in it. – NumaNuma Dec 01 '16 at 09:41
  • Something like this http://stackoverflow.com/questions/3309899/how-to-pass-a-text-file-as-a-argument or http://stackoverflow.com/questions/13185727/reading-a-txt-file-using-scanner-class-in-java – Murat Karagöz Dec 01 '16 at 09:45
  • Murat K. I want the program to prompt the user with a "pick a file" window, the user then picks the .txt file with the password in it, the scanner reads it, and then completes the process. – NumaNuma Dec 01 '16 at 09:50

2 Answers2

0

You will need to pass the user's input to read the File for Scanner. I've tweaked a few lines of your code to do this. See if this helps.

public class TxtKeyVerifier {

    public static void main(String[] args) throws FileNotFoundException {

    Scanner keyboard = new Scanner(System.in);
    JFileChooser fileChooser = new JFileChooser(".");
    fileChooser.showOpenDialog(null);
    File keyfile = fileChooser.getSelectedFile();

        Scanner sc = new Scanner(keyfile);
        String input = sc.nextLine();

        if (authenticate1(input )) {

            System.out.println("This program is working if this text is found within outputfile.txt.");

            File outputfile = new File("outputfile.txt");
            FileOutputStream fos = new FileOutputStream(outputfile);
            PrintStream ps = new PrintStream(fos);
            System.setOut(ps);
            System.out.println("This program is working if this text is found within outputfile.txt.");

        } else if (authenticate2(input)) {

            System.out.println("It works.");

        } else {
            System.out.println("Error: Wrong password.");
        }
        sc.close();
        keyboard.close();
    }

    private static boolean authenticate1(String password1) {

        return ((password1.length() == 6) && (password1.matches("beep11")) && (password1.matches("beep11"))
                && (password1.matches("beep11")));
    }

    private static boolean authenticate2(String password2) {

        return ((password2.length() == 6) && (password2.matches("beep22")) && (password2.matches("beep22"))
                && (password2.matches("beep22")));
    }
}
anacron
  • 5,733
  • 2
  • 21
  • 31
  • Great answers, I even really liked the answer that was before your edit, Anacron. The login via typed key file path is a pretty good start at designing an actual encrypted login system. – NumaNuma Dec 01 '16 at 10:15
  • Haha.. I saw that you posted this in one of your comments: 'prompt the user with a "pick a file" window,'. So, I edited the answer. __:)__ Do you want me to revert to the previous answer? – anacron Dec 01 '16 at 10:43
0

You want to use a FileChooser and in this case JFileChooser from the swing package.

From the docs

JFileChooser provides a simple mechanism for the user to choose a file. For information about using JFileChooser, see How to Use File Choosers, a section in The Java Tutorial. The following code pops up a file chooser for the user's home directory that sees only .jpg and .gif images:

JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
    "JPG & GIF Images", "jpg", "gif");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(parent);
if(returnVal == JFileChooser.APPROVE_OPTION) {
   System.out.println("You chose to open this file: " +
        chooser.getSelectedFile().getName());
}

With the FileChooser you will get the path. Pass the path of the file to your scanner and proceed with your existing code. A full guide on technical possibilities can be found on the Oracle: How to Use File Choosers

Hope this helps.

Murat Karagöz
  • 26,294
  • 9
  • 70
  • 97