如何为密钥验证系统添加文件上传提示?

时间:2016-12-01 09:36:17

标签: java

我需要一些指示,告诉用户如何提示用户输入密码的密钥文件。我已经为此工作了几天,这是我的大学决赛项目,我认为这是一个伟大的Java初学者步入安全加密,因为如果我继续建立这个程序,我可以创建一系列密钥来获取一个主文件。赞赏的想法,我将在成功登录之前添加一个提示,询问用户是否确定他们想要在某个时间登录。

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

2 个答案:

答案 0 :(得分:0)

您需要传递用户的输入才能阅读File for Scanner。我已经调整了几行代码来执行此操作。看看这是否有帮助。

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

答案 1 :(得分:0)

你想在swing包中使用FileChooser,在这种情况下使用JFileChooser

来自文档

  

JFileChooser为用户提供了一种选择文件的简单机制。有关使用JFileChooser的信息,请参阅“Java教程”中的“如何使用文件选择器”一节。   以下代码弹出用户主目录的文件选择器,该目录只能看到.jpg和.gif图像:

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

使用FileChooser,您将获得路径。将文件的路径传递给扫描仪,然后继续使用现有代码。有关技术可能性的完整指南可在Oracle: How to Use File Choosers

上找到

希望这有帮助。