Simple and efficient password encryption in .NET

Sep 11, 2006

Four years ago, I was working as a developer in a company that made custom content management systems. We offered back-ups of the databases to our customers on a monthly basis, but in those databases we stored all the users’ login information. The customer was of course happy about getting all the passwords, but I saw that as a severe security breach.

So, I convinced my boss to let me implement password encryption using a very simple but powerful tool – one way encryption. One way encryption means that you can encrypt a value but you cannot decrypt it again, so no one was able to retrieve the actual passwords.

This is the simple method I used for encryption.

using System.Text;

using System.Security.Cryptography;

 

/// <summary>

/// Encrypts a string that cannot be decrypted. It is encrypted one-way in order

/// to make it impossible to decrypt. The purpose of this mehod could be to hide

/// password information from potential hackers and even the system administrators.

/// </summary>

/// <param name="plainMessage">The string you want to encrypt.</param>

/// <returns>A MD5 hashed byte array as a base64 string.</returns>

public string Encrypt(string plainMessage)

{

  using (MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider())

  {

    byte[] hashedDataBytes = md5Hasher.ComputeHash(Encoding.UTF8.GetBytes(plainMessage));

    return Convert.ToBase64String(hashedDataBytes);

  }

}

Example of use

The use of this method is pretty simple. The password field in the database contained the encrypted password and every time a user tried to authenticate I would encrypt the password and query the database using the encrypted string. This is a dummy method that does just that.

private bool Login(string password)

{

  string encryptedPassword = Encrypt(password);

  // Query database for encrypted password

  return CanAuthenticate(encryptedPassword);

}

If I pass my own name to the Encrypt method this would be the result:

Encrypt("Mads Kristensen ") --> “DHi8pBS6u7fWewZBHO9WPw==”.

Now the database was filled with totally unreadable values that effectively shielded the right passwords.

UPDATE septemper 12

As some of the comments point out, the MD5 algorithm is not secure enough. So, I wrote a new Encrypt method using the SHA256 algorithm.

* $4.95/month BlogEngine.net Hosting – Click Here!

Comments (7) -

Jesper
Jesper
9/12/2006 6:27:04 AM #

Man, this is exactly what I need (in a completely different project) - perfect.

I really, really, really appreciate this blog Laughing

 Mads Kristensen
Mads Kristensen
9/12/2006 7:59:32 AM #

Jesper, I'm very glad to hear that.

 Jos&#233; Rui Abreu Mira
José Rui Abreu Mira
9/12/2006 8:15:34 AM #

By using MD5 what you are really doing is calculating the password's hash (http://en.wikipedia.org/wiki/Md5), not encrypting the password. By encrypting a value you should be able to, somehow, decrypt it.
BTW, MD5 is no longer a trusted method to calculate hashes since it is vulnerable to collision algorithms. It is advised to use at the least the SHA-256 hash algorithm (http://en.wikipedia.org/wiki/SHA_hash_functions) because attacks have been found for both SHA-0 and SHA-1.

 Eber Irigoyen
Eber Irigoyen
9/12/2006 4:37:50 PM #

you would want to use salted passwords

www.securitytechnique.com/.../8-2

Mads Kristensen
Mads Kristensen
9/12/2006 6:11:21 PM #

I like the idea of salted passwords, but they introduce a new problem. Where do you store the salt key in a secure way?

 Eber Irigoyen
Eber Irigoyen
9/13/2006 12:43:03 AM #

the point is that if they steal your database or somehow have access to it, they could try dictionary attacks, if you use salted passwords, then those attacks wouldn't do anything, is just about adding an extra layer of protection

 Mike Ward
Mike Ward
9/13/2006 7:39:05 PM #

The answer to where to store the salt value is simple. Store it with the hash. There is no reason to hide the salt value since it is there only force an attacker to build a new dictionary. The salt code should be different everytime you write a new password.

Comments are closed

About the author

Mads Kristensen

Mads Kristensen
Program Manager at the Microsoft Web Platform team and founder of BlogEngine.NET.

More...

Month List

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer’s view in any way.