日期计算:
1 /// <summary> 2 /// 获得两个日期之间的工作天数(周一到周五) 3 /// </summary> 4 /// <param name="startTime">起始日期</param> 5 /// <param name="endTime">结束日期</param> 6 /// <returns>返回工作天数</returns> 7 public static int GetWorkDays(DateTime startTime, DateTime endTime) 8 { 9 if (startTime > endTime) return 0; //如果结束时间小于起始时间,则返回-1 10 11 int weekdays = 0; 12 for (DateTime start = startTime; start <= endTime; start = start.AddDays(1)) 13 { 14 if (start.DayOfWeek == DayOfWeek.Saturday || start.DayOfWeek == DayOfWeek.Sunday) 15 { 16 weekdays++; 17 } 18 } 19 20 TimeSpan ts = endTime.AddDays(1) - startTime; 21 return Math.Abs(ts.Days) - weekdays; 22 }
获得两个日期之间的工作天数(周一到周五)
几种加密方式:
1 /// <summary> 2 /// Base64加密 3 /// </summary> 4 /// <param name="codeName">加密采用的编码方式</param> 5 /// <param name="source">待加密的明文</param> 6 /// <returns></returns> 7 public static string EncodeBase64(Encoding encode, string source) 8 { 9 try 10 { 11 byte[] bytes = encode.GetBytes(source); 12 source = Convert.ToBase64String(bytes); 13 source = source.Replace("+", " "); 14 } 15 catch 16 { 17 source = string.Empty; 18 } 19 return source; 20 } 21 22
Base64加密
1 /// <summary> 2 /// Base64解密 3 /// </summary> 4 /// <param name="codeName">解密采用的编码方式,注意和加密时采用的方式一致</param> 5 /// <param name="result">待解密的密文</param> 6 /// <returns>解密后的字符串</returns> 7 public static string DecodeBase64(Encoding encode, string result) 8 { 9 string decode = ""; 10 try 11 { 12 byte[] bytes = Convert.FromBase64String(result.Replace(" ", "+")); 13 decode = encode.GetString(bytes); 14 } 15 catch 16 { 17 decode = result; 18 } 19 return decode; 20 } 21 22
Base64解密
1 private string _DESKey = "dswebeee"; 2 public string DESKey 3 { 4 set 5 { 6 _DESKey = value; 7 } 8 } 9 10 /// <summary> 11 /// des加密 12 /// </summary> 13 /// <param name="toEncrypt"></param> 14 /// <returns></returns> 15 public string DESEncrypt(string toEncrypt) 16 { 17 //定义DES加密服务提供类 18 DESCryptoServiceProvider des = new DESCryptoServiceProvider(); 19 //加密字符串转换为byte数组 20 byte[] inputByte = System.Text.ASCIIEncoding.UTF8.GetBytes(toEncrypt); 21 //加密密匙转化为byte数组 22 byte[] key = Encoding.ASCII.GetBytes(_DESKey); //DES密钥(必须8字节) 23 des.Key = key; 24 des.IV = key; 25 //创建其支持存储区为内存的流 26 MemoryStream ms = new MemoryStream(); 27 //定义将数据流链接到加密转换的流 28 CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write); 29 cs.Write(inputByte, 0, inputByte.Length); 30 cs.FlushFinalBlock(); 31 StringBuilder ret = new StringBuilder(); 32 foreach (byte b in ms.ToArray()) 33 { 34 //向可变字符串追加转换成十六进制数字符串的加密后byte数组。 35 ret.AppendFormat("{0:X2}", b); 36 } 37 return ret.ToString(); 38 } 39 40 41 /// <summary> 42 /// des解密 43 /// </summary> 44 /// <param name="toDecrypt"></param> 45 /// <returns></returns> 46 public string DESDecrypt(string toDecrypt) 47 { 48 //定义DES加密解密服务提供类 49 DESCryptoServiceProvider des = new DESCryptoServiceProvider(); 50 //加密密匙转化为byte数组 51 byte[] key = Encoding.ASCII.GetBytes(_DESKey); 52 des.Key = key; 53 des.IV = key; 54 //将被解密的字符串每两个字符以十六进制解析为byte类型,组成byte数组 55 int length = (toDecrypt.Length / 2); 56 byte[] inputByte = new byte[length]; 57 for (int index = 0; index < length; index++) 58 { 59 string substring = toDecrypt.Substring(index * 2, 2); 60 inputByte[index] = Convert.ToByte(substring, 16); 61 } 62 //创建其支持存储区为内存的流 63 MemoryStream ms = new MemoryStream(); 64 //定义将数据流链接到加密转换的流 65 CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write); 66 cs.Write(inputByte, 0, inputByte.Length); 67 cs.FlushFinalBlock(); 68 return ASCIIEncoding.UTF8.GetString((ms.ToArray())); 69 }
des加密和解密
代码优化:
1 var timer = Stopwatch.StartNew(); 2 long sumtimer = 0; 3 //要查看执行时间的代码 4 //Method(); 5 timer.Stop(); 6 string ms = String.Format("Method方法 {0} ms \r\n", timer.ElapsedMilliseconds); 7 sumtimer += timer.ElapsedMilliseconds; 8 timer = Stopwatch.StartNew();
查看代码的执行时间
时间: 2024-10-07 16:32:22