Tuesday 21 August 2012

.Net Tips : Password Encryption in .Net with C# Examples and VB.Net Examples

For security purpose there is a need to convert plain string in cryptographic string or encrypt password, so hackers did not understand the password.
You can create cryptographic string using seven different algorithms that provided in .Net Framework.
This is mainly use in encrypt a password and store.
This encrypted password is do not convert back to original string.

Now if you want to compare password for authentication at that time you need to encrypt entered password at that time of login and check that encrypted string with already stored encrypted string.
Same input string generate same encrypted hash code.

Here is example for this.
In this Example we use MD5 algorithm. Means Encrypt password using MD5 algorithm.

C# Example :
        string strPassword = "thisisdemo";
        string strAlgorigthm = "MD5";
        //You can also specify these algorights RIPEMD160 or RIPEMD-160 ,SHA or SHA1 ,
        //SHA256 or SHA-256 ,SHA384 or SHA-384,SHA512 or SHA-512
        //SHA1Managed algorithm you must be instantiated directly like this "objAlg = new SHA1Managed()"

        HashAlgorithm objAlg = null;
        objAlg = HashAlgorithm.Create(strAlgorigthm);

        // Convert string to bytes
        byte[] objData = System.Text.Encoding.Default.GetBytes(strPassword);

        // Generate the hash code of password
        byte[] objHashBytes = objAlg.ComputeHash(objData);

        Response.Write("<b>Hash Code : </b>");
        Response.Write(BitConverter.ToString(objHashBytes));

VB.net Example :
        Dim strPassword As String = "thisisdemo"
        Dim strAlgorigthm As String = "MD5"
        'You can also specify these algorights RIPEMD160 or RIPEMD-160 ,SHA or SHA1 ,
        'SHA256 or SHA-256 ,SHA384 or SHA-384,SHA512 or SHA-512
        'SHA1Managed algorithm you must be instantiated directly like this "objAlg = new SHA1Managed()"

        Dim objAlg As HashAlgorithm = Nothing
        objAlg = HashAlgorithm.Create(strAlgorigthm)

        ' Convert string to bytes
        Dim objData As Byte() = System.Text.Encoding.[Default].GetBytes(strPassword)

        ' Generate the hash code of password
        Dim objHashBytes As Byte() = objAlg.ComputeHash(objData)

        Response.Write("<b>Hash Code : </b>")
        Response.Write(BitConverter.ToString(objHashBytes))

Output :
(To view original image , click on image)

You can also use other algorithms like RIPEMD160 or RIPEMD-160 ,SHA or SHA1 ,SHA256 or SHA-256 ,SHA384 or SHA-384,SHA512 or SHA-512.

Here is details of hashing algorithms available in .Net Framework. Click Here...


Only SHA1Managed algorithm can not be implemented using the factory approach.
It must be instantiated directly like this "hashAlg = new SHA1Managed()".

This is very useful .Net Tips.

For Beginning .Net articles. Click Here...

Note : Give Us your valuable feedback in comments. Give your suggestions in this article so we can update our articles accordingly that.

No comments:

Post a Comment