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     4271
  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 Sumit Gupta working in 3 Pillar Global Pvt. Ltd as Module Lead. I have 7+ year of experience in .Net technologies. I love to explore new technologies and write technical article. Sumit Gupta
No Photo
 
Country India
Company 3 Pillar Global 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