貿易伙伴要求我發送HMAC SHA1哈希作爲小寫heaxits。我能找到的唯一參考是關於PHP的。我可以在.NET和Java中執行哈希,但是如何輸出「小寫hexits」?小寫hexits看起來不等於Base64。小寫六進制。 .NET和Java等效
0
A
回答
1
小寫的十六進制數字(hexits)使用方法:
public static String toHex(byte[] bytes) {
BigInteger bi = new BigInteger(1, bytes);
return String.format("%0" + (bytes.length << 1) + "x", bi);
}
從相關的問題: In Java, how do I convert a byte array to a string of hex digits while keeping leading zeros?
2
啊!我喜歡簡單。這是解決方案。
Public Shared Function Encrypt(ByVal plainText As String, ByVal preSharedKey As String) As String
Dim preSharedKeyBytes() As Byte = Encoding.UTF8.GetBytes(preSharedKey)
Dim plainTextBytes As Byte() = Encoding.UTF8.GetBytes(plainText)
Dim hmac = New HMACSHA1(preSharedKeyBytes)
Dim cipherTextBytes As Byte() = hmac.ComputeHash(plainTextBytes)
Dim strTemp As New StringBuilder(cipherTextBytes.Length * 2)
For Each b As Byte In cipherTextBytes
strTemp.Append(Conversion.Hex(b).PadLeft(2, "0"c).ToLower)
Next
Dim cipherText As String = strTemp.ToString
Return cipherText
End Function
這與在raw_output參數中使用FALSE的PHP hash_hmac函數兼容。
0
這裏的莎草科的解決方案的C#編譯:
private static String toHex(byte[] cipherTextBytes)
{
var strTemp = new StringBuilder(cipherTextBytes.Length * 2);
foreach(Byte b in cipherTextBytes)
{
strTemp.Append(Microsoft.VisualBasic.Conversion.Hex(b).PadLeft(2, '0').ToLower());
}
String cipherText = strTemp.ToString();
return cipherText;
}
相關問題
- 1. QUrl toPercentEncoding()小寫十六進制
- 2. KeyStorage .Net等效於Java?
- 3. SQL十進制等效於.NET
- 4. .Net等效於ScaleHeight和Scalewidth
- 5. Clojure等價於Python的編碼('十六進制')和解碼('十六進制')
- 6. 使用Java腳本顯示小數點,二進制和十六進制
- 7. Java - ByteBuffer和十進制和十六進制之間的轉換
- 8. 將十進制轉換爲十六進制和十六進制
- 9. 將十六進制和二進制值寫入文件
- 10. 在Python中轉換等效的解碼('十六進制')
- 11. 紅寶石替換與十六進制等效的HTML實體
- 12. 以十六進制等效的HTML實體
- 13. 發送字符串到它的十六進制等效
- 14. NSIS寫作QWORD十六進制值
- 15. sqlite3觸發器將十六進制文本轉換爲二進制blob等效
- 16. .NET中的十六進制編輯
- 17. 十六進制值等於負值
- 18. 十六進制和十進制轉換
- 19. PHP十六進制和二進制
- 20. ShellExecute等效於.NET
- 21. 十六進制串&十六進制
- 22. .NET中的等效URL重寫?
- 23. 我們如何在Java中編寫浮點數的二進制,八進制和十六進制文字?
- 24. 無效的十六進制數ora-01465
- 25. ORA-01465:無效的十六進制數
- 26. 無效的十六進制轉義
- 27. BitXor十進制等效
- 28. 將二進制,十六進制和八進制轉換爲java數據類型
- 29. lastindexof()和十六進制0x1A
- 30. Cout和十六進制[C++]
感謝堆了點。有誰知道如何在.NET 3.5中做到這一點? BigInteger僅在.NET中引入.NET。 – sedge
完美地工作!謝謝! –