简体   繁体   English

如何使用C#在asp.net中加密和验证密码?

[英]How to encrypt and verify password in asp.net using C#?

I used following tutorial to hash passwords to my db. 我使用以下教程将密码散列到数据库。 I would like to ask to more experienced developers here if this method is still "up to date"? 我想在这里问更多有经验的开发人员,这种方法是否仍然是“最新的”? I wouldn't like to have security problems. 我不想遇到安全问题。

Here is the link in question: How to encrypt and decrypt password in asp.net using C#? 这里是有问题的链接: 如何使用C#在asp.net中加密和解密密码? . I modified a little bit the code so that it would always use SHA512 as hash algorithm. 我修改了一些代码,以便始终使用SHA512作为哈希算法。 I also never specify a salt but let it generate it (second parameter = null). 我也从不指定盐,而是让它生成盐(第二个参数= null)。

Thanks in advance for your help, wish you all a nice week! 在此先感谢您的帮助,祝大家一周愉快!

Greetings 问候

if this method is still "up to date"? 这种方法是否仍然是“最新的”?

Yes, in general. 是的,一般而言。

I also never specify a salt but let it generate it (second parameter = null). 我也从不指定盐,而是让它生成盐(第二个参数= null)。

Yeah. 是啊。 Now hash like a hunderd thousand times and you are ok ;) No joke. 现在像一个千百次杂乱无章的哈希,你还好;)没有玩笑。 I think minimum should be around - well, it should take a second to operate. 我认为最低值应该在附近-好吧,它需要一秒钟才能运行。

Now, here is the question you actually NEVER ASK EXCEP TIN YOUR TITLE. 现在,这是您实际上从未询问标题的问题。 How do you verify? 您如何验证?

NOT by decryption. 不是通过解密。

  • Take password enterd by user. 使用用户输入的密码。
  • Take salt from your salted password (yes, store it) 从您的加盐密码中取盐(是的,存储它)
  • Take number of iterations from your salted password 从您的固定密码中获取迭代次数
  • Salt input from user same number of times with same algorithm. 用相同算法从用户输入相同次数的盐。
  • Compare both hashes. 比较两个哈希。

Finished. 成品。

Hash are NOT encryption. 哈希不加密。 Envcryption means you can decrypt - Hashes are irreversible. 加密意味着您可以解密-哈希是不可逆的。

As I'm asp.net beginner to answer it,I used following code that might help you where you can encrypt the password and save to db and when retrieve that encrypted string from db then decrypt to match your verifying password.Following code are tested for your (pwd) cryptogrphy. 当我是asp.net的初学者时,我使用了以下代码,可能会帮助您在哪里加密密码并保存到db,并在从db中检索该加密的字符串然后解密以匹配您的验证密码。为您的(pwd)密码术。

Design File 设计文件

<form id="form1" runat="server">
<div>
    <asp:TextBox ID="TextBox1" Text="Arslan Ali" runat="server" placeHolder="Enter Password"></asp:TextBox><br />
    <asp:Button ID="Button1" runat="server" Text="Encrypt" OnClick="Button1_Click" /><br />
    <asp:Button ID="Button2" runat="server" Text="Decrypt" OnClick="Button2_Click" /><br />
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br />
</div>
</form>

In your Code File Required NameSpaces 在您的代码文件中必需的名称空间

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

Define Hash String 定义哈希字符串

    string hash = @"foxle@rn";

Encrypt 加密

protected void Button1_Click(object sender, EventArgs e)
{
    byte[] data = UTF8Encoding.UTF8.GetBytes(TextBox1.Text);
    using(MD5CryptoServiceProvider md5=new MD5CryptoServiceProvider())
    {
        byte[] keys = md5.ComputeHash(UTF8Encoding.UTF8.GetBytes(hash));
        using (TripleDESCryptoServiceProvider tripleDes = new TripleDESCryptoServiceProvider() {Key=keys,Mode=CipherMode.ECB,Padding=PaddingMode.PKCS7 })
        {
            ICryptoTransform transform=tripleDes.CreateEncryptor();
            byte[] results=transform.TransformFinalBlock(data,0,data.Length);
            Label1.Text = Convert.ToBase64String(results);
        }
    }
}

Decrypt 解码

protected void Button2_Click(object sender, EventArgs e)
{
    byte[] data = Convert.FromBase64String(Label1.Text);
    using (MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider())
    {
        byte[] keys = md5.ComputeHash(UTF8Encoding.UTF8.GetBytes(hash));
        using (TripleDESCryptoServiceProvider tripleDes = new TripleDESCryptoServiceProvider() { Key = keys, Mode = CipherMode.ECB, Padding = PaddingMode.PKCS7 })
        {
            ICryptoTransform transform = tripleDes.CreateDecryptor();
            byte[] results = transform.TransformFinalBlock(data, 0, data.Length);
            Label1.Text = UTF8Encoding.UTF8.GetString(results);
        }
    }
}

I hope so,It may help you but I'm confirming ,I'm too beginner to crypto as well as asp.net web-forms. 希望如此,它可能对您有所帮助,但我要确认,对于加密以及asp.net网络表单,我还是个新手。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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