雖 2004 年王小雲教授宣告破解 MD5 等演算法,但對一般軟體應用安全性仍足夠。
密碼加密儲存:註冊時加密存入資料庫,驗證時將輸入密碼加密後比對密文。
| 演算法 | 類別 |
|---|---|
| MD5 | System.Security.Cryptography.MD5System.Security.Cryptography.MD5CryptoServiceProviderSystem.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5") |
| SHA1 | System.Security.Cryptography.SHA1System.Security.Cryptography.SHA1CryptoServiceProviderSystem.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "SHA1") |
public string Get_MD5_Method1(string strSource)
{
System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] bytResult = md5.ComputeHash(System.Text.Encoding.Default.GetBytes(strSource));
string strResult = BitConverter.ToString(bytResult, 4, 8); // 16位,取第9-25字元
strResult = strResult.Replace("-", "");
return strResult;
}
public string Get_MD5_Method2(string strSource)
{
string strResult = "";
System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
byte[] bytResult = md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(strSource));
for (int i = 0; i < bytResult.Length; i++)
strResult += bytResult[i].ToString("X"); // 16進制
return strResult;
}
public string Get_MD5_Method3(string strSource)
{
return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(strSource, "MD5");
}
以上皆為伺服器端加密,用戶端傳輸時密碼為明文。銀行級應用採用客戶端 ActiveX 控制項預先加密再傳送。