Sha-1算法在同一输入上提供不同的输出

时间:2014-06-29 11:50:46

标签: java sha1

public void actionPerformed(ActionEvent e) {

        char[] input = aPField[0].getPassword();
        String pw = new String(input);

        a.add(aField[0].getText().trim());
        a.add(pw);

我正在尝试使用Java设置登录。我有这个加密方法,当用户注册或尝试登录时,我用它来加密密码。问题是算法使用相同的输入字符串给我一个不同的输出。

即 输入:“测试”

输出:

1)b108a24a44571380fc8e3a56832a8ac4143b3676

2)2d214a90133f9dce60554c611d07f82a38fa6a82

3)1b2eead2ae63c66e243fd27491385f4a94cd3e7a

public void actionPerformed(ActionEvent e){

    ArrayList a = new ArrayList();



    if (e.getActionCommand().equals("login")) {

        a.add(aField[0].getText());
        a.add(aPField[0].getPassword());
        if(box.getState()){

            cf.configWriter("username", aField[0].getText() );

        }


        boolean answer = fc.Login(a, "login");

    }

private boolean login(ArrayList a){

    boolean answer = false;

    String username = (String) a.get(0);
    String password = String.valueOf(a.get(1));

    int id = core.login(username, password);


    return answer;

}

protected Integer login(String nomeUtente,String passwordUtente){

    String pw = cryptPassword(passwordUtente);


    ResultSet rs = null;

    Statement stmt = null;

    String query = "SELECT ID FROM login where Username ='" + nomeUtente
            + "' AND Password = '" + pw + "'";

    int result = -1;

    try {

        stmt = conn.createStatement();
        rs = stmt.executeQuery(query);
        rs.next();
        result = rs.getInt("ID");

    } catch (SQLException e) {

        e.printStackTrace();

    }
    return result;

}

private String cryptPassword(String password){

    MessageDigest mDigest = null;
    try {
        mDigest = MessageDigest.getInstance("SHA1");
    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    byte[] result = mDigest.digest(password.getBytes());
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < result.length; i++) {
        sb.append(Integer.toString((result[i] & 0xff) + 0x100, 16)
                .substring(1));
    }

    String digest = sb.toString();

    System.out.println(digest);

    return digest;
}

我正在通过其他方法发送用户名和通行证,因为我在使用前控制器和其他一些东西之前直接进入通过加密器。

ActionPerformed-&GT; FrontController.loginrequest-&GT; LoginController.Login-&GT; DbCore.Login-&GT; DbCore.cryptPassword

1 个答案:

答案 0 :(得分:1)

简单地使用

String password = "Test";
String sha = DigestUtils.sha1Hex(password);

你的cryptPassword(字符串密码)函数实现的缺席