SHA1 sha1 = new SHA1CryptoServiceProvider();这里
需要引入命名空间:
using System.Security.Cryptography;
class Sha1
{
/// <summary>
/// SHA1加密
/// </summary>
/// <param name="content">待加密的字符串</param>
/// <param name="encode">编码方式</param>
/// <returns></returns>
public static String Sha1Sign(String content,Encoding encode)
{
try
{
SHA1 sha1 = new SHA1CryptoServiceProvider();//创建SHA1对象
byte[] bytes_in = encode.GetBytes(content);//将待加密字符串转为byte类型
byte[] bytes_out = sha1.ComputeHash(bytes_in);//Hash运算
sha1.Dispose();//释放当前实例使用的所有资源
String result = BitConverter.ToString(bytes_out);//将运算结果转为string类型
result = result.Replace("-", "").ToUpper();//替换并转为大写
return result;
}catch(Exception ex)
{
return ex.Message;
}
}
}
运行结果: