建站学 - 轻松建站从此开始!

建站学-个人建站指南,网页制作,网站设计,网站制作教程

当前位置: 建站学 > 网站开发 > JSP教程 >

微博短网址原理(ShortUrl)的算法(2)

时间:2011-03-02 16:44来源: 作者: 点击:
下面来看看程序部分: Java代码 1.public static string[] ShortUrl(string url) 2. { 3. //可以自定义生成MD5加密字符传前的混合KEY 4. string key = Leejor; 5. //要使用生成URL的字符 6. string[] chars = new s

下面来看看程序部分:


Java代码
1.public static string[] ShortUrl(string url)  
2.        {  
3.            //可以自定义生成MD5加密字符传前的混合KEY  
4.            string key = "Leejor";  
5.            //要使用生成URL的字符  
6.            string[] chars = new string[]{  
7.                "a","b","c","d","e","f","g","h",  
8.                "i","j","k","l","m","n","o","p",  
9.                "q","r","s","t","u","v","w","x",  
10.                "y","z","0","1","2","3","4","5",  
11.                "6","7","8","9","A","B","C","D",  
12.                "E","F","G","H","I","J","K","L",  
13.                "M","N","O","P","Q","R","S","T",  
14.                "U","V","W","X","Y","Z" 
15. 
16.              };  
17.            //对传入网址进行MD5加密  
18.            string hex = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(key + url, "md5");  
19. 
20.            string[] resUrl = new string[4];  
21. 
22.            for (int i = 0; i < 4; i++)  
23.            {  
24.                //把加密字符按照8位一组16进制与0x3FFFFFFF进行位与运算  
25.                int hexint = 0x3FFFFFFF & Convert.ToInt32("0x" + hex.Substring(i * 8, 8), 16);  
26.                string outChars = string.Empty;  
27.                for (int j = 0; j < 6; j++)  
28.                {  
29.                    //把得到的值与0x0000003D进行位与运算,取得字符数组chars索引  
30.                    int index = 0x0000003D & hexint;  
31.                    //把取得的字符相加  
32.                    outChars += chars[index];  
33.                    //每次循环按位右移5位  
34.                    hexint = hexint >> 5;  
35.                }  
36.                //把字符串存入对应索引的输出数组  
37.                resUrl[i] = outChars;  
38.            }  
39. 
40.            return resUrl;  
41.        } 
public static string[] ShortUrl(string url)
        {
            //可以自定义生成MD5加密字符传前的混合KEY
            string key = "Leejor";
            //要使用生成URL的字符
            string[] chars = new string[]{
                "a","b","c","d","e","f","g","h",
                "i","j","k","l","m","n","o","p",
                "q","r","s","t","u","v","w","x",
                "y","z","0","1","2","3","4","5",
                "6","7","8","9","A","B","C","D",
                "E","F","G","H","I","J","K","L",
                "M","N","O","P","Q","R","S","T",
                "U","V","W","X","Y","Z"

              };
            //对传入网址进行MD5加密
            string hex = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(key + url, "md5");

            string[] resUrl = new string[4];

            for (int i = 0; i < 4; i++)
            {
                //把加密字符按照8位一组16进制与0x3FFFFFFF进行位与运算
                int hexint = 0x3FFFFFFF & Convert.ToInt32("0x" + hex.Substring(i * 8, 8), 16);
                string outChars = string.Empty;
                for (int j = 0; j < 6; j++)
                {
                    //把得到的值与0x0000003D进行位与运算,取得字符数组chars索引
                    int index = 0x0000003D & hexint;
                    //把取得的字符相加
                    outChars += chars[index];
                    //每次循环按位右移5位
                    hexint = hexint >> 5;
                }
                //把字符串存入对应索引的输出数组
                resUrl[i] = outChars;
            }

            return resUrl;
        }

现在可以直接使用该方法,可以等到下面四组值

ShortUrl(http://www.me3.cn")[0];  //得到值fAVfui

ShortUrl("http://www.me3.cn")[1];  //得到值3ayQry

ShortUrl("http://www.me3.cn")[2];  //得到值UZzyUr

ShortUrl("http://www.me3.cn")[3];  //得到值36rQZn

(责任编辑:admin)
织梦二维码生成器
顶一下
(3)
100%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片