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));
    }}因为要做Discuz! 7.1和asp.net整合的项目 ,所以需要Cookies的解密  网上找了的 都没有用,所以只能自己来了,谢谢!

解决方案 »

  1.   

    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();
        }已有高手写了部分  但好像还是不行,希望高手能帮忙全部完成下,谢谢
      

  2.   

    还没解决?
     PHP 不懂...
      

  3.   

    $keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : '';这句代码什么意思    特别是  -$ckey_length   C#中Substring没有 参数为负数的///
      

  4.   

     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;这部分有人帮忙翻译了
      

  5.   

    在编码之前,要确定php与.net中的字符串的编码类型一致。如字符串都采用utf-8编码。否则结果是不一样的。
      

  6.   

     public string _authcode(string str, string operation, string key, int expiry)
        {
          
                int ckey_length = 4;
                key = md5(string.IsNullOrEmpty(key) ? ConfigurationManager.AppSettings["UC_KEY"] : key);
                string keya = md5(key.Substring(0, 16));
                string keyb = md5(key.Substring(16, 16));
                string md5MT = md5(microtime());
                string keyc = ckey_length > 0 ? (operation.Equals("DECODE") ? str.Substring(0, ckey_length) : md5MT.Substring(md5MT.Length - ckey_length)) : string.Empty;            string cryptkey = keya + md5(keya + keyc);
                int key_length = cryptkey.Length;            str = operation.Equals("DECODE") ? base64_decode_b(str.Substring(ckey_length)) : (expiry > 0 ? expiry + time() : 0).ToString("D10") + (md5(str + keyb)).Substring(0, 16) + str;
                
                int string_length = str.Length;
                string result = string.Empty;            int[] box = new int[256];
                for (int i = 0; i < 256; i++)
                    box[i] = i;            int[] rndkey = new int[256];
                for (int i = 0; i < 256; i++)
                    rndkey[i] = (int)(cryptkey[i % key_length]);            for (int i = 0, j = 0; i < 256; i++)
                {
                    j = (j + box[i] + rndkey[i]) % 256;
                    int tmp = box[i];
                    box[i] = box[j];
                    box[j] = tmp;
                }            byte[] tmpResult = new byte[string_length];
                for (int a = 0, j = 0, i = 0; i < string_length; i++)
                {
                    a = (a + 1) % 256;
                    j = (j + box[a]) % 256;
                    int tmp = box[a];
                    box[a] = box[j];
                    box[j] = tmp;
                    //result += (char)(str[i] ^ box[(box[a] + box[j]) % 256]);
                    tmpResult[i] = (byte)(str[i] ^ box[(box[a] + box[j]) % 256]);
                }
                //有问题
                result = System.Text.Encoding.GetEncoding(ConfigurationManager.AppSettings["UC_CHARSET"]).GetString(tmpResult); 
                
                if (operation.Equals("DECODE"))
                {
                    if ((long.Parse(result.Substring(0, 10)) == 0 || long.Parse(result.Substring(0, 10)) > time()) && result.Substring(10, 16).Equals(md5(result.Substring(26) + keyb).Substring(0, 16)))
                        return result.Substring(26);
                    else
                        return string.Empty;
                }
                else
                {
                    return keyc + base64_encode(tmpResult).Replace("=", string.Empty);
                    //return keyc + base64_encode(result).Replace("=", string.Empty);
                }
            }代码有了 但提示“ Base-64 字符串中的无效字符”
      

  7.   

    str的值:ab8bq3QmNJsT0nhPo90ny7K2l5SfvQ81Wq%2FBDWyKydpI2WuIppzUU2tehh6pIqr7pYyDuVnfW15BXC45T5%2Bh%2BL2MJg
      

  8.   

    return keyc + base64_encode(tmpResult).Replace("=", "");