1 static void Main( string[] args ) 2 { 3 string s = getTimestamp(); 4 string ss = getNoncestr(); 5 Console.WriteLine( ss ); 6 Console.WriteLine( s ); 7 Console.ReadKey(); 8 }
/* * / <summary> * / 生成时间戳 * / 从 1970 年 1 月 1 日 00:00:00 至今的秒数,即当前的时间,且最终需要转换为字符串形式 * / </summary> * / <returns></returns> */ public string getTimestamp() { TimeSpan ts = DateTime.UtcNow - new DateTime( 1970, 1, 1, 0, 0, 0, 0 ); return(Convert.ToInt64( ts.TotalSeconds ).ToString() ); }
public static string getNoncestr() { Random random = new Random(); return(MD5Util.GetMD5( random.Next( 1000 ).ToString(), "GBK" ) ); } public class MD5Util { public MD5Util() { /* * * TODO: 在此处添加构造函数逻辑 * */ } /** 获取大写的MD5签名结果 */ public static string GetMD5( string encypStr, string charset ) { string retStr; MD5CryptoServiceProvider m5 = new MD5CryptoServiceProvider(); /* 创建md5对象 */ byte[] inputBye; byte[] outputBye; /* 使用GB2312编码方式把字符串转化为字节数组. */ try { inputBye = Encoding.GetEncoding( charset ).GetBytes( encypStr ); } catch ( Exception ex ) { inputBye = Encoding.GetEncoding( "GB2312" ).GetBytes( encypStr ); } outputBye = m5.ComputeHash( inputBye ); retStr = System.BitConverter.ToString( outputBye ); retStr = retStr.Replace( "-", "" ).ToUpper(); return(retStr); } }