查 System.Security.Cryptography
using System.Security.Cryptography;
// 將傳入的字串加密。
// 由呼叫端應用程式傳來的參數(strEcrypt),選擇要使用的加密演算法,如: MD5、SHA1…等
public string RunEncrypt(string strOriginal, string strEcrypt)
{
HashAlgorithm algorithm = null;
string strHashed = string.Empty;
byte[] byteData = null;
switch (strEcrypt)
{
case "MD5":
algorithm = new MD5CryptoServiceProvider(); // 使用 MD5 演算法做加密
break;
case "RIPEMD160":
algorithm = new RIPEMD160Managed();
break;
case "SHA1":
algorithm = new SHA1Managed();
break;
case "SHA256":
algorithm = new SHA256Managed();
break;
case "SHA384":
algorithm = new SHA384Managed();
break;
case "SHA512":
algorithm = new SHA512Managed();
break;
default:
algorithm = new MD5CryptoServiceProvider();
break;
}
if (!string.IsNullOrEmpty(strOriginal))
{
byteData = algorithm.ComputeHash(Encoding.ASCII.GetBytes(strOriginal));
strHashed = Encoding.ASCII.GetString(byteData);
}
return strHashed;
}