简体   繁体   中英

How to configure ASP.NET MembershipProvider with custom cryptography?

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 this?

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

Thanks in advance.

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); ;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM