views:

271

answers:

1

I want to set System.Web.Security.Membership.HashAlgorithmType (or thru web.config) to a custom Cryptography class I created, In this function I have Encrypt and Decrypt functions I wanna map the property value to use this class.

How do I do it.

PS. I don't mind to change the crypto class' struct, my point is using a custom crypto class.

Thanks in advance.

A: 

I placed the following into the MembershipProvider implementation:

string PasswordEncryptionKey = "the Key"; //should be set somewhere else
internal static byte[] EncryptPassword(string password)
{
    MD5CryptoServiceProvider hash = new MD5CryptoServiceProvider();
    byte[] key = hash.ComputeHash(
                    UTF8Encoding.UTF8.GetBytes(PasswordEncryptionKey));
    hash.Clear();

    RijndaelManaged rm = new RijndaelManaged();
    rm.Key = key;
    rm.Mode = CipherMode.ECB;
    rm.Padding = PaddingMode.PKCS7;

    ICryptoTransform transform = rm.CreateEncryptor();
    byte[] bytes = UTF8Encoding.UTF8.GetBytes(password);
    byte[] result = transform.TransformFinalBlock(bytes, 0, bytes.Length);
    rm.Clear();
    return result;
}

internal new static string DecryptPassword(byte[] encodedPassword)
{
    MD5CryptoServiceProvider hash = new MD5CryptoServiceProvider();
    byte[] key = hash.ComputeHash(
                          UTF8Encoding.UTF8.GetBytes(PasswordEncryptionKey));
    hash.Clear();

    RijndaelManaged rm = new RijndaelManaged();
    rm.Key = key;
    rm.Mode = CipherMode.ECB;
    rm.Padding = PaddingMode.PKCS7;

    ICryptoTransform transform = rm.CreateDecryptor();
    byte[] result = transform.TransformFinalBlock(
                              encodedPassword, 0, encodedPassword.Length);
    rm.Clear();
    return UTF8Encoding.UTF8.GetString(result); ;
}
Shimmy