我正在做一个项目,我正在为员工创建一个身份验证程序,以便登录动物园。登录尝试限制为3.用户名和密码存储在credentials.txt文件中。如果登录正确,则应显示第4列中的相应文本文件。哈希将使用已经提供的MD5进行转换,只需粘贴到代码中即可。
griffin.keyes 108de81c31bf9c622f76876b74e9285f "alphabet soup" zookeeper
rosario.dawson 3e34baa4ee2ff767af8c120a496742b5 "animal doctor" admin
bernie.gorilla a584efafa8f9ea7fe5cf18442f32b07b "secret password" veterinarian
donald.monkey 17b1b7d8a706696ed220bc414f729ad3 "M0nk3y business" zookeeper
jerome.grizzlybear 3adea92111e6307f8f2aae4721e77900 "grizzly1234" veterinarian
bruce.grizzlybear 0d107d09f5bbe40cade3de5c71e9e9b7 "letmein" admin
我遇到的问题是当我使用正确的用户名和密码时,它仍显示为错误的名称和密码。我无法弄清楚我是否错误地插入了MD4消息摘要或者是否有其他错误。我没有太多经验,但听到了我的代码。
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Scanner;
public class AuthenticationSystem {
public static void main(String[] args) throws FileNotFoundException, IOException, NoSuchAlgorithmException {
Scanner scnr;
scnr = new Scanner(new File("/Users/milanpatel/NetBeansProjects/AuthenticationSystem/src/authenticationsystem/credentials.txt"));
String credentials [][]= new String[100][4];
int count = 0;
while (scnr.hasNextLine()) {
String line = scnr.nextLine();
credentials[count][0] = line.substring(0, 20).trim();
credentials[count][1] = line.substring(20, 55).trim();
credentials[count][2] = line.substring(55, 74).trim();
credentials[1][3] = line.substring(74).trim();
count++;
}
Scanner input = new Scanner(System.in);
boolean run = true;
int tries = 0;
while (run) {
System.out.println("-Welcome-");
System.out.println("1-Login");
System.out.println("2-Exit");
int ch = Integer.parseInt(input.nextLine().trim());
if (ch == 1) {
//increment number of attempts
tries++;
//request username and password
System.out.print("Enter Username: ");
String username = input.nextLine();
System.out.print("Enter Password: ");
String password = input.nextLine();
//generate hash
String original = "letmein"; //Replace "password" with the actual password inputted by the user
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(original.getBytes());
byte[] digest = md.digest();
StringBuilder sb = new StringBuilder();
for (byte b : digest) {
sb.append(String.format("%02x", b & 0xff));
}
String hPassword = sb.toString();
boolean badUser = true;
for (int i = 0; i < count; i++) {
if (username.contentEquals(credentials[i][0])) {
if (hPassword.contentEquals(credentials[i][1])) {
//if verified, logged in
List<String> data = null;
//check type of user and print
switch (credentials[i][3]) {
case "zookeeper":
data = Files.readAllLines(Paths.get("zookeeper.txt"), Charset.defaultCharset());
break;
case "admin":
data = Files.readAllLines(Paths.get("admin.txt"), Charset.defaultCharset());
break;
case "veterinarian":
data = Files.readAllLines(Paths.get("veterinarian.txt"), Charset.defaultCharset());
break;
default:
break;
}
if (data != null) {
for (String s : data) {
System.out.println(s);
}
}
//reset
tries = 0;
System.out.println("\n1) Logout.");
System.out.println("2) Exit.");
ch = Integer.parseInt(input.nextLine().trim());
if (ch == 2) {
run = false;
}
badUser = false;
break;
}
}
}
if (badUser) {
System.out.println("Invalid Username or password.");
}
} else {
break;
}
//limit attempts
if (tries == 3) {
run = false;
System.out.println("You have exceeded the number of login attempts.");
}
}
}
}
答案 0 :(得分:0)
在您的credentials
矩阵中,您只拥有最后读取的凭据,因为您始终会覆盖已输入的凭据,而无需在矩阵中继续使用。
移动
int count = 0;
来自你的while循环内部(你可以在scnr.hasNextLine()
处)。
您将有一个编译错误,因为您已经定义了一个int count = 0;
,因此要么删除重新定义,只需重置计数或定义一个不同的变量,这样就不会意外地误用它。
正如Erwin Bolwidt在评论中所说,您需要在本地IDE中调试您的应用程序,因为这是我看到的第一个问题,可能会有更多问题。
<强>更新强>:
在第二次运行时,我注意到您正在使用count
进行循环,以检查用户是否为badUser
。
建议:将int count = 0;
保留在当前名称中,并且不要声明第二个变量,只需重复使用之前创建的变量,并在执行循环时使用它。这意味着从这一行中删除第二个int count = 0;
:
boolean badUser = true;
int count = 0;
然后离开
boolean badUser = true;
for
循环后面的for (int i = 0; i < count; i++) {
循环应该可以正常工作。
我没有关于您的计划的所有信息,但我继续并成功地登录了#34;,除了您阅读与我的关联的.txt
文件的部分#34;作用&#34;
更新2 :从以下位置更改此行:
credentials[1][3] = line.substring(74).trim();
到
credentials[count][3] = line.substring(74).trim();
您的其他问题与您始终针对original
变量创建md5密码而不是使用输入的内容有关:
更改以下行:
String password = input.nextLine();
//generate hash
String original = "letmein"; //Replace "password" with the actual password inputted by the user
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(original.getBytes());
到此:
String password = input.nextLine();
//generate hash
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(password.getBytes());
byte[] digest = md.digest();
以下是整个计划:
public class AuthenticationSystem {
public static void main(String[] args) throws FileNotFoundException, IOException, NoSuchAlgorithmException {
Scanner scnr;
scnr = new Scanner(new File("/Users/milanpatel/NetBeansProjects/AuthenticationSystem/src/authenticationsystem/credentials.txt"));
String credentials[][] = new String[100][4];
int count = 0;
while (scnr.hasNextLine()) {
String line = scnr.nextLine();
credentials[count][0] = line.substring(0, 20).trim();
credentials[count][1] = line.substring(20, 55).trim();
credentials[count][2] = line.substring(55, 74).trim();
credentials[count][3] = line.substring(74).trim();
count++;
}
Scanner input = new Scanner(System.in);
boolean run = true;
int tries = 0;
while (run) {
System.out.println("-Welcome-");
System.out.println("1-Login");
System.out.println("2-Exit");
int ch = Integer.parseInt(input.nextLine().trim());
if (ch == 1) {
//increment number of attempts
tries++;
//request username and password
System.out.print("Enter Username: ");
String username = input.nextLine();
System.out.print("Enter Password: ");
String password = input.nextLine();
//generate hash
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(password.getBytes());
byte[] digest = md.digest();
StringBuilder sb = new StringBuilder();
for (byte b : digest) {
sb.append(String.format("%02x", b & 0xff));
}
String hPassword = sb.toString();
boolean badUser = true;
for (int i = 0; i < count; i++) {
if (username.contentEquals(credentials[i][0])) {
if (hPassword.contentEquals(credentials[i][1])) {
//if verified, logged in
List<String> data = null;
//check type of user and print
switch (credentials[i][3]) {
case "zookeeper":
data = Files.readAllLines(Paths.get("zookeeper.txt"), Charset.defaultCharset());
break;
case "admin":
data = Files.readAllLines(Paths.get("admin.txt"), Charset.defaultCharset());
break;
case "veterinarian":
data = Files.readAllLines(Paths.get("veterinarian.txt"), Charset.defaultCharset());
break;
default:
break;
}
if (data != null) {
for (String s : data) {
System.out.println(s);
}
}
//reset
tries = 0;
System.out.println("\n1) Logout.");
System.out.println("2) Exit.");
ch = Integer.parseInt(input.nextLine().trim());
if (ch == 2) {
run = false;
}
badUser = false;
break;
}
}
}
if (badUser) {
System.out.println("Invalid Username or password.");
}
} else {
break;
}
//limit attempts
if (tries == 3) {
run = false;
System.out.println("You have exceeded the number of login attempts.");
}
}
}
}