User Id :    Password :      New Member   Forgot Password
 
How to encrypt password or data in c#
Description This code snippet is useful for encrypting password or any other data.   No. of Views     2950
  Rating     2
Author Sumit Gupta   Posted On     12 Aug 2011
Tags C#,C# : How To,General    

Sample Code   Download Code

Using the small function, you can encrypt the password and store it in database and again when you need to compare the password, encrypt the password again that user typed and compare with previouly encrypted password.

Here is the code:

 

using System.Text;
using System.Security.Cryptography;

 public static string EncryptPassword(string strPassword)
    {
        MD5CryptoServiceProvider p = new MD5CryptoServiceProvider();
        byte[] arr = Encoding.UTF8.GetBytes(strPassword);
        arr = p.ComputeHash(arr);
        StringBuilder sb = new StringBuilder();
        foreach (byte b in arr)
        {
            sb.Append(b.ToString("x2").ToLower());
        }
        return sb.ToString();
    }

 

Happy Coding...

About Author

About Author I am cool and fun loving guy and enjoy my work. I love music and play table-tennis.... Sumit Gupta
No Photo
 
Country India
Company BrickRed Technologies Pvt. Ltd.
Home Page http://www.facebook.com/sumitgupta1225

Rate this article

Rating options from poor, fair, good, very good to excelent.  
 

Comments

 
 
Posted By Annonymous on 13 Aug 2011 at 03:48 PM
 
First that is a hash, not encryption. Second MD5 is not a secure hash. Third you should at least use a salt. Forth repetitive rounds are strongly encouraged (PKDF2). Fifth BitConverter.ToString will hex encode the bytes. Sixth Convert.ToBase64String would produce a smaller string.
 
 
     
Write your comment here.  
Comment
Verification Code   
  
    
 
Section sponsored by