Click here to Skip to main content
Click here to Skip to main content

Password Encryption using MD5 Hash Algorithm in C#

, 21 Sep 2012
Rate this:
Please Sign up or sign in to vote.
You know, it’s really bad if someone knew your password. As a developer or web master or something else, we have an ethic that we also don’t want to see other’s password. So, what we need to do to store those passwords? One simple solution is using encryption. We change the readable password into un
MD5 Hash Algorithm
You know, it’s really bad if someone knew your password. As a developer or web master or something else, we have an ethic that we also don’t want to see other’s password. So, what we need to do to store those passwords? One simple solution is using encryption. We change the readable password into unreadable text. One of the popular (or simple) method is MD5 Hash Algorithm.

How does MD5 Hash Algorithm Works? The point is that this algorithm change your password into 32 hexadecimal digits. It doesn’t care how long is your password and it’ll become 32 hexadecimal digits. The next question is, how can we do that? That’s a stupid question. If you want to make the code by yourself then you should learn it here. Because I’m a .NET developer, I knew that .NET provides Cryptography Framework.
Okay, here is the code that you can use to generate MD5 Hash (of course in C#):
using System.Text;
using System.Security.Cryptography;
 
namespace CryptoLib
{
    public static class Encryptor
    {
        public static string MD5Hash(string text)
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            
            //compute hash from the bytes of text
            md5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(text));

            //get hash result after compute it
            byte[] result = md5.Hash;
 
            StringBuilder strBuilder = new StringBuilder();
            for (int i = 0; i < result.Length; i++)
            {
                //change it into 2 hexadecimal digits
                //for each byte
                strBuilder.Append(result[i].ToString("x2"));
            }
 
            return strBuilder.ToString();
        }
    }
}

I use that function and try it in my WPF Application and here is the result:

image

Okay, that’s all I got this time. Hope this simple tutorial helps you. See you on my next post Open-mouthed.

Read More from CodeProject.com


License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Junian Triajianto
Software Developer
Indonesia Indonesia
Just an ordinary geek.
 
For more information visit my blogs:
Junian Legacy
Kodefun, learning by coding
Follow on   Twitter   Google+

Comments and Discussions

 
Questionthanks PinmemberRichard Ellicott16-Apr-14 4:25 
QuestionNot working with Unicode string Pinmembertonybui_vn9-Oct-13 17:01 
Questionhow to decrpt after encrption PinprofessionalOmprakash Kukana25-Sep-13 20:25 
Questionmantap Pinmembergadasadox24-Aug-13 5:13 
QuestionHow encrypt with a challenge code ?? PinmemberYesudasan Moses15-Aug-13 4:58 
Suggestionmd5 isn't secure anymore PinmemberMember 945446624-Sep-12 3:52 
GeneralRe: md5 isn't secure anymore PinmemberChad3F30-Sep-12 21:29 
GeneralRe: md5 isn't secure anymore PinmemberMember 94544663-Oct-12 11:20 
GeneralRe: md5 isn't secure anymore PinmemberChad3F3-Oct-12 13:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140709.1 | Last Updated 22 Sep 2012
Article Copyright 2012 by Junian Triajianto
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid