首頁  >  問答  >  主體

java - 關於字串編碼。

public static void main(String[] args) throws Exception {
    String str = "resource";
    System.out.println(Arrays.toString(getHash(str,"MD5").getBytes()));//使用默认解码后输出
}

public static String getHash(String str, String hashType) {
    try {
        MessageDigest digest = MessageDigest.getInstance(hashType);
        digest.reset();
        byte[] b = digest.digest(str.getBytes());
        System.out.println(Arrays.toString(b)); //编码前输出
        return new String(b); //使用默认编码
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return str;
}

輸出:
[-106, -85, 78, 22, 63, 78, -32, 58, -86, 77, 16, 81, -86, 81, -46, 4]

[-17, -65, -67, -17, -65, -67, 78, 22, 63, 78, -17, -65, -67, 58, -17, -65, -67, 77, 16, 81, -17, -65, -67, 81, -17, -65, -67, 4]

為什麼編碼前和編碼後再解碼所輸出的不一樣?

我想大声告诉你我想大声告诉你2702 天前638

全部回覆(1)我來回復

  • 黄舟

    黄舟2017-05-27 17:43:40

    你可能認為(new String(b)).getBytes().equals(b),实际上并非如此。(尽管new String(s.getBytes()).equals(s)一定是。 )

    因為byte[]转换成String时,有些字节是未必能转换成字符的,比如第一个-106、第二个-85就是,所以转换成String时前两个就变成了未知字符(表面上会显示?,但实际上是一个Unicode字符),再转成byte[](你这边defaultCharset应该是UTF-8吧),每個未知字元就變成3個位元組了。

    如果用GBK的話,情況還算好,但還是略有不同:

    [-106, -85, 78, 22, 63, 78, -32, 58, -86, 77, 16, 81, -86, 81, -46, 4]
    [-106, -85, 78, 22, 63, 78, 63, 58, -86, 77, 16, 81, -86, 81, 63, 4]
    

    所以結論是:如果用String表示一個Hash值,不能把byte[]強轉換成String,而是按慣例轉換成16進位表示

    回覆
    0
  • 取消回覆