Encrypt passwords in a database using MD5

Source: Internet
Author: User
Tags md5
NET provides data encryption classes, 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))

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.