在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
网上找了下全是PHP或者JS 的,自己写了个C#的: using System; namespace WpfApplication1 $time = time(); if (8 !== strlen($key)) { return array($as, $cp);
public static string generateASCP() { string AS = ""; string CP = ""; string md5KeyAsc5 = ""; string md5KeyDesc5 = ""; long time = Timestamp(); string key = time.ToString("X2").ToUpper(); string md5Key = EncryptWithMD5("" + time).ToUpper(); if (8 != key.Length) { AS = "479BB4B7254C150"; CP = "7E0AC8874BB0985"; } else { md5KeyAsc5 = md5Key.Substring(0, 5); md5KeyDesc5 = md5Key.Substring(md5Key.Length - 5); AS = ""; CP = ""; for (int i = 0; i < 5; i++) { AS += md5KeyAsc5[i].ToString() + key[i].ToString(); } AS = "" + "A1" + AS + key.Substring(key.Length - 3); CP = key.Substring(0, 3) + CP + "E1"; } return "as=" + AS + "&cp=" + CP; } /// <summary> /// 当前时间戳 /// </summary> /// <returns></returns> static long Timestamp() { System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区 long timeStamp = (long)(DateTime.Now - startTime).TotalSeconds; // 相差秒数 System.Console.WriteLine(timeStamp); return timeStamp; } /// <summary> /// 时间戳转日期 /// </summary> /// <param name="time"></param> /// <returns></returns> static DateTime TimestampToDatetime(long time) { long unixTimeStamp = 1478162177; System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区 DateTime dt = startTime.AddSeconds (unixTimeStamp); System.Console.WriteLine(dt.ToString("yyyy/MM/dd HH:mm:ss:ffff")); return dt; } static string EncryptWithMD5(string source) { byte[] sor = Encoding.UTF8.GetBytes(source); MD5 md5 = MD5.Create(); byte[] result = md5.ComputeHash(sor); StringBuilder strbul = new StringBuilder(40); for (int i = 0; i < result.Length; i++) { strbul.Append(result[i].ToString("x2"));//加密结果"x2"结果为32位,"x3"结果为48位,"x4"结果为64位 } return strbul.ToString(); } } }
转载个JAVA的:
public class ToutiaoUtil { /** * 生成 as 和 cp , 用作 API 的请求参数 * <p> * function () { * var t = Math.floor((new Date).getTime() / 1e3), i = t.toString(16).toUpperCase(), e = md5(t).toString().toUpperCase(); * if (8 != i.length)return {as: "479BB4B7254C150", cp: "7E0AC8874BB0985"}; * for (var s = e.slice(0, 5), o = e.slice(-5), n = "", a = 0; 5 > a; a++)n += s[a] + i[a]; * for (var l = "", r = 0; 5 > r; r++)l += i[r + 3] + o[r]; * return {as: "A1" + n + i.slice(-3), cp: i.slice(0, 3) + l + "E1"} * } * </p> */ public static Map<String, String> getAsCp() { int t = (int) (System.currentTimeMillis() / 1000); String i = Integer.toHexString(t).toUpperCase(); String e = getMD5(t + "").toUpperCase(); String s = e.substring(0, 5); String o = e.substring(e.length() - 5, e.length()); String n = ""; for (int j = 0; 5 > j; j++) { n += s.substring(j, j + 1) + i.substring(j, j + 1); } String l = ""; for (int r = 0; 5 > r; r++) { l += i.substring(r + 3, r + 3 + 1) + o.substring(r, r + 1); } String as = "A1" + n + i.substring(i.length() - 3, i.length()); String cp = i.substring(0, 3) + l + "E1"; Map<String, String> map = new HashMap<>(); map.put(Constant.AS, as); map.put(Constant.CP, cp); return map; } /** * 对字符串 MD5 加密 */ public static String getMD5(String str) { try { // 生成一个 MD5 加密计算摘要 MessageDigest md = MessageDigest.getInstance("MD5"); // 计算 MD5 函数 md.update(str.getBytes()); // digest() 最后确定返回 MD5 hash 值, 返回值为8为字符串 // 因为 MD5 hash 值是16位的hex值, 实际上就是8位的字符 // BigInteger 函数则将8位的字符串转换成 16 位 hex 值, 用字符串来表示得到字符串形式的 hash 值 return new BigInteger(1, md.digest()).toString(16); } catch (Exception e) { ErrorAction.print(e); } return ""; } }
java refer:
|
请发表评论