Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 149953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T09:12:45+00:00 2026-05-11T09:12:45+00:00

i’m doing a test how hash and salt passwords. Well , i can add

  • 0

i’m doing a test how hash and salt passwords. Well , i can add hash and salt password to the Database but i got stuck to store passwords from database. i have a simple Database :

                                Table                                _______                                 ProvaHS                                --------                           (PK) LoginID   int                                 UserName  nvarchar(50)                                Password  nvarchar(50)                                Salt      nvarchar(50) 

So i create a form to add new record to the database with this code:

 public partial class Window1 : Window {     public Window1()     {         InitializeComponent();     }      #region SALT     public static class PasswordCrypto     {         private static SHA1CryptoServiceProvider Hasher = new SHA1CryptoServiceProvider();         //Private Hasher As New MD5CryptoServiceProvider()          static internal string GetSalt(int saltSize)         {             byte[] buffer = new byte[saltSize + 1];             RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();             rng.GetBytes(buffer);             return Convert.ToBase64String(buffer);         }          static internal string HashEncryptString(string s)         {             byte[] clearBytes = Encoding.UTF8.GetBytes(s);             byte[] hashedBytes = Hasher.ComputeHash(clearBytes);             return Convert.ToBase64String(hashedBytes);         }          static internal string HashEncryptStringWithSalt(string s, string salt)         {             return HashEncryptString(salt + s);         }     }     #endregion      private void GetSalt()     {         this.textBoxSalt.Text = PasswordCrypto.GetSalt(16);     }        private void GetSaltHash()       {           // It's how i salt and hash the password before to save it to the Database           this.textBoxPassword.Text = PasswordCrypto.HashEncryptStringWithSalt(this.textBoxClear.Text, this.textBoxSalt.Text);       }      private void GetHash()     {         //Demo purposes -- this is an unsalted hash         this.textBoxClear.Text = PasswordCrypto.HashEncryptString(this.textBoxPassword.Text);     }      private void Add(object sender, RoutedEventArgs e)     {         DataClasses1DataContext dc = new DataClasses1DataContext();          try         {             if (textBoxUserName.Text.Length > 0)             {                 ProvaH tab = new ProvaH();                 tab.UserName = textBoxUserName.Text;                 tab.Password = textBoxPassword.Text;                 tab.Salt = textBoxSalt.Text;                 dc.ProvaHs.InsertOnSubmit(tab);                 dc.SubmitChanges();              }         }         catch (Exception ex)         {              MessageBox.Show('Error!!!');         }      }      private void HashButton(object sender, RoutedEventArgs e)     {         GetHash();     }      private void SaltButton(object sender, RoutedEventArgs e)     {         GetSalt();     }      private void HashSaltButton(object sender, RoutedEventArgs e)     {         GetSaltHash();     }      private void Close_W(object sender, RoutedEventArgs e)     {         this.Close();     }  } 

}

  • with this method i can salt,hash and save password to the database..(following advices StackOverflow’s member ) thanks..

Now i’m testing how store password from the database and here i got a trouble…

  public partial class Login : Window {     public Login()     {         InitializeComponent();     }     #region SALT     public static class PasswordCrypto     {         private static SHA1CryptoServiceProvider Hasher = new SHA1CryptoServiceProvider();         //Private Hasher As New MD5CryptoServiceProvider()          static internal string GetSalt(int saltSize)         {             byte[] buffer = new byte[saltSize + 1];             RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();             rng.GetBytes(buffer);             return Convert.ToBase64String(buffer);         }          static internal string HashEncryptString(string s)         {             byte[] clearBytes = Encoding.UTF8.GetBytes(s);             byte[] hashedBytes = Hasher.ComputeHash(clearBytes);             return Convert.ToBase64String(hashedBytes);         }          static internal string HashEncryptStringWithSalt(string s, string salt)         {             return HashEncryptString(salt + s);         }     }     #endregion      private void closs(object sender, RoutedEventArgs e)     {         this.Close();     }         public bool ValidateApplicationUser(string userName, string password)       {          bool OK = false;           DataClasses1DataContext dc = new DataClasses1DataContext();            object saltValue = from c in dc.ProvaHs where c.UserName == userName  select c.Salt;           if (!(saltValue == System.DBNull.Value))           {               password = PasswordCrypto.HashEncryptStringWithSalt(passwordTextBox.Password, saltValue.ToString());            }           var query = from c in dc.ProvaHs where c.UserName == userName && c.Password == password select new { c.LoginID, c.UserName, c.Password };           if (query.Count() != 0)           {               return true;           }        return false;       }      private void Confirm(object sender, RoutedEventArgs e)     {            bool authenticated = true;          if (usernameTextBox.Text != '' && passwordTextBox.Password.ToString() != '')             {                authenticated = ValidateApplicationUser(usernameTextBox.Text, passwordTextBox.Password.ToString());            }            if (!authenticated)            {                MessageBox.Show('Invalid login. Try again.');            }            else            {                MessageBox.Show('Aaaaahhhh.JOB DONE!!!!....');            }     } } 

when i debug the application i receive always an error at this code line : if (query.Count() != 0) in ‘query’ = Empty :’Enumeration yielded no results’ Do you have any suggest how work out this error and store password from database in my case? Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T09:12:46+00:00Added an answer on May 11, 2026 at 9:12 am

    Try:

    public bool ValidateApplicationUser(string userName, string password) {   DataClasses1DataContext dc = new DataClasses1DataContext();    var saltValue = dc.ProvaHs.Where(c => c.UserName == userName)                             .Select(c => c.Salt)                             .SingleOrDefault();    if (saltValue == null) return false;    password = PasswordCrypto.HashEncryptStringWithSalt(passwordTextBox.Password, saltValue.ToString());    return dc.ProvaHs.Any(c => c.UserName == userName && c.Password == password); } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 268k
  • Answers 268k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Site Owner are a very special permission (actually they are… May 13, 2026 at 1:08 pm
  • Editorial Team
    Editorial Team added an answer That's the compiler that comes with Apple's XCode tools package.… May 13, 2026 at 1:08 pm
  • Editorial Team
    Editorial Team added an answer More likely than not GD is not installed correctly for… May 13, 2026 at 1:08 pm

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want use html5's new tag to play a wav file (currently only supported
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I've got a string that has curly quotes in it. I'd like to replace
In order to apply a triggered animation to all ToolTip s in my app,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.