【发布时间】:2014-06-16 20:16:06
【问题描述】:
我正在创建一个基于基本密码的登录系统。它使用 MD5 来保护密码。正确的密码是“csk”(不带引号)。如果有人正确输入,他就可以访问本地计算机中的 key.html 文件。但如果有人连续三次输入错误的密码,他就会被“禁止”再次登录。但是我所构建的设计只禁止该特定会话的用户。如果他再次打开终端,它会从头开始。如果变量计数自上次以来大于 3(三),则程序在通过 void main() 执行时将显示“您被禁止”。我想保持基本,不使用 JDBC 和 SQL 等。此外,这是一个本地应用程序,而不是基于 Web 的应用程序。我很困惑我应该采取什么方法。这是我编写的代码:
import java.math.*;
import java.security.*;
import java.io.*;
import java.util.*;
import java.net.HttpURLConnection;
public class pwd {
public static void main(String[] args)throws NoSuchAlgorithmException, IOException, InterruptedException {
int count = 1;
boolean run = true;
while (run && count<4){
System.out.println("Enter the password");
Scanner kb = new Scanner(System.in);
String pass = kb.nextLine();
String pd = "ea0882721f7f44384ce772375696f9a6"; //Password is "csk" without quotes geeks, this is it's MD5
// so enter "csk" in the terminal
// to run the program on execution
String md5sum = md5(pass);
String os = System.getProperty("os.name");
boolean o = false;
int win = os.indexOf("Windows");
if (md5sum.equals(pd)){
System.out.println("You've logged in successfully, get the Key now");
String url = "file:///C:/Users/<username>/Desktop/key.html"; // example www
Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);
run = false;
}
else {
System.out.println("You've entered the wrong password, try again.");
System.out.println();
run = true;
if (count>=3) {
System.out.println("You are banned from logging in, due to repeated unsuccessful login attempts.");
}
++count;
}
}
}
public static String md5(String input)throws NoSuchAlgorithmException, IOException {
String md5 = null;
MessageDigest digest = MessageDigest.getInstance("MD5");
digest.update(input.getBytes(), 0, input.length());
md5 = new BigInteger(1, digest.digest()).toString(16);
return md5;
}
}
编辑:我不需要将 MD5 散列更改为其他任何内容,这只是一个基本的散列。
【问题讨论】: