dtj88302 2018-06-28 09:14
浏览 79

如何将php mb_convert_encoding()转换为C#等价物

I have tried rather unsuccessfully , to convert the following php code to C# and require help please.

php code is

$string="012014Te$ting#501834502014060007400";
$salt = "Cli3ntH@sah";
$utfString=mb_convert_encoding($string.$salt,ÄSCII");
$hashTag=sha1($utfString,true);
$Hash = base64_encode($hashTag);

with C# code

    byte[] ascii = Encoding.ASCII.GetBytes(objtohash);
    byte[] utf8 = Encoding.Convert(Encoding.ASCII, Encoding.UTF8, ascii);
    byte[] hashBytes2 = sha1.ComputeHash(utf8);
    var Hash  = Convert.ToBase64String(hashBytes2);

also tried this, where objtohash = $string.$salt (i.e. concatenated)

var sha1 = new System.Security.Cryptography.SHA1Managed();
//convert to ascii byte array
byte[] AScii = EncodeAscii(objtohash);
//Hash it
byte[] hashBytes = sha1.ComputeHash(AScii);
//convert it to base 64
var Hash = Convert.ToBase64String(hashBytes);

I have tried several other ways as per SO, but I cannot get the same hashed value as the php sample.Hopefully someone can do it and hopefully give explanation as to why.

Thanks

  • 写回答

1条回答 默认 最新

  • doujia9204 2018-06-29 10:20
    关注

    The syntax error was finger trouble.

    The answer it turns out, ..basically by trying any and all combinations of what i could find by googling is:

            var objtohashArry = Encoding.ASCII.GetBytes(objtohash);
            var HashSharresult = SHA1.Create().ComputeHash(objtohashArry);
            var requestHash = Convert.ToBase64String(HashSharresult);
            RequestHash = requestHash;
    
    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序