1

我要做的是将输入的密码转换为 jPasswordField 为 SHA-256 哈希。如果我将密码保存为字符串,但我正在使用的字段正在返回 char[],我四处游荡并找到了如何做到这一点,所以我最终只是猜测该怎么做......起初我得到了即使密码相同,结果也会不同,但现在我相信我更接近了,因为它是一个常数;但它仍然不是它的输出

echo -n 'abc' | sha256sum

ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad

而我的动作的输出(对于相同的输入)是

86900f25bd2ee285bc6c22800cfb8f2c3411e45c9f53b3ba5a8017af9d6b6b05

我的动作是这样的:

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        NoSuchAlgorithmException noSuchAlgorithmException = null;
        MessageDigest messageDigest = null;
        try {
            messageDigest = MessageDigest.getInstance("SHA-256");

        } catch (NoSuchAlgorithmException ex) {
            noSuchAlgorithmException = ex;
        }
        if (noSuchAlgorithmException != null) {
            System.out.println(noSuchAlgorithmException.toString());
        }
        else {
            UnsupportedEncodingException unsupportedEncodingException = null;
            byte[] hash = null;
            char[] password = jPasswordField1.getPassword();
            StringBuffer stringBuffer = new StringBuffer();
            for (char c : password) {
                if (c > 0 && c < 16) {
                    stringBuffer.append("0");
                }
                stringBuffer.append(Integer.toHexString(c & 0xff));
            }
            String passwordString = stringBuffer.toString();
            try {
                hash = messageDigest.digest(passwordString.getBytes("UTF-8"));
            } catch (UnsupportedEncodingException ex) {
                unsupportedEncodingException = ex;
            }
            if (unsupportedEncodingException != null) {
                System.out.println(unsupportedEncodingException.toString());
            }
            else {
                stringBuffer = new StringBuffer();
                for (byte b : hash) {
                    stringBuffer.append(String.format("%02x", b));
                }
                String passwordHashed = stringBuffer.toString();
                System.out.println(passwordHashed);
            }
        }

有任何想法吗?

4

2 回答 2

1

你已经搞定了。char[]只是采取了从转换为String->的硬/错误方式new String(password)就是您所需要的。(提示,如果你发现自己在字节和字符之间手动转换,你可能做错了)。

附带说明一下,“抛出”异常是有原因的。这样可以轻松跳过引发异常时不应执行的以下代码。通过捕获异常并将其转换为“if”块,您可以使代码变得比需要的更复杂。

于 2012-05-29T16:07:33.463 回答
0

这将打印出与sha256sum

public static void main(String[] args) 
    throws NoSuchAlgorithmException, UnsupportedEncodingException {

    char[] password = new char[]{'a', 'b', 'c'};

    MessageDigest messageDigest = null;
    messageDigest = MessageDigest.getInstance("SHA-256");

    byte[] hash = null;

    // This is how you convert a char array into a String without reencoding it into a different set of characters.
    String passwordString = new String(password);

    hash = messageDigest.digest(passwordString.getBytes("UTF-8"));
    StringBuilder sb = new StringBuilder();
    for (byte b : hash) {
        sb.append(String.format("%02x", b));
    }
    String passwordHashed = sb.toString();
    System.out.println(passwordHashed);
}
于 2012-05-29T16:44:08.990 回答