dongyuduan1890 2013-08-16 20:22
浏览 43

将代码转换为C#?

I convert this code:

strtoupper(bin2hex(mhash(mhash_sha512, "$ico{$product[0]}{$user[1]}")));

into C#:

byte[] data = SHA512.Create().ComputeHash(Encoding.ASCII.GetBytes(args[0]));
string result = "";
for(int index = 0; index < data.Length; index++)
    result += data[index].ToString("X2");

and it works very good. But if I try convert input into UTF16:

strtoupper(bin2hex(mhash(mhash_sha512, iconv('UTF-8', 'UTF-16', "$ico{$product[0]}{$user[1]}"))));

and in C# convert using Encoding class:

SHA512.Create().ComputeHash(Encoding.Convert(Encoding.ASCII/UTF8, Encoding.GetEncoding("UTF-16"), Encoding.ASCII/UTF8.GetBytes(args[0])));

it not working. I need to get the same output from both languages. How can I correctly convert string ("bbb") into UTF16 in C# language? Thank you...

  • 写回答

1条回答 默认 最新

  • drmq16019 2013-08-16 20:37
    关注

    In case of little endian UTF-16 encoding, change Encoding.GetEncoding("UTF-16") to Encoding.Unicode in case of big endian UTF-16, change it to 'Encoding.BigEndianUnicode`.

    Upon further inspection (e.g. UTF-8 is not the same as Encoding.ASCII), would this be a good translation of your PHP code?

    var bytesToHash = Encoding.Convert(Encoding.UTF8, Encoding.BigEndianUnicode, Encoding.UTF8.GetBytes(args[0]));
    var result = string.Concat(SHA512.Create().ComputeHash(bytesToHash).Select(b => b.ToString("X2")));
    

    I am not familiar with the expected output of this php mhash function, but perhaps you could try this:

    var hash = new System.Security.Cryptography.SHA512CryptoServiceProvider().ComputeHash(Encoding.Default.GetBytes(data));
    var hashString = BitConverter.ToString(hash);
    var phpLikeHash = hashString.Replace("-", String.Empty).ToUpper();
    

    UPDATE Ok, so based upon your new information the first example I included would work if no encoding conversion of the input is required. So this at least confirms that we can rely on the SHA512 producing the same output as PHP's mhash in the way you use it, and that any differences are related to the bytes provided as input.

    I would suggest you experiment a bit with different encodings using the same literal string as input in both PHP code and c#. Like e.g. below:

        static bool HashIt(Encoding source, Encoding dest, string input, string expectedOutput)
        {
            byte[] bytes = source.GetBytes(input);
            if (source != dest && dest != null)
                bytes =  Encoding.Convert(source, dest, bytes);
            var hash = SHA512.Create().ComputeHash(bytes);
            var hashString = string.Concat(hash.Select(b => b.ToString("X2")));
            if (hashString.Equals(expectedOutput))
            {
                Console.WriteLine("Match found");
                Console.WriteLine("Source encoding: {0}", source.WebName);
                if (source != dest && dest != null)
                    Console.WriteLine("Converted to: {0}", dest.WebName);
                return true;
            }
            return false;
        }
    
        static void Main(string[] args)
        {
            var inputs = new [] { "13338170AS875HEO49F8Sam-PC", @"13338170AS875HEO49F8Sam-PC" };
            var expectedOutput = "A91A64DD4DF1880651CB6B919BE02C4363ED6D4B07EA246CF47FFB509918E4AA4C294FF8BA9F73E5‌​CD1CE463BB3E66F84A6C294D70C781CD0610345BCADEEDA7";
            var encodings = Encoding.GetEncodings().Select(e => e.GetEncoding());
            var matchFound = false;
            foreach (var srcEncoding in encodings)
            {
                foreach (var input in inputs)
                {
                    if (HashIt(srcEncoding, null, input, expectedOutput))
                        matchFound = true;
                    foreach (var destEncoding in encodings)
                    {
                        if (HashIt(srcEncoding, destEncoding, input, expectedOutput))
                            matchFound = true;
                    }
                }
            }
            if (!matchFound)
                Console.WriteLine("No matches found");
            Console.ReadLine();
        }
    

    Which produces the following output on my system:

    No matches found
    

    So you may be out of luck. I don't see at this time what else to try.

    评论

报告相同问题?

悬赏问题

  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch