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;
    
    评论

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题