Encrypt passwords in a database using MD5

Source: Internet
Author: User
Tags insert md5
Encryption | data | Database NET provides a class for data encryption, and the following examples illustrate how to use MD5 for data encryption.

First, create a UserAccount table, the fields two: username and password, the types are varchar (25) and binary (16), and the following ASP.net code is the specific implementation when creating the user:

<%@ Import namespace= "System.Security.Cryptography"%>
<%@ Import namespace= "System.Text"%>
<%@ Import namespace= "System.Data"%>
<%@ Import namespace= "System.Data.SqlClient"%>
<script runat= "Server" language= "VB" >
Sub CreateAccount (sender as Object, E as EventArgs)
' 1. Create a connection
Const strconnstring as String
strconnstring= "Data source=.;i Nitial catalog=test; User Id=sa; password=; "
Dim objconn as New SqlConnection (strconnstring)

' 2. Create Command object
Dim strSQL as String = _
"INSERT into UserAccount (Username,password)" & _
"VALUES (@Username, @Password)"
Dim objcmd as New SqlCommand (strSQL, objconn)

' 3. Create parameters
Dim Paramusername as SqlParameter
Paramusername = New SqlParameter ("@Username", SqlDbType.VarChar, 25)
Paramusername.value = txtUsername.Text
OBJCMD.PARAMETERS.ADD (Paramusername)


' Encrypt password field '

Dim Md5hasher as New MD5CryptoServiceProvider ()

Dim hashedbytes as Byte ()
Dim Encoder as New utf8encoding ()

Hashedbytes = Md5hasher.computehash (encoder. GetBytes (Txtpwd.text))

Dim Parampwd as SqlParameter
Parampwd = New SqlParameter ("@Password", Sqldbtype.binary, 16)
Parampwd.value = Hashedbytes
OBJCMD.PARAMETERS.ADD (PARAMPWD)


' Insert Database
objConn.Open ()
Objcmd.executenonquery ()
Objconn.close ()

' Redirect other pages
End Sub
</script>

<form runat= "Server" >
User name: <asp:textbox runat= "Server" id= "txtUserName"/>
<br/>
Password: <asp:textbox runat= "Server" id= "txtpwd" textmode= "Password"/>
<p><asp:button runat= "Server" text= "Create user" onclick= "CreateAccount"/></p>
</form>

The following is the ASP.net code that validates the user:


<%@ Import namespace= "System.Security.Cryptography"%>
<%@ Import namespace= "System.Text"%>
<%@ Import namespace= "System.Data"%>
<%@ Import namespace= "System.Data.SqlClient"%>
<script runat= "Server" language= "VB" >
Sub Login (sender as Object, E as EventArgs)
' 1. Create a connection
Const strconnstring as String
strconnstring= "Data source=.;i Nitial catalog=test; User Id=sa; password=; "
Dim objconn as New SqlConnection (strconnstring)

' 2. Create Command object
Dim strSQL as String = "SELECT COUNT (*) from UserAccount" & _
"WHERE username= @Username and password= @Password"
Dim objcmd as New SqlCommand (strSQL, objconn)

' 3. Create parameters
Dim Paramusername as SqlParameter
Paramusername = New SqlParameter ("@Username", SqlDbType.VarChar, 25)
Paramusername.value = txtUsername.Text
OBJCMD.PARAMETERS.ADD (Paramusername)


' Encrypt password
Dim Md5hasher as New MD5CryptoServiceProvider ()

Dim hasheddatabytes as Byte ()
Dim Encoder as New utf8encoding ()

Hasheddatabytes = Md5hasher.computehash (encoder. GetBytes (Txtpwd.text))

Dim Parampwd as SqlParameter
Parampwd = New SqlParameter ("@Password", Sqldbtype.binary, 16)
Parampwd.value = Hasheddatabytes
OBJCMD.PARAMETERS.ADD (PARAMPWD)


' Execute Query
objConn.Open ()
Dim iresults as Integer = Objcmd.executescalar ()
Objconn.close ()

If iresults = 1 Then
' Legal
Else
' Not legal
End If
End Sub
</script>

<form runat= "Server" >
User name: <asp:textbox runat= "Server" id= "txtUserName"/><br/>
Password: <asp:textbox runat= "Server" id= "txtpwd" textmode= "Password"/>
<p><asp:button runat= "Server" text= "login" onclick= "Login"/>
</form>

The following is an example of MD5CryptoServiceProvider directly generated:
<%@ Import namespace= "System.Security.Cryptography"%>
<%@ Import namespace= "System.Text"%>
<script language= "VB" runat= "Server" >
Sub Displayencryptedtext (sender as Object, E as EventArgs)
If Page.IsValid Then
Dim Md5hasher as New MD5CryptoServiceProvider ()

Dim hasheddatabytes as Byte ()
Dim Encoder as New utf8encoding ()

Hasheddatabytes = Md5hasher.computehash (encoder. GetBytes (txtPassword.Text))

Ltlresults.text = "<b>encrypted results</b><br/> Results are into" & _
"An array of bytes. These bytes contain the values:<p><ul> "

Dim B as Byte
For each B in hasheddatabytes
Ltlresults.text &= "<li>" & B & "</li>"
Next b

Ltlresults.text &= "</ul>"
End If
End Sub
</script>

<form runat= "Server" >
Enter A string:
<asp:textbox id= "Txtpassword" runat= "Server"/>
<asp:requiredfieldvalidator runat= "Server" controltovalidate= "Txtpassword"
Display= "Dynamic" errormessage= "<i>you must provide a value here...</i>"/>
<asp:regularexpressionvalidator runat= "Server" controltovalidate= "Txtpassword"
Display= "Dynamic" errormessage= "<i>the string must be characters or less...</i>"
validationexpression= "^. {1,20}$ "/>
<br/>
<asp:button runat= "Server" text= "View the String as Encrypted Text"
onclick= "Displayencryptedtext"/>
<p>
<asp:literal runat= "Server" id= "Ltlresults"/>
</form>



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.