Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 对Convert.ToChar(0)进行哈希处理时,其哈希结果不同于PHP中的chr(0)_C#_Php_Hash_Bytearray_Chr - Fatal编程技术网

C# 对Convert.ToChar(0)进行哈希处理时,其哈希结果不同于PHP中的chr(0)

C# 对Convert.ToChar(0)进行哈希处理时,其哈希结果不同于PHP中的chr(0),c#,php,hash,bytearray,chr,C#,Php,Hash,Bytearray,Chr,我在PHP中有一个字符串,它被转换成字节数组并进行散列 转换为字节数组的字符串如下所示: “g”。chr(0)。"公安条例", 我需要在C#中等效字节数组,以便得到相同的散列 编辑:这里是完整的问题,产生的哈希不一样 PHP C# 我尝试过不同的base64编码,如: public static string ByteToString(byte[] buff) { string sbinary = ""; for (int i = 0; i < bu

我在PHP中有一个字符串,它被转换成字节数组并进行散列

转换为字节数组的字符串如下所示:

“g”。chr(0)。"公安条例",

我需要在C#中等效字节数组,以便得到相同的散列

编辑:这里是完整的问题,产生的哈希不一样

PHP

C#

我尝试过不同的base64编码,如:

public static string ByteToString(byte[] buff)
    {
        string sbinary = "";
        for (int i = 0; i < buff.Length; i++)
            sbinary += buff[i].ToString("X2"); /* hex format */
        return sbinary;
    }   
公共静态字符串ByteToString(字节[]buff)
{
字符串sbinary=“”;
for(int i=0;i

但最终问题似乎是由于php使用的chr(0)而被散列的字节数组。

您可以使用
Convert.ToChar(Int32)
方法将unicode值表示为字符


用法:
“g”+Convert.ToChar(0)+“poo”
他还要求将其作为
字节
数组,这是最重要的,因为必须使用字符到字节的转换:

//Conversion object
System.Text.UTF8Encoding  encoding = new System.Text.UTF8Encoding();

//Your any String text
String stringText = "g" + Convert.ToChar(0) + "poo";

//Convert to the wanted byte array
byte[] byteArray = encoding.GetBytes(stringText);
如果愿意,您也可以在一行中执行相同的操作:)


我再回答一遍,因为你改变了整个问题,所以你的新问题有了新的解决方案

首先,HMACSHA512不会给出与php代码相同的结果。顺便说一下,PHP生成的哈希是:

41028bb90af31dc6e7fa100a8ceb1e220bfedf67ea723292db9a4e1c14f69c73adf30eeba61ab054cdc91c82f6be2f76dd602392be630b5e99b1f86da1460cbe
为了在C#中得到相同的结果,我创建了
billiejeansha512
类,以使散列与PHP相等。另外,我还使用
encoding.GetBytes
将byte[]转换为字符串,我创建了
方法ByteToString
以正确转换

下面的代码不像PHP那么简单,但我向您或任何人提出挑战,让他们用相同的PHP散列来做这件事更简单!我向你挑战,我加倍向你挑战!让我们转到代码:

//These are not default imports, so you need to use it
using System.Text;
using System.Security.Cryptography;

//Before your actual class, you need to make your custom 512
public class BillieJeansSHA512 : HMAC
{
    public BillieJeansSHA512(byte[] key)
    {
        HashName = "System.Security.Cryptography.SHA512CryptoServiceProvider";
        HashSizeValue = 512;
        BlockSizeValue = 128;
        Key = (byte[])key.Clone();
    }
}

//Now, there's your actual class
public class HelloWorld{


    //First, use this method to convert byte to String like a boss
    static string ByteToString(byte[] buff)
    {
        string sbinary = "";
        for (int i = 0; i < buff.Length; i++)
            sbinary += buff[i].ToString("x2"); /* hex format */
        return sbinary;
    }    

    //Now let's get it started!
    public static void Main(String []args){
        System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();

        //Your data
        var apiSecret = "5432919427bd18884fc2a6e48b65dfba48fd9a1a46e3468b52fadbc6d6b463425";
        var data = "payment_currency=USD&group_orders=0&count=100&nonce=1385689989977529";
        var endPoint = "/info/orderbook";

        String message = endPoint + Convert.ToChar(0) + data;

        //Hash will be stored here
        String hash = "";

        //put your key at your custom 512
        BillieJeansSHA512 hmacsha512 = new BillieJeansSHA512(encoding.GetBytes(apiSecret));
        //hash'em all
        byte[] result = hmacsha512.ComputeHash(encoding.GetBytes(message));

        //convert bytes to string
        hash = ByteToString(result);

        //See it :)
        Console.WriteLine(hash);

        //Or if you using it at a web-page, this is it.
        //Response.Write(hash)

        //Now the easy part, convert it to base64
        var bytesTo64 = System.Text.Encoding.UTF8.GetBytes(hash);
        String hash64 = System.Convert.ToBase64String(bytesTo64);
    }
}
并且
String hash64
具有与PHP相同的编码base64值:

NDEwMjhiYjkwYWYzMWRjNmU3ZmExMDBhOGNlYjFlMjIwYmZlZGY2N2VhNzIzMjkyZGI5YTRlMWMxNGY2OWM3M2FkZjMwZWViYTYxYWIwNTRjZGM5MWM4MmY2YmUyZjc2ZGQ2MDIzOTJiZTYzMGI1ZTk5YjFmODZkYTE0NjBjYmU=

我假设你想要这样的东西:“g”+(char)0+“poo”。这个字符串在php中是否意味着要将“g”与null连接,然后与“poo”连接?是的,这是正确的。。然后我需要把它转换成一个字节数组。。但是我不知道如何用C#制作同样的字符串。。字节数组和我从中生成的后续散列是不同的我回答了如何将其作为字节数组。当我散列结果不同于在phpWell中散列“g”+Convert.ToChar(0)+“poo”时,这不是同一个问题。我为这个问题做了一个新的答案,所以,看看吧。我真诚地想谢谢你!你为什么要定制512?块大小在PHP或其他语言中是不同的??男人。。我一直在扯头发。谢谢对这很难,我至少花了3个小时!!玩得开心:)@WagnerLeonardi你刚刚救了我的命。非常感谢你发布这篇文章。
41028bb90af31dc6e7fa100a8ceb1e220bfedf67ea723292db9a4e1c14f69c73adf30eeba61ab054cdc91c82f6be2f76dd602392be630b5e99b1f86da1460cbe
//These are not default imports, so you need to use it
using System.Text;
using System.Security.Cryptography;

//Before your actual class, you need to make your custom 512
public class BillieJeansSHA512 : HMAC
{
    public BillieJeansSHA512(byte[] key)
    {
        HashName = "System.Security.Cryptography.SHA512CryptoServiceProvider";
        HashSizeValue = 512;
        BlockSizeValue = 128;
        Key = (byte[])key.Clone();
    }
}

//Now, there's your actual class
public class HelloWorld{


    //First, use this method to convert byte to String like a boss
    static string ByteToString(byte[] buff)
    {
        string sbinary = "";
        for (int i = 0; i < buff.Length; i++)
            sbinary += buff[i].ToString("x2"); /* hex format */
        return sbinary;
    }    

    //Now let's get it started!
    public static void Main(String []args){
        System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();

        //Your data
        var apiSecret = "5432919427bd18884fc2a6e48b65dfba48fd9a1a46e3468b52fadbc6d6b463425";
        var data = "payment_currency=USD&group_orders=0&count=100&nonce=1385689989977529";
        var endPoint = "/info/orderbook";

        String message = endPoint + Convert.ToChar(0) + data;

        //Hash will be stored here
        String hash = "";

        //put your key at your custom 512
        BillieJeansSHA512 hmacsha512 = new BillieJeansSHA512(encoding.GetBytes(apiSecret));
        //hash'em all
        byte[] result = hmacsha512.ComputeHash(encoding.GetBytes(message));

        //convert bytes to string
        hash = ByteToString(result);

        //See it :)
        Console.WriteLine(hash);

        //Or if you using it at a web-page, this is it.
        //Response.Write(hash)

        //Now the easy part, convert it to base64
        var bytesTo64 = System.Text.Encoding.UTF8.GetBytes(hash);
        String hash64 = System.Convert.ToBase64String(bytesTo64);
    }
}
41028bb90af31dc6e7fa100a8ceb1e220bfedf67ea723292db9a4e1c14f69c73adf30eeba61ab054cdc91c82f6be2f76dd602392be630b5e99b1f86da1460cbe
NDEwMjhiYjkwYWYzMWRjNmU3ZmExMDBhOGNlYjFlMjIwYmZlZGY2N2VhNzIzMjkyZGI5YTRlMWMxNGY2OWM3M2FkZjMwZWViYTYxYWIwNTRjZGM5MWM4MmY2YmUyZjc2ZGQ2MDIzOTJiZTYzMGI1ZTk5YjFmODZkYTE0NjBjYmU=