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

发布于 2024-07-25 10:57:54 字数 197 浏览 6 评论 0原文

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

我该怎么做呢?

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

提前致谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萧瑟寒风 2024-08-01 10:57:54

我将以下内容放入 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); ;
}

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); ;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文