-1
這是我的代碼,我有字節編碼的問題。當我得到純文本字符串並對其進行散列並嘗試打印結果時,它會變得混亂。例如,對於明文=「嗨」,它會打印出:hash:???????? 1?W?p ???? = ????? &問題與字符串編碼
公共類HASHME {
private String hash;
private String salt;
public HASHME(String plaintext)
{
try {
System.setProperty("file.encoding", "UTF-8");
salt = "salt";
plaintext = plaintext + salt;
byte[] bytesOfPlain = plaintext.getBytes("UTF8");
MessageDigest md = MessageDigest.getInstance("SHA-256");
byte[] hashedBytes = md.digest(bytesOfPlain);
hash = new String(hashedBytes, "UTF8");
System.out.println("hash: " + hash);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
可以打印出bytesOfPlain以及hashedBytes – CodeGuy
***你期待什麼***,它正在做你所問的它打印不可打印的字符,它不會神奇地十六進制編碼字節,這可能是你期望的,但我們不知道,只能猜測。 –
請參閱[http://stackoverflow.com/questions/9655181/convert-from-byte-array-to-hex-string-in-java](http://stackoverflow.com/questions/9655181/convert-from- byte-array-to-hex-string-in-java) –