【发布时间】:2018-02-14 19:24:39
【问题描述】:
我创建了一个网站,在登录时我使用 pbkdf2_sha256 进行密码散列。我也用过盐。 我想制作一个简单的软件只是为了体验,我想使用与网站保存的相同凭据登录 c# 软件。 我见过 Rfc2898DeriveBytes 我猜它只需要 2 个参数(密码,整数中的盐)。但是我在网站上指定的迭代呢?
任何人,请指导我如何在 c# (WPF) 应用程序中登录并使用 pbkdf2_sha256 创建哈希并验证密码。
我在 stacksoverflow.com 上看到了一个代码。
var salt = "FbSnXHPo12gb";
var password = "geheim";
var interactions = 12000;
using (var hmac = new HMACSHA256())
{
var df = new Pbkdf2(hmac, password, salt, interactions);
Console.WriteLine(Convert.ToBase64String(df.GetBytes(32)));
}
我也使用过这个,但在var df = new Pbkdf2(hmac, password, salt, interactions); 中出现错误
找不到 pbkdf2。
我的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MySql.Data.MySqlClient;
using System.IO;
using System.Security.Cryptography;
namespace login
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void login_Click(object sender, RoutedEventArgs e)
{
var salt = "FbSnXHPo12gb";
var password = "geheim";
var interactions = 12000;
using (var hmac = new HMACSHA256())
{
var df = new Pbkdf2(hmac, password, salt, interactions);
Console.WriteLine(Convert.ToBase64String(df.GetBytes(32)));
}
string myConnection = "datasource=localhost;port=3306;username=root;password=abcde12345 ; database=finalproject";
MySqlConnection myConn = new MySqlConnection(myConnection);
MySqlCommand SelectCommand = new MySqlCommand("select * from login where Username='" + this.username.Text + "' and Password='" + this.password.Password + "';", myConn);
MySqlDataReader myReader;
myConn.Open();
myReader = SelectCommand.ExecuteReader();
int count = 0;
while (myReader.Read())
{
count = count + 1;
}
if (count == 1)
{
MessageBox.Show("Hello");
}
else
{
MessageBox.Show("Wrong username and password");
}
myConn.Close();
}
}
}
所以请告诉我应该在服务器上使用哪种散列算法对密码有用。我读过关于 bcrypt 和 scyrpt 的文章。它们适合密码哈希吗?实际上,我不是在解密,我只是在软件中对密码进行哈希处理,然后将存储在服务器上的哈希值与软件中生成的哈希值进行比较
请帮助我。 有错误请见谅。
【问题讨论】:
-
也许我弄错了,但“Pbkdf2”不都是小写的吗?可能意味着它没有找到它的原因。
-
您引用的特别 PBKDF2 实现仅在 .NET Core 中可用,在完整的 .NET 框架中不可用。要在框架中使用 PBKDF2,您需要寻找 System.Security.Cryptography.Rfc2898DeriveBytes。它确实有一个 ctor 重载,允许您指定迭代次数。
-
Xander 是对的。 Rfc2898DeriveBytes 是你想要的类。要么,要么搜索实现Argon2 绑定的 NuGet 包,因为 Argon2 是当前最先进的。
-
其实我不是在解密,我只是在软件中对密码进行哈希处理,然后将存储在服务器上的哈希值与软件中生成的哈希值进行比较。
-
System.Security.Cryptography.Rfc2898DeriveBytes 这是一个命名空间,我用过这个,但错误还是一样。