将php函数转换为C#

时间:2013-10-24 04:52:53

标签: c# php encryption code-conversion

我在PHP中有这个功能,我试图在C#中复制..到目前为止,我一直没有成功。以下是PHP代码:

    function _remove_cipher_noise($data, $key)
{
    $keyhash = $this->hash($key);
    $keylen = strlen($keyhash);
    $str = '';

    for ($i = 0, $j = 0, $len = strlen($data); $i < $len; ++$i, ++$j)
    {
        if ($j >= $keylen)
        {
            $j = 0;
        }

        $temp = ord($data[$i]) - ord($keyhash[$j]);

        if ($temp < 0)
        {
            $temp = $temp + 256;
        }

        $str .= chr($temp);
    }
    echo base64_encode($str)."<br/>";
}

C#CODE:

    public static byte[] RemoveCypherNoise(string cypherDataString, string
keyString)
{
    System.Text.Encoding asciiEncoding = System.Text.Encoding.ASCII;
    System.Security.Cryptography.SHA1 hash = System.Security.Cryptography.SHA1CryptoServiceProvider.Create();
    int temp;
    byte[] key = asciiEncoding.GetBytes(keyString);
    byte[] cypherData = asciiEncoding.GetBytes(cypherDataString);
    byte[] keyHash = hash.ComputeHash(key);
    //string result = "";
    byte[] result = new byte[cypherData.Length];

    for (int i = 0, j = 0; i < cypherData.Length; i++, j++)
    {
        if (j >= keyHash.Length)
        {
            j = 0;
        }

        temp = cypherData[i] - keyHash[j];
        if (temp < 0)
        {
            temp = temp + 256;
        }

        result[i] = (byte)temp;
    }
    var abc = asciiEncoding.GetString(result);
    var ddd = Convert.ToBase64String(result);
}

两个代码的输出哈希值都不相同,任何人都可以帮我解决

0 个答案:

没有答案