java—获取同一密码的不同哈希值(+salt)

dfddblmv  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(342)

我正试图得到一个散列密码下面的代码在这里。从这里,我现在只使用 salt 方法 hash 方法和 isExpectedPassword 方法。
我从文本字段获取密码:

char[] passCharArray = txtPassword.toString().toCharArray();

然后我调用这个类来获得一个salt值(我称之为 Encryptor 而不是 Passwords 就像在原来的帖子里):

byte[] salt = Encryptor.getNextSalt();

然后我得到散列密码:

byte[] hashedPass = Encryptor.hash(passCharArray, salt);

我使用以下代码打印结果以查看发生了什么,并对结果进行了注解:

String saltString = Arrays.toString(salt);
System.out.println("SALT: " + saltString);
//SALT: [18, 117, -98, 41, 92, 124, 118, 17, 107, 14, 0, -81, 110, 70, 10, 42]

String hashedPassString = Arrays.toString(hashedPass);
System.out.println("HASHED PASS: " + hashedPassString);
//HASHED PASS: [44, -127, -43, 84, 40, -16, -46, -71, 109, -44, -41, 47, -61, -119, 21, 99, -23, 101, -13, 116, -12, 118, -66, 44, 104, 5, 4, 18, -55, 47, 59, 116]

System.out.println("Passwords match: " + Encryptor.isExpectedPassword(passCharArray, salt, hashedPass));
//Passwords match: false

下面两个是 System.out.print 我把它放在 isExpectedPassword 方法来查看一个人在被调用时有哪些值。

//Encryptor pwdHash: [-103, -87, 53, -75, 59, 11, 77, 116, 123, 59, 68, -35, 16, -68, 42, 34, -32, 75, 22, -94, -37, -26, 16, 20, 7, -46, -6, -20, -88, 104, -121, 77]
//Encryptor expectedHash: [44, -127, -43, 84, 40, -16, -46, -71, 109, -44, -41, 47, -61, -119, 21, 99, -23, 101, -13, 116, -12, 118, -66, 44, 104, 5, 4, 18, -55, 47, 59, 116]

所以基本上, hashedPass (和 expectedHash )应与相同 pwdHash ,但事实并非如此。我不明白我做错了什么。我的代码有什么遗漏吗?在我不知情的情况下有什么变化吗?
这是我的完整代码,如果人们想看到整件事以防万一:

public class Encryptor {

    private static final Random RANDOM = new SecureRandom();
    private static final int ITERATIONS = 10000;
    private static final int KEY_LENGTH = 256;

    private Encryptor(){}

    public static byte[] getNextSalt(){
        byte[] salt = new byte[16];
        RANDOM.nextBytes(salt);
        return salt;
    }

    public static byte[] hash(char[] password, byte[] salt) {
        PBEKeySpec spec = new PBEKeySpec(password, salt, ITERATIONS, KEY_LENGTH);
        Arrays.fill(password, Character.MIN_VALUE);
        try {
            SecretKeyFactory skf = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
            return skf.generateSecret(spec).getEncoded();
        } catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
            throw new AssertionError("Error while hashing a password: " + e.getMessage(), e);
        } finally {
            spec.clearPassword();
        }
    }

    public static boolean isExpectedPassword(char[] password, byte[] salt, byte[] expectedHash) {
        byte[] pwdHash = hash(password, salt);

        String s = Arrays.toString(pwdHash);
        System.out.println("Encryptor pwdHash: " + s);

        String s2 = Arrays.toString(expectedHash);
        System.out.println("Encryptor expectedHash: " + s2);

        Arrays.fill(password, Character.MIN_VALUE);
        if (pwdHash.length != expectedHash.length) return false;
        for (int i = 0; i < pwdHash.length; i++) {
            if (pwdHash[i] != expectedHash[i]) return false;
        }
        return true;
    }

}

public class Controller implements Initializable {
    @FXML
    private Button btnLogin;
    //Some private variables

    @FXML
    private AnchorPane ancPane;
    @FXML
    private ImageView imgLogo;
    @FXML
    private Hyperlink hplRegister;
    @FXML
    private TextField txtUsername;
    @FXML
    private TextField txtPassword;

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        //Some styling

        hplRegister.setOnAction(event -> {
            //Registering event
        });

        btnLogin.setOnAction(event -> {
            try {

                //Loading fxml data
                // I've put the code here just for testing purposes
                // and will not be the final placement.

                char[] passCharArray = txtPassword.toString().toCharArray();

                byte[] salt = Encryptor.getNextSalt();
                byte[] hashedPass = Encryptor.hash(passCharArray, salt);

                String saltString = Arrays.toString(salt);
                System.out.println("SALT: " + saltString);

                String hashedPassString = Arrays.toString(hashedPass);
                System.out.println("HASHED PASS: " + hashedPassString);

                System.out.println("Passwords match: " + Encryptor.isExpectedPassword(passCharArray, salt, hashedPass));

            }catch (Exception e){
                e.printStackTrace();
            }
        });
    }

    //Some getter methods.
}
91zkwejq

91zkwejq1#

问题是你正在删除密码。当您散列密码时,您将清除char数组,并用空格填充它,这将清除 passCharArray . 当您第二次传递它时,它基本上是检查密码的散列和空白数组的散列。这些肯定不匹配。
在实际情况下,您将从db或其他来源获得salt和hash密码。传入的版本在传递给之前不会被散列,因此也不会被清除 isExpectedPassword

相关问题