This tutorial will demonstrate how to use MD5 to encrypt passwords in your database using ASP.NET 4.0 and C#.
MD5 is a one way encryption technique, which means that once the password is encrypted we will not be able to decrypt it. We use this for scenarios similar to a user's password because we don't want anyone to determine a user's password from the database. When a given password is encrypted with MD5, that password will always generate the same array of 16 bytes. This means that there is actually no need to decrypt user passwords, instead we simply need to encrypt their password and then verify if the array of bytes returned by the MD5 encryption matches the array of bytes stored in the database. This allows us to keep their passwords safe because we don't need to send the user's password over the internet to and from the database, instead we use the encrypted form of it that cannot be decrypted.
Need help with cloud hosting? Try Server Intellect. We used them for our cloud hosting services and we are very happy with the results!
Adding the Database
For this example, we will need to create a simple web site with a database that we will use to encrypt some data. Our database will need to store a user id, user name, and password. At this point, I have created a new ASP.NET Empty Web Site. To begin:
Adding the ConnectionString
Next, we need to setup a connection string to the database we have added. To do this, open up the Web.Config file for editing and add in the following code between the <configuration> and <system.web> tags:
I just signed up at Server Intellect and couldn't be more pleased with my fully scalable & redundant cloud hosting! Check it out and see for yourself.
Adding the Default.aspx Page
To test this out, we will need to create a simple web form with two textboxes, two buttons, and a label so that we can collect data from the user and then store and retrieve that data from our database. To setup our form:

Encrypting the Password
Now that we have our form all setup, we need to add in some functionality to accept input from the user, encrypt the data, and then store it in our database. Then, we will add some functionality to check the encrypted password in the database and ensure that it is being stored properly. To begin, open Default.aspx up to design mode and double click btnCreate to generate the click event method for that button. Then, at the top of the Default.aspx.cs class add in the following using statements:
We used over 10 web hosting companies before we found Server Intellect. Our cloud hosting,was set up in less than 24 hours. We were able to confirm our order over the phone. They responded to our inquiries within an hour. Server Intellect's customer support and assistance are the best we've ever experienced.
Next, add in the following code to the btnCreate_Click event method:
Let's review what this code is actually doing when the user clicks this button. First, we need to prepare the data before we store it in the database by grabbing the username and encrypting the password. Then, we simply use a SQL query to store the data in our database.
That's pretty much it for actually encrypting the password and storing it in the database, but in order to verify that we have actually stored the correct data we will add some functionality to btnCheck to display the encrypted password as a byte array. To do this, open Default.aspx to design mode and double click btnCheck. Then, add the following code to the btnCheck_Click event method:
This code simply selects the row of the database where the user name corresponds to the text in txtUserName and then stores the data from the password column in a byte array. Then, we output each of those bytes to our label seperated by spaces.
If you're looking for a really good web host, try Server Intellect - we found the setup procedure and their control panel, very easy to adapt to and their IT team is awesome!
Testing
To test this out, load up the web site. Input some data for the user name and password and click btnCreate. Then, click btnCheck to verify that you have 16 bytes of data. Here is my sample data and the results:
UserName: 'admin'
Password: 'default!'

The Default.aspx source looks like this:
Need help with cloud hosting? Try Server Intellect. We used them for our cloud hosting services and we are very happy with the results!
Adding the Database
For this example, we will need to create a simple web site with a database that we will use to encrypt some data. Our database will need to store a user id, user name, and password. At this point, I have created a new ASP.NET Empty Web Site. To begin:
- Right click the project in your solution explorer.
- Select add ASP.NET folder.
- Select App_Data.
- Right click the App_Data folder.
- Select add new item...
- Select a SQL Database.
- Name it 'Database.mdf'.
- Click add.
- Expand the Database.mdf folder in your server/database explorer.
- Right click the Tables folder.
- Select add new table.
- Add the following columns with their respective types to the table:
Column Name Data Type UserId int UserName nvarchar(50) Password binary(16) - Right click the UserId column and select set primary key.
- Change the IsIdentity property of the UserId column to 'Yes'.
- Save the table as 'Users'.
Adding the ConnectionString
Next, we need to setup a connection string to the database we have added. To do this, open up the Web.Config file for editing and add in the following code between the <configuration> and <system.web> tags:
<connectionStrings> |
I just signed up at Server Intellect and couldn't be more pleased with my fully scalable & redundant cloud hosting! Check it out and see for yourself.
Adding the Default.aspx Page
To test this out, we will need to create a simple web form with two textboxes, two buttons, and a label so that we can collect data from the user and then store and retrieve that data from our database. To setup our form:
- Right click the project in your solution explorer.
- Select add new item...
- Select a web form.
- Name it 'Default.aspx'.
- Click add.
- Open Default.aspx up to design mode.
- From the top menu, select Table -> Insert Table.
- Add a table with 3 rows, 2 columns, and an unsepcified width.
- In the top left cell, type in 'UserName: '.
- Beneath this cell, type in 'Password: '.
- In the top right cell, drag and drop a textbox.
- Change the ID property of the textbox to 'txtUserName'.
- Beneath this cell, drag and drop a textbox.
- Change the ID propety of the textbox to 'txtPassword'.
- Change the TextMode property of the textbox to 'Password'.
- Select the two cells from the bottom row and merge them.
- Set the alignment of the bottom row to center.
- Drag and drop a button into the bottom row.
- Change the ID property of the button to 'btnCreate'.
- Change the Text propert of the button to 'Create Account'.
- Add a break line underneath the table.
- Drag and drop a button after the break line.
- Change the ID property of the button to 'btnCheck'.
- Change the Text property of the button to 'Check Data'.
- Drag and drop a label next to btnCheck.
- Change the Text property of the label to an empty string.

Encrypting the Password
Now that we have our form all setup, we need to add in some functionality to accept input from the user, encrypt the data, and then store it in our database. Then, we will add some functionality to check the encrypted password in the database and ensure that it is being stored properly. To begin, open Default.aspx up to design mode and double click btnCreate to generate the click event method for that button. Then, at the top of the Default.aspx.cs class add in the following using statements:
using System.Data; |
We used over 10 web hosting companies before we found Server Intellect. Our cloud hosting,was set up in less than 24 hours. We were able to confirm our order over the phone. They responded to our inquiries within an hour. Server Intellect's customer support and assistance are the best we've ever experienced.
Next, add in the following code to the btnCreate_Click event method:
protected void btnCreate_Click(object sender, EventArgs e) |
Let's review what this code is actually doing when the user clicks this button. First, we need to prepare the data before we store it in the database by grabbing the username and encrypting the password. Then, we simply use a SQL query to store the data in our database.
That's pretty much it for actually encrypting the password and storing it in the database, but in order to verify that we have actually stored the correct data we will add some functionality to btnCheck to display the encrypted password as a byte array. To do this, open Default.aspx to design mode and double click btnCheck. Then, add the following code to the btnCheck_Click event method:
protected void btnCheck_Click(object sender, EventArgs e) |
This code simply selects the row of the database where the user name corresponds to the text in txtUserName and then stores the data from the password column in a byte array. Then, we output each of those bytes to our label seperated by spaces.
If you're looking for a really good web host, try Server Intellect - we found the setup procedure and their control panel, very easy to adapt to and their IT team is awesome!
Testing
To test this out, load up the web site. Input some data for the user name and password and click btnCreate. Then, click btnCheck to verify that you have 16 bytes of data. Here is my sample data and the results:
UserName: 'admin'
Password: 'default!'

The Default.aspx source looks like this:
<body> |
No comments:
Post a Comment