Sunday, November 1, 2009

Cryptography

I've been playing with the System.Security.Cryptography namespace, making sure I'm familiar with the classes here.

I'm really starting to feel good about taking the .NET certification exam 70-536, think I'll do it next week. I've been studying hours every night, and going over the Microsoft Press study book by Tony Northrup, as well as just reading the MSDN .NET FCL.

Wrote the following test program that shows how to create an MD5 hash.


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


namespace SecurityTest
{
class Program
{
static void Main(string[] args)
{
String unencryptedPassword = "password";
String encryptedPassword = EncryptPassword("MD5", unencryptedPassword);
Console.WriteLine("Encrypted password is: '" + encryptedPassword + "'");
Console.ReadKey();
}

public static String EncryptPassword(String cryptAlgorithm, String unencryptedPassword)
{
HashAlgorithm hashAlgorithm = HashAlgorithm.Create(cryptAlgorithm);

byte[] hash = hashAlgorithm.ComputeHash(Encoding.UTF8.GetBytes(unencryptedPassword));

StringBuilder s = new StringBuilder();
foreach (byte b in hash)
{
s.Append(b.ToString("x2").ToLower());
}
string password = s.ToString();

return password;
}
}
}

No comments:

Post a Comment