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 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀