小写十六进制字符。NET和Java等价物

小写十六进制字符。NET和Java等价物,java,php,.net,sha1,hmac,Java,Php,.net,Sha1,Hmac,一个贸易伙伴要求我将HMAC SHA1散列作为小写的heaxit发送。我能找到的唯一参考资料是关于PHP的。我可以在.NET和Java中进行哈希运算,但是如何用它们输出“小写的十六进制数”?小写的十六进制看起来并不等同于Base64 对于小写十六进制数字(hexits),请使用: 公共静态字符串toHex(字节[]字节){ BigInteger bi=新的BigInteger(1,字节); 返回字符串.格式(“%0”+(bytes.length)对于小写十六进制数字(hexits)使用: 公共静

一个贸易伙伴要求我将HMAC SHA1散列作为小写的heaxit发送。我能找到的唯一参考资料是关于PHP的。我可以在.NET和Java中进行哈希运算,但是如何用它们输出“小写的十六进制数”?小写的十六进制看起来并不等同于Base64

对于小写十六进制数字(hexits),请使用:

公共静态字符串toHex(字节[]字节){
BigInteger bi=新的BigInteger(1,字节);
返回字符串.格式(“%0”+(bytes.length)对于小写十六进制数字(hexits)使用:

公共静态字符串toHex(字节[]字节){
BigInteger bi=新的BigInteger(1,字节);
返回字符串.格式(“%0”+(bytes.length)对于小写十六进制数字(hexits)使用:

公共静态字符串toHex(字节[]字节){
BigInteger bi=新的BigInteger(1,字节);
返回字符串.格式(“%0”+(bytes.length)对于小写十六进制数字(hexits)使用:

公共静态字符串toHex(字节[]字节){
BigInteger bi=新的BigInteger(1,字节);

返回String.format(“%0”+(bytes.length啊!我喜欢简单。下面是解决方案

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

这与PHP hash_hmac函数兼容,原始输出参数为FALSE。

啊!我喜欢简单。下面是解决方案

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

这与PHP hash_hmac函数兼容,原始输出参数为FALSE。

啊!我喜欢简单。下面是解决方案

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

这与PHP hash_hmac函数兼容,原始输出参数为FALSE。

啊!我喜欢简单。下面是解决方案

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
这与PHP hash_hmac函数兼容,在raw_输出参数中为FALSE。

这里是sedge解决方案的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;
}
下面是莎草解决方案的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;
}
下面是莎草解决方案的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;
}
下面是莎草解决方案的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;
}

感谢堆。有人知道如何在.NET 3.5中做到这一点吗?BigInteger仅在4中引入.NET。非常好!谢谢!感谢堆。有人知道如何在.NET 3.5中做到这一点吗?BigInteger仅在4中引入.NET。非常好!谢谢!感谢堆。有人知道如何在.NET 3.5中做到这一点吗?BigInteger仅在4.0中引入.NET。非常好用!谢谢!感谢堆。有人知道如何在.NET 3.5中实现这一点吗?BigInteger仅在4.0中引入.NET。非常好用!谢谢!