如何使用MD5转换密码并根据有效凭据检查用户凭据?

时间:2016-10-01 18:10:30

标签: java hash login md5

我对Java很新。我必须编写一个程序,从用户那里获取用户名和密码,使用MD5转换密码,然后验证用户名和密码。用户有3次机会输入正确的信息。一旦它是正确的,我需要显示欢迎信息。如果输入错误信息3次,程序需要退出。我甚至不知道从哪里开始。

我收到了一个包含MD5信息的文件。我还获得了一个文件,其中包含所有用户信息以及散列密码。

这些是用户凭据:

griffin.keyes   108de81c31bf9c622f76876b74e9285f    "alphabet soup" 

rosario.dawson  3e34baa4ee2ff767af8c120a496742b5    "animal doctor"

bernie.gorilla  a584efafa8f9ea7fe5cf18442f32b07b    "secret password"

我提供的MD5是:

import java.security.MessageDigest;

public class MD5Digest {

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

        //Copy and paste this section of code
        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();
        StringBuffer sb = new StringBuffer();
        for (byte b : digest) {
            sb.append(String.format("%02x", b & 0xff));
        }
        //End copy/paste

        System.out.println("original:" + original);
        System.out.println("digested:" + sb.toString()); //sb.toString() is what you'll need to compare password strings
    }

}

我也应该把我的程序分成两个单独的方法,但这是我现在最不担心的问题。我已经在这方面工作了一个星期了,我还没有走得太远。任何帮助都会非常感激。这就是我所拥有的

import java.security.MessageDigest;
import java.util.Scanner;

class CredAndPass {
    public static void CredAndPass() throws Exception {
        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();
        StringBuffer sb = new StringBuffer();
        for (byte b : digest) {
            sb.append(String.format("%02x", b & 0xff));
        }
    }
}
public class AuthSystem {

    public static void main(String[] args) throws Exception {
        int i;
        String username = "";
        String password = "";
        for (i = 0; i <= 2; ++i){
            Scanner scnr = new Scanner(System.in);
            System.out.println("Enter username:");
            username = scnr.next();
            System.out.println("Enter password:");
            String password1 = scnr.next();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

首先,我们改进CredAndPass.CredAndPass以获取给定MD5

String哈希值
class CredAndPass {
    public static String CredAndPass(String original) throws Exception {
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(original.getBytes());
        byte[] digest = md.digest();
        // Note here that I use a StringBuilder instead of a StringBuffer
        // as it is not meant to be shared so no need to use a thread safe
        // builder of String
        StringBuilder sb = new StringBuilder(32);
        for (byte b : digest) {
            sb.append(String.format("%02x", b & 0xff));
        }
        // Returns the result
        return sb.toString();
    }
}

然后我们稍微修改main方法的逻辑,在离开之前检查3次,如果不成功的话

public static void main(String[] args) throws Exception {
    // A Map for testing purpose representing the login and hash of your existing users
    Map<String, String> credentials = new HashMap<>();
    credentials.put("griffin.keyes", "108de81c31bf9c622f76876b74e9285f");
    credentials.put("rosario.dawson", "3e34baa4ee2ff767af8c120a496742b5");
    credentials.put("bernie.gorilla", "a584efafa8f9ea7fe5cf18442f32b07b");
    // Variable used to know if it was successful or not
    boolean success = false;
    BufferedReader scnr = new BufferedReader(new InputStreamReader(System.in));
    String username = null;
    // Iterate up to 3 times
    for (int i = 0; i < 3; ++i){
        System.out.println("Enter username:");
        username = scnr.readLine();
        System.out.println("Enter password:");
        String password1 = scnr.readLine();

        // Get the hash for the given username
        String hash = credentials.get(username);
        // Check if the username exists and if so check if the hash of his 
        // password matches with the hash of the provided password
        if (hash == null || !hash.equals(CredAndPass.CredAndPass(password1))) {
            // We don't have a match so we keep going
            System.err.println("User or password invalid");
            continue;
        }
        success = true;
        // Exit from the loop as we have a success
        break;
    }
    if (success) {
        System.out.printf("Welcome %s!%n", username);
    } else {
        System.out.println("Could not connect");
    }
}