SHA,全称SecureHashAlgorithm,是一种数据加密算法,该算法的思想是接收一段明文,然后以一种不可逆的方式将它转换成一段(通常更小)密文,也可以简单的理解为取一串输入码(称为预映射或信息),并把它们转化为长度较短、位数固定的输出序列即散列值(也称为信息摘要或信息认证代码)的过程。
SHA加密方式,包含多种方法,现汇总如下
#region SHA加密算法
public static string SHAmd5Encrypt(string str)
{
var bytes = Encoding.Default.GetBytes(str);//求Byte[]数组
var Md5 = new MD5CryptoServiceProvider();
var encryptbytes = Md5.ComputeHash(bytes);//求哈希值
return Base64To16(encryptbytes);//将Byte[]数组转为净荷明文(其实就是字符串)
}
public static string SHA1Encrypt(string str)
{
var bytes = Encoding.Default.GetBytes(str);
var SHA = new SHA1CryptoServiceProvider();
var encryptbytes = SHA.ComputeHash(bytes);
return Base64To16(encryptbytes);
}
public static string SHA256Encrypt(string str)
{
var bytes = Encoding.Default.GetBytes(str);
var SHA256 = new SHA256CryptoServiceProvider();
var encryptbytes = SHA256.ComputeHash(bytes);
return Base64To16(encryptbytes);
}
public static string SHA384Encrypt(string str)
{
var bytes = Encoding.Default.GetBytes(str);
var SHA384 = new SHA384CryptoServiceProvider();
var encryptbytes = SHA384.ComputeHash(bytes);
return Base64To16(encryptbytes);
}
public static string SHA512Encrypt(string str)
{
var bytes = Encoding.Default.GetBytes(str);
var SHA512 = new SHA512CryptoServiceProvider();
var encryptbytes = SHA512.ComputeHash(bytes);
return Base64To16(encryptbytes);
}
private static string Base64To16(byte[] buffer)
{
string md_str = string.Empty;
for (int i = 0; i < buffer.Length; i++)
{
md_str += buffer[i].ToString("x2");
}
return md_str;
}
#endregion