-5
public void actionPerformed(ActionEvent e) {

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

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

I'm trying to set up a login in Java. I've got this Encrypt method and I'm using it to crypt the password when the user registers or tries to log in. The problem is that the algorithm gives me a different output using the same input string.

I.E Input : "Test"

Output:

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

I'm sending the Username and the Pass through other method cause i've used a front controller and some other things before going directly to the pass encrypter.

ActionPerformed->FrontController.loginrequest->LoginController.Login->DbCore.Login->DbCore.cryptPassword.

2
  • 2
    Impossible to reproduce. Please post a runnable example reproducing the problem. My guess is that the inputs are not actually the same. Maybe they contain blank spaces. Also, SHA1 is by no mean an encryption algorithm. It's a Hash or Digest algorithm.
    – JB Nizet
    Jun 29, 2014 at 11:55
  • "The problem is that the alghoritm gives me a different output using the same input string" No it doesn't. For input Test, it gives output 640ab2bae07bedc4c163f679a746f7ab7fb5d1fa (which is the correct hash), every time.
    – Boann
    Jun 29, 2014 at 11:56

1 Answer 1

1

Use simply

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

inestand of your cryptPassword(String password) function implementation

1
  • Thanks, but my function works perfectly, I've fixed all already Jun 29, 2014 at 12:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.