最近的项目需要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(0, 4 - 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
}

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44
