110,533
社区成员
发帖
与我相关
我的任务
分享
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;
}
package com.csdn.shorturl;
public class MyShortCutUrl {
/**
* @param args
*/
public static void main(String[] args) {
String url = "http://tech.sina.com.cn/i/2011-03-23/11285321288.shtml";
for (String string : ShortText(url)) {
System.out.println(string);
}
}
public static String[] ShortText(String string) {
String key = "XuLiang"; // 自定义生成MD5加密字符串前的混合KEY
String[] chars = new String[] { // 要使用生成URL的字符
"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" };
String hex = (new CMyEncrypt()).getMD5OfStr(key + string);
int hexLen = hex.length();
int subHexLen = hexLen / 8;
String[] ShortStr = new String[4];
for (int i = 0; i < subHexLen; i++) {
String outChars = "";
int j = i + 1;
String subHex = hex.substring(i * 8, j * 8);
long idx = Long.valueOf("3FFFFFFF", 16) & Long.valueOf(subHex, 16);
for (int k = 0; k < 6; k++) {
int index = (int) (Long.valueOf("0000003D", 16) & idx);
outChars += chars[index];
idx = idx >> 5;
}
ShortStr[i] = outChars;
}
return ShortStr;
}
}