[英]SHA1 in Java and PHP with different results
我知道有幾個這樣的問題,但我嘗試了我在stackoverflow上找到的每一個解決方案,但我仍然沒有得到預期的結果。
我正在嘗試在Java和PHP中將字符串轉換為sha1,但我得到了不同的結果。 該字符串是隨機生成的。 我檢查了兩端的字符串,它們是相同的(甚至嘗試在線比較工具)。
這是我在另一個應用程序中使用的相同代碼,它在那里工作,但不是在這種情況下。
我試圖用sha1散列的一個字符串是: UgJaDVYEClRUD1cAAVUBVwRTB1MDAA9SBgcDBwNXAwNZBQdUAAACBA==
Java結果: 72c9bbe7eed0efe5e82ea9568136d8f52347259e
PHP結果: f720d73d18a7bb9cf36808af17ce40621ebfb405
Java代碼
public static String sha1(String toHash)
{
String hash = null;
try
{
MessageDigest digest = MessageDigest.getInstance("SHA-1");
byte[] bytes = toHash.getBytes("ASCII"); //I tried UTF-8, ISO-8859-1...
digest.update(bytes, 0, bytes.length);
bytes = digest.digest();
StringBuilder sb = new StringBuilder();
for(byte b : bytes)
{
sb.append(String.format("%02X", b));
}
hash = sb.toString();
}
catch(NoSuchAlgorithmException e)
{
e.printStackTrace();
}
catch(UnsupportedEncodingException e)
{
e.printStackTrace();
}
return hash.toLowerCase(Locale.ENGLISH);
}
PHP代碼
sha1("UgJaDVYEClRUD1cAAVUBVwRTB1MDAA9SBgcDBwNXAwNZBQdUAAACBA==");
任何幫助,將不勝感激
更新
在Java和PHP中,我做了以下事情:
Java的
String toHash = "qwerty";
String hash = sha1(toHash); //Prints: b1b3773a05c0ed0176787a4f1574ff0075f7521e
toHash = Base64.encodeToString(toHash.getBytes("ASCII"), Base64.DEFAULT);
hash = sha1(toHash); //Prints: 88bfb2d77c3b42823bab820c1737f03c97d87c1b
PHP
$toHash = "qwerty";
sha1($toHash); //Prints: b1b3773a05c0ed0176787a4f1574ff0075f7521e
sha1(base64_encode($toHash)); //Prints: 278aa0e8dde2af58a4eed613467da219a35c5278
我想Base64編碼對PHP和Java上不同的字符串做了些什么,對於為什么有任何想法?
更新2
我應該更清楚,對不起,我的意思是:
Java的輸出
sha1(Base64.encodeToString("qwerty".getBytes("ASCII"), Base64.DEFAULT));
與PHP的輸出不同
sha1(base64_encode("qwerty"));
更新3
雖然base64編碼的字符串都等於cXdlcnR5
。
基本上:
- sha1("qwerty") == sha1("qwerty")
- Base64.encodeToString("qwerty".getBytes(), Base64.DEFAULT) == base64_encode("qwerty")
- sha1(Base64.encodeToString("qwerty".getBytes(), Base64.DEFAULT)) != sha1(base64_encode("qwerty"))
我已經在我散列的字符串上刪除了base64編碼,但我仍然想知道我可以做些什么來使它工作。
編輯
你在encodeToString
方法中使用了兩次toHash
變量,使你的第二行代碼變得多余。
這段代碼
String toHash = "qwerty";
String hash = sha1(toHash); //Prints: b1b3773a05c0ed0176787a4f1574ff0075f7521e
toHash = Base64.encodeToString(toHash.getBytes("ASCII"), Base64.DEFAULT);
hash = sha1(toHash); //Prints: 88bfb2d77c3b42823bab820c1737f03c97d87c1b
相當於這段代碼
String toHash = "qwerty";
toHash = Base64.encodeToString(toHash.getBytes("ASCII"), Base64.DEFAULT);
hash = sha1(toHash); //Prints: 88bfb2d77c3b42823bab820c1737f03c97d87c1b
所以基本上就是java
在使用PHP的同時
我猜你錯了
3年后,我遇到了同樣的問題,但這次我發現了問題。 以下是遇到這個問題的人的解決方案:
我用的是:
sha1("qwerty") == sha1("qwerty")
Base64.encodeToString("qwerty".getBytes(), Base64.DEFAULT) == base64_encode("qwerty")
sha1(Base64.encodeToString("qwerty".getBytes(), Base64.DEFAULT)) != sha1(base64_encode("qwerty"))
這個問題是Base64.DEFAULT
,Base64的默認行為包裝了字符串(將\\n
添加到字符串)。 為了獲得與PHP方法相同的結果,您應該使用Base64.NO_WRAP
:
sha1("qwerty") == sha1("qwerty")
Base64.encodeToString("qwerty".getBytes(), Base64.NO_WRAP) == base64_encode("qwerty")
sha1(Base64.encodeToString("qwerty".getBytes(), Base64.NO_WRAP)) == sha1(base64_encode("qwerty"))
我做了這個改變之后就開始工作了
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.