简体   繁体   English

将ASP.NET会员密码从加密转换为哈希

[英]Convert ASP.NET Membership Passwords from Encrypted to Hashed

I've developed a website that uses ASP.NET membership. 我已经开发了一个使用ASP.NET成员资格的网站。 Based on comments from previous sites, I decided to encrypt passwords so they could be recovered for users who forgot them. 根据先前站点的评论,我决定对密码进行加密,以便对忘记密码的用户进行恢复。

However, the new site (which now has over 500 registered users) has brought me some criticism that the industry standard is really to hash passwords. 但是,新站点(现在有500多个注册用户)给我带来了一些批评,即行业标准实际上是对密码进行哈希处理。

However, after a fairly extensive search, I have been unable to find anything about how to convert existing users' passwords from encrypted to hashed. 但是,经过相当广泛的搜索之后,我无法找到有关如何将现有用户密码从加密密码转换为哈希密码的任何信息。

I know I can change the web.config file, and new users' passwords will use the new format. 我知道我可以更改web.config文件,新用户的密码将使用新格式。 But it does nothing to update the existing users. 但是它并没有更新现有用户。

Note: I previously asked a similar question but mostly just got a debate about which is better, encrypted or hashed. 注意:之前我曾问过类似的问题,但大多数人只是在争论哪个更好,经过加密或散列。 I'm past that discussion but I've been unable to find a way to convert them without losing the hundreds of users already registered. 我已经超出讨论范围,但是一直无法找到一种转换方式而又不会失去数百个已注册用户的方法。

it seems you already know how to decrypt the passwords and change the web.config file, but you're stuck with how to implement the rest of the process. 看来您已经知道如何解密密码并更改web.config文件,但是您仍然无法实现其余的过程。

using ILSpy , here's how to generate the salt for each user: 使用ILSpy ,这是为每个用户生成盐的方法:

byte[] array = new byte[16];
new RNGCryptoServiceProvider().GetBytes(array);
return Convert.ToBase64String(array);    

once you have the salt, here's how to generate the password: 一旦加了盐,就可以生成密码:

byte[] bytes = Encoding.Unicode.GetBytes(pass);
byte[] array = Convert.FromBase64String(salt);
byte[] array2 = new byte[array.Length + bytes.Length];
Buffer.BlockCopy(array, 0, array2, 0, array.Length);
Buffer.BlockCopy(bytes, 0, array2, array.Length, bytes.Length);   
using (SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider()) {
  return Convert.ToBase64String(sha1.ComputeHash(array2));
}

where pass is the plain-text password you calculated, and salt is the string calculated in the first code snippet above. 其中pass计算出的纯文本密码, salt是上面第一个代码段中计算出的字符串。 the default algorithm is SHA1 , if you're wondering why it's being used. 如果您想知道为什么使用默认算法 ,则默认算法为SHA1

since this is a one-time process, i would write a HTTP handler to manually update the database during a short, scheduled maintenance period - hopefully you have that luxury. 由于这是一个一次性的过程,因此我将编写一个HTTP处理程序以在较短的计划维护期内手动更新数据库-希望您有这么多奢侈。 (obviously make a backup and test first). (显然先进行备份和测试)。 you need to update the following fields in the aspnet_Membership table: 您需要更新aspnet_Membership表中的以下字段:

  1. Password - calculated above Password -以上计算
  2. PasswordFormat - 1 PasswordFormat -1
  3. PasswordSalt - calculated above PasswordSalt以上计算

never had to do anything like this, but hopefully that will get you started :) 从来没有做过这样的事情,但希望这可以帮助您入门:)

Maybe I'm missing something here, but it should be pretty simple. 也许我在这里错过了一些东西,但是应该很简单。 Create a process to decrypt the password, then salt accordingly and store the hash of the salt + user's decrypted password in the database. 创建一个解密密码的过程,然后相应地对salt进行加密,并将salt +用户的解密密码的哈希值存储在数据库中。 Obviously you don't want to be hashing the user's encrypted password. 显然,您不想散列用户的加密密码。 Don't forget to store the salt too. 别忘了储存盐。

IMHO, Greg's response (and the associated comments) on your previous question ( Changing passwordFormat from Encrypted to Hashed ) is the way to go. 恕我直言,格雷格(Greg)对您先前的问题( 将passwordFormat从Encrypted更改为Hashed )的答复(以及相关评论)是解决方法。 Essentially, you want to: 本质上,您要:

  1. Add a hashed membership provider 添加哈希成员资格提供程序
  2. Loop through all of the encrypted password users, 遍历所有加密的密码用户,
  3. For each one decrypt the password, create the hash, store it, delete the encrypted version from the database, and move on. 对于每个密码,都要解密,创建哈希,进行存储,从数据库中删除加密版本,然后继续。

When you are done, all of the encrypted password users should be converted to hashed. 完成后,所有加密的密码用户都应转换为哈希。

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

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