function _authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {
    $ckey_length = 4;    $key = md5($key ? $key : UC_KEY);
    $keya = md5(substr($key, 0, 16));
    $keyb = md5(substr($key, 16, 16));
    $keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : '';    $cryptkey = $keya.md5($keya.$keyc);
    $key_length = strlen($cryptkey);    $string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string;
    $string_length = strlen($string);    $result = '';
    $box = range(0, 255);    $rndkey = array();
    for($i = 0; $i <= 255; $i++) {
        $rndkey[$i] = ord($cryptkey[$i % $key_length]);
    }    for($j = $i = 0; $i < 256; $i++) {
        $j = ($j + $box[$i] + $rndkey[$i]) % 256;
        $tmp = $box[$i];
        $box[$i] = $box[$j];
        $box[$j] = $tmp;
    }    for($a = $j = $i = 0; $i < $string_length; $i++) {
        $a = ($a + 1) % 256;
        $j = ($j + $box[$a]) % 256;
        $tmp = $box[$a];
        $box[$a] = $box[$j];
        $box[$j] = $tmp;
        $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
    }    if($operation == 'DECODE') {
        if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) {
            return substr($result, 26);
        } else {
                return '';
            }
    } else {
        return $keyc.str_replace('=', '', base64_encode($result));
    }}翻译的就是这个PHP函数成c#,小弟不懂PHP,所以在此求助大家,谢谢!

解决方案 »

  1.   

    或者翻译下前面这一部分$ckey_length = 4;    $key = md5($key ? $key : UC_KEY);
        $keya = md5(substr($key, 0, 16));
        $keyb = md5(substr($key, 16, 16));
        $keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : '';    $cryptkey = $keya.md5($keya.$keyc);
        $key_length = strlen($cryptkey);    $string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string;
        $string_length = strlen($string);    $result = '';
        $box = range(0, 255);
      

  2.   

    兄弟,你这代码写得太精练了,翻译的真累。只翻译了一部分,没有时间再给你翻了,在网上找找,接着上面的翻吧!
        const string UC_KEY = "Key";
        /*
         * $ckey_length = 4;    $key = md5($key ? $key : UC_KEY);
        $keya = md5(substr($key, 0, 16));
        $keyb = md5(substr($key, 16, 16));
        $keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : '';    $cryptkey = $keya.md5($keya.$keyc);
        $key_length = strlen($cryptkey);    $string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string;
        $string_length = strlen($string);    $result = '';
        $box = range(0, 255);
        */    protected void Page_Load(object sender, EventArgs e)
        {
     
        }    private string _authcode(string str, string operation, string key, long expiry)
        {
            int ckey_length = 4;        key = key == string.Empty ? key : UC_KEY;
            key = md5(key);        string keya = md5(key.Substring(0, 16));
            string keyb = md5(key.Substring(16, 16));        string strtmp = operation == "DECODE" ? str.Substring(0, ckey_length) : md5(microtime().Substring(0, -ckey_length));
            string keyc = ckey_length > 0 ? strtmp : "";        string cryptkey = keya + md5(keya + keyc);
            int key_length = cryptkey.Length;        expiry = expiry > 0 ? System.DateTime.Now.Ticks + expiry : 0;
            string expirystr =  string.Format("%010d", expiry);
            str = operation == "DECODE" ? Convert.FromBase64String(str.Substring(ckey_length)).ToString() : expirystr + md5(str + keyb).Substring(0, 16) + str;        int string_length = str.Length;        string result = "";        int box = (new Random()).Next(0, 255);        //MD5(
            return string.Empty;    }private string microtime()
    {
        return "asdfasdfasdfk";
      //throw new Exception("The method or operation is not implemented.");
    }    public static string md5(string str)
        {
            return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5").ToUpper();
        }