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

我尝试将以下php代码转换为C#
并尝试取得不成功。</ p>

php代码是</ p>

  $ string =“012014Te $ ting#501834502014060007400”; 
$ salt =“Cli3ntH @ sah”;
$ utfString = mb_convert_encoding($ string。$ salt,ÄSCII“);
$ hashTag = sha1($ utfString,true);
$ Hash = base64_encode($ hashTag);
</ code> </ pre>
\ n

使用C#代码</ p>

  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);
</ code> </ pre>

也试过这个,其中objtohash = $ string。$ salt(即连接)</ p>

  var sha1 = new System.Security.Cryptography.SHA1Managed(); 
//转换为 ascii byte array
byte [] AScii = EncodeAscii(objtohash);
// Hash it
byte [] hashBytes = sha1.ComputeHash(AScii);
//将其转换为base 64
var Hash = Convert.ToBase64String(hashBytes);
</ code> </ pre>

我已经尝试了其他几种方法,但我无法获得与php示例相同的散列值。有些人 可以做到并希望解释为什么。</ p>

谢谢</ p>
</ div>

展开原文

原文

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

dpuuys2198
dpuuys2198 在php中,您在ASCII字符串上使用sha1函数,但在UTF8字符串上使用C#
2 年多之前 回复
duanna3634
duanna3634 请提供原始,预期和错误的值。
2 年多之前 回复
doukundong9652
doukundong9652 这里有语法错误?$utfString=mb_convert_encoding($$串盐,ASCII“。);
2 年多之前 回复
duanjiancong4860
duanjiancong4860 可能重复的是C#有类似PHP的mb_convert_encoding()吗?
2 年多之前 回复

1个回答



语法错误是手指麻烦。</ p>

答案结果是,基本上是 尝试通过谷歌搜索找到的任何和所有组合是:</ p>

  var objtohashArry = Encoding.ASCII.GetBytes(objtohash); 
var HashSharresult = SHA1.Create() .ComputeHash(objtohashArry);
var requestHash = Convert.ToBase64String(HashSharresult);
RequestHash = requestHash;
</ code> </ pre>
</ div>

展开原文

原文

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;

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐