php等效于以下sha1加密C#代码 [英] php equivalent to following sha1 encryption c# code

查看:74
本文介绍了php等效于以下sha1加密C#代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将以下用于sha1密码加密检查的c#代码转换为PHP版本,但似乎失败了:

I have tried converting the following c# code for sha1 password encryption check to a PHP version, but seem to be failing:

按照我的理解,这段代码-> 1)编码成ASCII 2)存储在字节数组中3)将该字节数组加密到sha1中,然后再次将新的字节[60]存储到字节数组中4)将该字节数组转换为64位字符串.我无法在以下代码中获得字节字符串部分.

As this code goes in my understanding-> 1) Encode into ASCII 2)Stores in byte array 3) encrypt that byte array into sha1 and stores into again in byte array new byte[60] 4)converts that byte array in 64 bit string. I am not able to get the byte string part in following code.

 private static string ComputeHas(byte[] arry){

    StringBuilder sb = new StringBuilder(arry.Length);
    return Convert.ToBase64String(arry);
}
public static string SHA1HashEncode(string pwd)
{
    SHA1 sh = new SHA1CryptoServiceProvider();
    byte[] arr = new byte[60];
    string hash = "";
    byte[] array = Encoding.ASCII.GetBytes(pwd);
    arr = sh.ComputeHash(array);
    hash = ComputeHas(arr);
    return hash;
}

这是我尝试过的:

//convert string to ascii 
for($i = 0; $i < mb_strlen($pwd, 'ASCII'); $i++) {

$pwd1=ord($pwd[$i]); 
} 

//sha1 encryption of password 
$pwd2=sha1($pwd1);

到目前为止,我为获得可能的准确度所做的尝试如下://将字符串转换为ascii

What i have tried so far to get possible exact is following: //convert string to ascii

for($i = 0; $i < mb_strlen($pwd, 'ASCII'); $i++)
{
    $pwd1=ord($pwd[$i]);
    $pwd2[]=$pwd1; 
}

//convert array to string
$pwdString = serialize($pwd2);

//compute hase of string
$pwdHash= array();
$pwdHash[]=array_fill(0,60,sha1($pwdString));

$pwdHashString=serialize($pwdHash);

//convert to 64 bit string encodeing

$pwd64encode=base64_encode($pwdHashString);
print_r($pwd64encode);

但是我不能完全理解php中的C#代码.

But i am not getting exact out of C# code in php.

推荐答案

来自PHP有关的文档SHA1

$variable = sha1("input");

这篇关于php等效于以下sha1加密C#代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆