NumaNuma NumaNuma - 3 days ago 5
Java Question

How do I add file upload prompt for my key verification system?

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")));
}
}

Answer

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")));
    }
}
Comments