C# SHA1加密演算法,並轉為大寫
阿新 • • 發佈:2019-01-23
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;
}
}
}
執行結果: