Question:
how to md5 encrypt the password in asp.net using c sharp with sql?
Nidhi
2011-03-01 04:21:44 UTC
how to md5 encrypt the password in asp.net using c sharp with sql

PLZ......... HELP ME..........
Three answers:
MILKRONAUT
2011-03-01 06:36:42 UTC
Your going to want to add some salt to the password before you place it into the database, MD5 has experienced some significant collisions.



'The string we wish to encrypt

Dim strPlainText as String = "Encrypt me!"



'The array of bytes that will contain the encrypted value of strPlainText

Dim hashedDataBytes as Byte()





'The encoder class used to convert strPlainText to an array of bytes

Dim encoder as New UTF8Encoding()



'Create an instance of the MD5CryptoServiceProvider class

Dim md5Hasher as New MD5CryptoServiceProvider()



'Call ComputeHash, passing in the plain-text string as an array of bytes

'The return value is the encrypted value, as an array of bytes

hashedDataBytes = md5Hasher.ComputeHash(encoder.GetBytes(strPlainText))
Apoclyo Fable
2011-03-01 04:36:49 UTC
Doesn't it have some sort of Microsoft cyrptographic provider class for that sort of stuff and also, I think SQL has a MD5 field type.
Deep
2014-03-10 10:49:09 UTC
here is the complete tutorial



Step 1 : Include the required namespace.

using System.Security.Cryptography; using System.Text;// for stringbuilder class



using System.Security.Cryptography;

using System.Text;// for stringbuilder class



Step 2 : Create an object of MD5 class.

MD5 md5H = MD5.Create();



Step 3 : Convert the string into byte array and compute its hash

byte[] data = md5H.ComputeHash(Encoding.UTF8.GetBytes(text));



Step 4 : Loop through each byte of hashed data and format each one as a hexadecimal string.

for (int i = 0; i < data.Length; i++){

sBuilder.Append(data[i].ToString("x2"));

}



complete code

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Security.Cryptography;

using System.Text;



public partial class MD5Hashing : System.Web.UI.Page

{

public string MD5Hash(string text)

{

MD5 md5H = MD5.Create();

//convert the input string to a byte array and compute its hash

byte[] data = md5H.ComputeHash(Encoding.UTF8.GetBytes(text));

// create a new stringbuilder to collect the bytes and create a string

StringBuilder sB = new StringBuilder();

//loop through each byte of hashed data and format each one as a hexadecimal string

for (int i = 0; i < data.Length; i++){

sB.Append(data[i].ToString("x2"));

}

//return hexadecimal string

return sB.ToString();



}

}


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...