1 /// <summary>
2 /// 算法的理论(网上找到的资料):
3 /// 1.将长网址md5生成32位签名串,分为4段,每段8个字节;
4 /// 2.对这四段循环处理,取8个字节,将他看成16进制串与0x3fffffff(30位1)与操作,即超过30位的忽略处理;
5 /// 3.这30位分成6段,每5位的数字作为字母表的索引取得特定字符,依次进行获得6位字符串;
6 /// 4.总的md5串可以获得4个6位串;取里面的任意一个就可作为这个长url的短url地址;
7 /// </summary>
8 /// <param name="url"></param>
9 /// <returns></returns>
10 public static string[] ShortUrl(string url)
11 {
12 //可以自定义生成MD5加密字符传前的混合KEY
13 string key = "Leejor";
14 //要使用生成URL的字符
15 string[] chars = new string[]{
16 "a" , "b" , "c" , "d" , "e" , "f" , "g" , "h" ,
17 "i" , "j" , "k" , "l" , "m" , "n" , "o" , "p" ,
18 "q" , "r" , "s" , "t" , "u" , "v" , "w" , "x" ,
19 "y" , "z" , "0" , "1" , "2" , "3" , "4" , "5" ,
20 "6" , "7" , "8" , "9" , "A" , "B" , "C" , "D" ,
21 "E" , "F" , "G" , "H" , "I" , "J" , "K" , "L" ,
22 "M" , "N" , "O" , "P" , "Q" , "R" , "S" , "T" ,
23 "U" , "V" , "W" , "X" , "Y" , "Z" };
24
25 //对传入网址进行MD5加密
26 string hex = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(key + url, "md5");
27
28 string[] resUrl = new string[4];
29
30 for (int i = 0; i < 4; i++)
31 {
32 //把加密字符按照8位一组16进制与0x3FFFFFFF进行位与运算
33 int hexint = 0x3FFFFFFF & Convert.ToInt32("0x" + hex.Substring(i * 8, 8), 16);
34 string outChars = string.Empty;
35 for (int j = 0; j < 6; j++)
36 {
37 //把得到的值与0x0000003D进行位与运算,取得字符数组chars索引
38 int index = 0x0000003D & hexint;
39 //把取得的字符相加
40 outChars += chars[index];
41 //每次循环按位右移5位
42 hexint = hexint >> 5;
43 }
44 //把字符串存入对应索引的输出数组
45 resUrl[i] = outChars;
46 }
47 return resUrl;
48 }
2 /// 算法的理论(网上找到的资料):
3 /// 1.将长网址md5生成32位签名串,分为4段,每段8个字节;
4 /// 2.对这四段循环处理,取8个字节,将他看成16进制串与0x3fffffff(30位1)与操作,即超过30位的忽略处理;
5 /// 3.这30位分成6段,每5位的数字作为字母表的索引取得特定字符,依次进行获得6位字符串;
6 /// 4.总的md5串可以获得4个6位串;取里面的任意一个就可作为这个长url的短url地址;
7 /// </summary>
8 /// <param name="url"></param>
9 /// <returns></returns>
10 public static string[] ShortUrl(string url)
11 {
12 //可以自定义生成MD5加密字符传前的混合KEY
13 string key = "Leejor";
14 //要使用生成URL的字符
15 string[] chars = new string[]{
16 "a" , "b" , "c" , "d" , "e" , "f" , "g" , "h" ,
17 "i" , "j" , "k" , "l" , "m" , "n" , "o" , "p" ,
18 "q" , "r" , "s" , "t" , "u" , "v" , "w" , "x" ,
19 "y" , "z" , "0" , "1" , "2" , "3" , "4" , "5" ,
20 "6" , "7" , "8" , "9" , "A" , "B" , "C" , "D" ,
21 "E" , "F" , "G" , "H" , "I" , "J" , "K" , "L" ,
22 "M" , "N" , "O" , "P" , "Q" , "R" , "S" , "T" ,
23 "U" , "V" , "W" , "X" , "Y" , "Z" };
24
25 //对传入网址进行MD5加密
26 string hex = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(key + url, "md5");
27
28 string[] resUrl = new string[4];
29
30 for (int i = 0; i < 4; i++)
31 {
32 //把加密字符按照8位一组16进制与0x3FFFFFFF进行位与运算
33 int hexint = 0x3FFFFFFF & Convert.ToInt32("0x" + hex.Substring(i * 8, 8), 16);
34 string outChars = string.Empty;
35 for (int j = 0; j < 6; j++)
36 {
37 //把得到的值与0x0000003D进行位与运算,取得字符数组chars索引
38 int index = 0x0000003D & hexint;
39 //把取得的字符相加
40 outChars += chars[index];
41 //每次循环按位右移5位
42 hexint = hexint >> 5;
43 }
44 //把字符串存入对应索引的输出数组
45 resUrl[i] = outChars;
46 }
47 return resUrl;
48 }
http://blog.csdn.net/5653325/article/details/6639829