简体   繁体   English

C#加密密码

[英]C# Encrypted Password

I have a password field for a user, that when it is saved to a database, the password is encrypted. 我为用户提供了一个密码字段,该字段在保存到数据库时会被加密。

Also, if i go to edit a user, the password field is hashed. 另外,如果我去编辑用户,密码字段将被散列。

But the hashed field is hundreds of characters long, due to the length of the encryption. 但是由于加密的长度,散列字段的长度为数百个字符。

Is there any way of showing maybe just 8 characters instead? 有没有办法显示也许只是8个字符呢?

this is what is displayed after saving, which isn't ideal. 这是保存后显示的内容,并不理想。

加密

Can't you make a separate option to change password and in the edit user password field show something like ********* . 您不能单独更改密码吗?在“编辑用户密码”字段中显示诸如********* And if during user editing you enter something new into a password field, then go again thru encryption. 并且,如果在用户编辑期间您在密码字段中输入了一些新内容 ,然后通过加密再次进行。

There are several things that are worrying me here, one of which is that, as RB notes in the comments, you're using the terms Encryption and Hashing interchangeably. 这里让我担心的是几件事,其中之一是,正如RB在评论中指出的那样,您正在互换使用术语“加密”和“哈希”。 The difference is pretty important. 区别非常重要。 In simple terms, 简单来说,

  • Encryption is when you obscure something in a way that can be reversed. 加密是指您以某种可以逆转的方式遮盖某些东西。 An example (a bad example mind you) is substituting the letter A with the number 1, the letter B with the number 2 and so on. 有一个例子(请记住,这是一个不好的例子)是用数字1代替字母A,用数字2代替字母B,依此类推。 "Hello" would become "7 5 12 12 15", which would look meaningless to somebody without the key, but is reversible if you know how. “ Hello”将变成“ 7 5 12 12 15”,对于没有钥匙的人来说,这看起来毫无意义,但如果您知道怎么做,则可以逆转。
  • Hashing is a one way mathematical function, which takes an input and turns it into a unique piece of seemingly meaningless output. 散列是一种单向数学函数,它接受输入并将其转换为看似无意义的独特输出。 In simple terms, you can pretty much assume that every unique piece of information that goes into a hash will come out as a unique value. 简而言之,您几乎可以假设进入哈希的每个唯一信息都将作为唯一值出现。 But, given that unique output, there is no way to reverse it and discover what the input was. 但是,给定唯一的输出,就没有办法将其反转并发现输入是什么。

To expand further on the above, if I was creating a website where a user was asked to sign up, I would take the password that they chose, I would then hash that password and then store that password somewhere safe. 为了进一步扩展上述内容,如果我正在创建一个要求用户注册的网站,我将使用他们选择的密码,然后对该密码进行哈希处理,然后将该密码存储在安全的地方。 I would then throw away the password the user actually entered. 然后,我将丢弃用户实际输入的密码。 Next time that user came to my site, they would enter their password, which I would again hash, I would then check that hash output against what I had saved earlier, and since the same word will always produce the same output, if the two hashes matched I would grant access to my site. 下次该用户访问我的网站时,他们将输入密码,我将再次对其进行哈希运算,然后我将根据先前保存的内容检查该哈希运算输出,并且由于相同的单词将始终产生相同的输出,如果两者相同哈希匹配,我将授予访问我的网站的权限。

I would also urge you to find out about salting passwords (more on which can be found at Wikipedia) which is pretty much a must-have these days. 我还敦促您找出有关加盐密码的信息(有关更多信息,请参阅Wikipedia) ,这几乎是当今必不可少的。

Now, on to your actual question. 现在,讨论您的实际问题。 Again (and I hope RB doesn't mind me expanding upon his excellent comments) what you are seeing in your password fields (the * ** ) is just a way for the browser to obscure your password from prying eyes. 再次(并且我希望RB不要介意我对他的出色评论进行扩展),您在密码字段( * ** )中看到的内容只是浏览器掩盖您的密码而不会被撬开的一种方式。 The fact that it looks like stars does not make it safe. 它看起来像星星的事实并不能保证其安全。 Further, you should only ever read from this input field. 此外,您应该只从该输入字段中读取内容。 You should never write to it. 你永远不要写它。 Writing anything to your password field lets an attacker know information about what kind of hash or security that you are using. 在密码字段中写入任何内容,可使攻击者知道有关您使用的是哪种哈希或安全性的信息。

The best thing you could do would be to leave the password field on your user edit screen blank. 最好的办法是将用户编辑屏幕上的密码字段保留为空白。 If MVC is autopopulating that for you then it suggests to me that you should change the name of the input box to something that doesn't tie in with your underlying object. 如果MVC为您自动填充了它,那么它向我建议您应该将输入框的名称更改为与基础对象无关的名称。 More about security in MVC can be found on Microsoft's asp.net website . 有关MVC安全性的更多信息,请访问Microsoft的asp.net网站

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

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