Discuz Cookies解密算法的C#版本

这篇博客介绍了如何将DISCUZ论坛的Cookies解密算法移植到C#环境中,旨在实现PHP DISCUZ与ASP.NET项目之间的用户单点登录。文章提供了解密部分的代码,方便开发者进行集成。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 最近的项目需要PHP论坛DISCUZ与ASP.NET项目的整合,为了让用户只需要登陆一次,把DISCUZ的COOKIES加解密算法重写成C#的版本了,相信不少使用DISCUZ的站长、开发者跟我有一样的需求,共享给大家:
一是因为懒,二是因为觉得没有必要,所以就没有把加密的部分也顺便完成,只有DECODE,见谅

 1   private   string [] DiscuzDecode( string  hash)
 2      {
 3        //Discuz Cookies的相应解密算法,C#版
 4        string[] userinfo = new string[3];
 5        string Result = "";
 6        string authkey = "XXXXXXXXXXX";//这里是Discuz论坛的随机加密字串
 7        string discuz_auth_key = FormsAuthentication.HashPasswordForStoringInConfigFile((authkey + Request.UserAgent), "MD5").ToLower();
 8        string key =FormsAuthentication.HashPasswordForStoringInConfigFile(discuz_auth_key, "MD5").ToLower();
 9        int key_length = key.Length;
10        hash = (hash.Length % 4 == 0? hash : hash + "====".Substring(04 - hash.Length % 4);
11        byte[] byteHash = Convert.FromBase64String(hash);
12        int byteHash_length = byteHash.Length;
13        byte[] rndkey = new byte[256];
14        byte[] box = new byte[256];
15        byte SwapTmp;
16        for (int i = 0; i <= 255; i++)
17        {
18            rndkey[i] = (byte)(key[i % key_length]);
19            box[i] = (byte)i;
20        }

21        for (int j = 0, i = 0; i < 256; i++)
22        {
23            j = (j + box[i] + rndkey[i]) % 256;
24            SwapTmp = box[i];
25            box[i] = box[j];
26            box[j] = SwapTmp;
27        }

28        for (int a = 0, j = 0, i = 0; i < byteHash_length; i++)
29        {
30            a = (byte)((a + 1% 256);
31            j = (byte)((j + box[a]) % 256);
32            SwapTmp = box[a];
33            box[a] = box[j];
34            box[j] = SwapTmp;
35            byteHash[i] = (byte)((byteHash[i]) ^ (box[(box[a] + box[j]) % 256]));
36        }

37
38        Result = System.Text.Encoding.Default.GetString(byteHash);
39        userinfo = Result.Split('/t');
40        //userinfo[0]为md5加密后的字串
41        //userinfo[1]为安全问答
42        //userinfo[2]为用户在bbs中的uid
43        return userinfo;
44    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值