如何使用自定义加密配置ASP.NET MembershipProvider?

时间:2009-06-26 03:43:46

标签: c# asp.net asp.net-membership cryptography web-config

我想将System.Web.Security.Membership.HashAlgorithmType(或通过web.config)设置为我创建的自定义Cryptography类,在此函数中我有加密和解密函数我想映射属性值以使用此类

我该怎么做?

PS。我不介意更改加密类的结构,我的观点是使用自定义加密类。

提前致谢。

1 个答案:

答案 0 :(得分:0)

我将以下内容放入MembershipProvider实现中:

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