目录
2、获取时间戳,时间戳从1970年1月1日00:00:00至今的秒数,即当前的时间
1、MD5签名方法
/// <summary>
/// MD5签名方法
/// </summary>
/// <param name="inputText">加密参数</param>
/// <returns></returns>
private static string MD5(string inputText)
{
MD5 md5 = new MD5CryptoServiceProvider();
byte[] fromData = System.Text.Encoding.UTF8.GetBytes(inputText);
byte[] targetData = md5.ComputeHash(fromData);
string byte2String = null;
for (int i = 0; i < targetData.Length; i++)
{
byte2String += targetData[i].ToString("x2");
}
return byte2String;
}
2、获取时间戳,时间戳从1970年1月1日00:00:00至今的秒数,即当前的时间
private long getTime()
{
TimeSpan cha = (DateTime.Now - TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)));
long t = (long)cha.TotalMilliseconds;
return t;
}
3、生成随机串
private static string GetRandomString(int length)
{
const string key = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789";
if (length < 1)
return string.Empty;
Random rnd = new Random();
byte[] buffer = new byte[8];
ulong bit = 31;
ulong result = 0;
int index = 0;
StringBuilder sb = new StringBuilder((length / 5 + 1) * 5);
while (sb.Length < length)
{
rnd.NextBytes(buffer);
buffer[5] = buffer[6] = buffer[7] = 0x00;
result = BitConverter.ToUInt64(buffer, 0);
while (result > 0 && sb.Length < length)
{
index = (int)(bit & result);
sb.Append(key[index]);
result = result >> 5;
}
}
return sb.ToString();
}
4、string转base64
public string ToBase64Str(string Str)
{
byte[] b = System.Text.Encoding.Default.GetBytes(Str);
return Convert.ToBase64String(b);
}
5、base64转string
public string ToStrBase64(string a)
{
byte[] c = Convert.FromBase64String(a);
a = System.Text.Encoding.UTF8.GetString(c);
return a;
}