#region MD5加密
/// <summary>
/// MD5加密
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string MD5(string str)
{
return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5");
}
#endregion
#region 时间转换
//时间转换成时间戳
public static int ConvertDateTimeInt(System.DateTime time)
{
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
return (int)(time - startTime).TotalSeconds;
}
//时间字符串转换成时间戳
public static int ConvertStringDateTimeInt(string timeStr)
{
if (string.IsNullOrEmpty(timeStr) || timeStr.Trim() == "0") return 0;
DateTime time = DateTime.ParseExact(timeStr, "yyyyMMddHHmmss", System.Globalization.CultureInfo.InstalledUICulture);
DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
return (int)(time - startTime).TotalSeconds;
}
// 时间戳转为C#格式时间
public static DateTime StampToDateTime(string timeStamp)
{
DateTime dateTimeStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
long lTime = long.Parse(timeStamp + "0000000");
TimeSpan toNow = new TimeSpan(lTime);
return dateTimeStart.Add(toNow);
}
#endregion
#region 将字符串使用base64算法加密
/// <summary>
/// 将字符串使用base64算法加密
/// </summary>
/// <param name="SourceString">待加密的字符串</param>
/// <param name="Ens">System.Text.Encoding 对象,如创建中文编码集对象:
/// System.Text.Encoding.GetEncoding("gb2312")</param>
/// <returns>编码后的文本字符串</returns>
public static string EncodingString(string SourceString, System.Text.Encoding Ens)
{
return Convert.ToBase64String(Ens.GetBytes(SourceString));
}
#endregion
#region 从base64编码的字符串中还原字符串,支持中文
/// <summary>
/// 从base64编码的字符串中还原字符串,支持中文
/// </summary>
/// <param name="Base64String">Base64加密后的字符串</param>
/// <param name="Ens">System.Text.Encoding对象,如创建中文编码集对象:
/// System.Text.Encoding.Default</param>
/// <returns>还原后的文本字符串</returns>
public static string DecodingString(string Base64String, System.Text.Encoding Ens)
{
return Ens.GetString((Convert.FromBase64String(Base64String)));
}
#endregion