asp.net中的加密方法

來源:互聯網
上載者:User
本文由chenyangASP著作權,可以轉載,複製,粘貼,並請註明出處,但不得修改!
無論什麼時候只要我們使用資料庫開發網站,我們就必須保護使用者資料,這非常必要。
駭客可以盜竊口令,個人隱私遭到嚴重的破壞。最好的方法就是不儲存原始密碼,而是加密後再放到資料庫中。
當我們想驗證使用者時,我們只需將使用者輸入的口令再次加密與資料庫中的記錄進行比較即可。
在asp中我們需要額外的對象加密。
但在Asp.Net中 SDK可以通過system.web.security namespace中的CookieAuthentication類的

HashPassWordForStoringInConfigFile方法來解決問題。
這樣做的目的是加密設定檔的口令,甚至cookies.
HashPasswordForStoringInConfigFile方法非常容易使用,並且,它支援“SHA1”和“MD5”散列演算法。
為了清楚“HashPasswordForStoringInConfigFile”方法,讓我們製作一個小的ASP.NET頁,把輸入串在SHA1和MD5格式中譯成密碼。

<%@ Import Namespace="System.Web.Security" %>
<Html>
<head>
<script language="VB" runat=server>
Sub encryptString(Src As Object, E As EventArgs)
SHA1.Text = CookieAuthentication.HashPasswordForStoringInConfigFile(txtPassword.Text, "SHA1")
MD5.Text = CookieAuthentication.HashPasswordForStoringInConfigFile(txtPassword.Text, "MD5")
End Sub
</script>
</head>
<body>

<form runat=server>

<p><b>Original Clear Text Password: </b><br>
<asp:Textbox id="txtPassword" runat=server />
<asp:Button runat="server" text="Encrypt String" onClick="encryptString" /></p>

<p><b>Encrypted Password In SHA1: </b>
<asp:label id="SHA1" runat=server /></p>

<p><b>Encrypted Password In MD5: </b>
<asp:label id="MD5" runat=server /></p>

</form>

</body>

</html>
怎麼樣加密一串字串是很容易的事,為了使它更容易使用我製作了一個函數。下面我就給出函數的原始碼.

Function EncryptPassword (PasswordString as String, PasswordFormat as String) as String 
If PasswordFormat = "SHA1" then
EncryptPassword = CookieAuthentication.HashPasswordForStoringInConfigFile(PasswordString, "SHA1")
Elseif PasswordFormat = "MD5" then
EncryptPassword= CookieAuthentication.HashPasswordForStoringInConfigFile(PasswordString, "MD5")
Else
EncryptPassword = ""
End if
End Function 

Source:http://www.vipcn.com/InfoView/Article_10312.html

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.