0

我在 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#代码:

    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);
}

两个代码的输出哈希不一样,有人可以帮我吗

4

0 回答 0