asp.net| encryption
In ASP, no encrypted objects are provided, we can only use external objects for encryption. Now it's okay to provide a solution for encryption in asp.net. A class FormsAuthentication is included in the namespace System.Web.Security, which has a method hashpasswordforstoringinconfigfile. This method can turn user-supplied characters into garbled, then store them, and can even be stored in cookies.
The HashPasswordForStoringInConfigFile method is simple to use, and it supports the "SHA1" and "MD5" cryptographic algorithms.
The following code is a simple demonstration of its usage:
<%@ Page language= "C #"%>
<%@ Import namespace= "System.Web.Security"%>
<script language= "C #" runat= "Server" >
public void encryptstring (Object sender, EventArgs e)
{
SHA1. Text = FormsAuthentication.HashPasswordForStoringInConfigFile (txtPassword.Text, "SHA1");
MD5. Text =formsauthentication.hashpasswordforstoringinconfigfile (txtPassword.Text, "MD5");
}
</script>
<body>
<form runat= "Server" id= "Form1" >
<p>
<b>original Clear Text Password: </b>
<br>
<asp:textbox id= "Txtpassword" runat= "Server"/>
<asp:button runat= "Server" text= "Encrypt String" id= "Button1"/>
</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>
As you can see, this is simple and easy to use. We can encapsulate this encryption program in a function that is easy to reuse. The code is as follows:
public string Encryptpassword (String passwordstring,string passwordformat)
{
if (passwordformat= "SHA1") {
Encryptpassword=formsauthortication.hashpasswordforstoringinconfigfile (passwordstring, "SHA1");
}
ElseIf (passwordformat= "MD5")
{Encryptpassword=formsauthortication.hashpasswordforstoringinconfigfile (passwordstring, "MD5");
}
Else
{
Encryptpassword= "";
}