下面来看看程序部分:
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) |