Tech
Forums
Jobs
Books
Events
Interviews
Live
More
Learn
Training
Career
Members
Videos
News
Blogs
Contribute
Article
Blog
Video
Ebook
Interview Question
.NET
.NET Core
.NET MAUI
.NET Standard
Active Directory
ADO.NET
Agile Development
AI
AJAX
AlbertAGPT
Alchemy
Alexa Skills
Algorand
Algorithms in C#
Android
Angular
ArcObject
ASP.NET
ASP.NET Core
Augmented Reality
Avalanche
AWS
Azure
Backbonejs
Big Data
BizTalk Server
Blazor
Blockchain
Bootstrap
Bot Framework
Business
Business Intelligence(BI)
C#
C# Corner
C# Strings
C, C++, MFC
Career Advice
Careers and Jobs
Chapters
ChatGPT
Cloud
Coding Best Practices
Cognitive Services
COM Interop
Compact Framework
Copilot
Cortana Development
Cosmos DB
Cryptocurrency
Cryptography
Crystal Reports
CSS
Current Affairs
Custom Controls
Cyber Security
Data Mining
Data Science
Databases & DBA
Databricks
Design Patterns & Practices
DevExpress
DevOps
DirectX
Dynamics CRM
Enterprise Development
Entity Framework
Error Zone
Exception Handling
F#
Files, Directory, IO
Flutter
Games Programming
GDI+
General
Generative AI
GO
Google Cloud
Google Development
Graphics Design
Graphite Studio
Hardware
Hiring and Recruitment
HoloLens
How do I
HTML 5
Infragistics
Internet & Web
Internet of Things
Ionic
Java
Java and .NET
JavaScript
JQuery
JSON
JSP
Knockout
Kotlin
Langchain
Leadership
Learn .NET
Learn iOS Programming
LINQ
Machine Learning
Metaverse
Microsoft 365
Microsoft Fabric
Microsoft Office
Microsoft Phone
Microsoft Teams
Mobile Development
MongoDB
MuleSoft
MySQL
NEAR
NetBeans
Networking
NFT
NoCode LowCode
Node.js
Office Development
OOP/OOD
Open Source
Operating Systems
Oracle
Outsourcing
Philosophy
PHP
Polygon
PostgreSQL
Power Apps
Power Automate
Power BI
Power Pages
Printing in C#
Products
Progress
Progressive Web Apps
Project Management
Public Speaking
Python
Q#
QlikView
Quantum Computing
R
React
React Native
Reports using C#
Robotics & Hardware
RPA
Ruby on Rails
RUST
Salesforce
Security
Servers
ServiceNow
SharePoint
Sharp Economy
SignalR
Smart Devices
Snowflake
Software Architecture/Engineering
Software Testing
Solana
Solidity
Sports
SQL
SQL Server
Startups
Stratis Blockchain
Swift
SyncFusion
Threading
Tools
TypeScript
Unity
UWP
Visual Basic .NET
Visual Studio
Vue.js
WCF
Wearables
Web API
Web Design
Web Development
Web3
Windows
Windows Controls
Windows Forms
Windows PowerShell
Windows Services
Workflow Foundation
WPF
Xamarin
XAML
XML
XNA
XSharp
Register
Login
0
Answer
pbkdf2 problem when compare password with the hash on mysql
zanyar halabjay
5y
1.3k
1
Reply
i have a problem when i want to compare my password with that password that hashed in mysql database and always say incorrect while i login it is my code for hashing and comparing:
class
Hashing
{
const
int
salt_size = 32;
const
int
hash_size = 32;
const
int
iteration = 167319;
public
static
string
Generate(
string
password)
{
var salt =
new
byte
[salt_size];
using
(RNGCryptoServiceProvider rng =
new
RNGCryptoServiceProvider()) {
rng.GetBytes(salt);
}
using
(Rfc2898DeriveBytes pbkdf2 =
new
Rfc2898DeriveBytes(password, salt, iteration))
{
byte
[] hash = pbkdf2.GetBytes(salt_size);
return
Convert.ToBase64String(salt) +
"|"
+ iteration +
"|"
+ Convert.ToBase64String(hash);
}
}
public
static
bool
isCorrect(
string
pass,
string
hash)
{
string
[] hashsplit = hash.Split(
'|'
);
byte
[] salt = Convert.FromBase64String(hashsplit[0]);
int
iteration = Int32.Parse(hashsplit[1]);
string
hashed = hashsplit[2];
using
(Rfc2898DeriveBytes pbkdf2 =
new
Rfc2898DeriveBytes(pass,salt,iteration))
{
byte
[] Hash = pbkdf2.GetBytes(salt_size);
if
(hashed == Convert.ToBase64String(Hash))
{
return
true
;
}
else
{
return
false
;
}
}
}
}
and this code is for checking username and password from mysql:
class
Lg : Msql_connection
{
private
string
username {
set
;
get
; }
private
string
pass {
set
;
get
; }
public
bool
validate_Login(
string
username,
string
pass)
{
bool
check =
false
;
MySqlDataReader mdr;
MySqlDataReader mdr2;
MySqlDataReader mdr3;
string
passw =
""
;
using
(MySqlCommand mcmd3 =
new
MySqlCommand())
{
mcmd3.CommandText =
"select password from login"
;
mcmd3.Connection = msc;
msc.Close();
msc.Open();
mdr3 = mcmd3.ExecuteReader();
if
(mdr3.Read())
{
passw = mdr3[
"password"
].ToString();
}
msc.Close();
}
using
(MySqlCommand mcmd2 =
new
MySqlCommand())
{
mcmd2.CommandText =
"select hid,attempt,time from login_attempt"
;
mcmd2.Connection = msc;
msc.Close();
msc.Open();
mdr2 = mcmd2.ExecuteReader();
if
(mdr2.Read()&&
int
.Parse(mdr2[
"attempt"
].ToString()) < 4)
{
using
(MySqlCommand mcmd =
new
MySqlCommand())
{
mcmd.CommandText =
"select username,password from login where binary username=@user and password=@pass"
;
mcmd.Connection = msc;
msc.Close();
msc.Open();
mcmd.Parameters.Add(
"@user"
, MySqlDbType.VarChar).Value =
this
.username=username;
mcmd.Parameters.Add(
"@pass"
, MySqlDbType.Text).Value =
this
.pass = Hashing.isCorrect(pass, passw).ToString();
mdr = mcmd.ExecuteReader();
if
(mdr.HasRows)
{
if
(mdr.Read())
{
reset_Attempt();
MessageBox.Show(
"correct"
);
check =
true
;
}
}
else
{
check =
false
;
update_Attempt();
MessageBox.Show(
"incorrect"
);
}
}
}
else
if
(
int
.Parse(mdr2[
"attempt"
].ToString()) >= 4)
{
check =
false
;
MessageBox.Show(
"You have been restrict"
);
set_time();
}
}
msc.Close();
return
check;
}
}
Post
Reset
Cancel
Answers (
0
)
Next Recommended Forum
How to mange multiple logins for same user ASP.NET MVC
Implement Cross Site Request Forgery