2010-11-09 16 views
1

Je les codes suivants pour vérifier md5 mot de passe crypté contre le mot de passe d'entrée utilisateur:La vérification de mot de passe MD5 renvoie toujours false?

  UserDAO userDAO = new UserDAO(); 

      // encrypt the input password 
      MD5 md5 = new MD5CryptoServiceProvider(); 
      UTF8Encoding encoder = new UTF8Encoding(); 
      Byte[] encryptedPassword; 
      encryptedPassword = md5.ComputeHash(encoder.GetBytes(TxtBoxPassword.Text)); 

      // get information for this username and begin checking authentication 
      DataTable data = userDAO.GetUserInformation(TxtBoxUsername.Text); 
      if (data.Rows.Count == 0) 
      { 
       LblError.Text = "Wrong username!"; 
       return; 
      } 
      Byte[] password = (Byte[])data.Rows[0]["Password"]; 

      if (!Convert.ToBase64String(password).Equals(Convert.ToBase64String(encryptedPassword))) 
      { 
       LblError.Text = "Wrong password!"; 
       return; 
      } 

Le problème est que je peux exécuter ce code très bien sur mon ordinateur (validé correctement admin/123456) alors que lorsque je publie mon site à un serveur, le contrôle renvoie toujours "mot de passe incorrect"? Ce qui donne?

Répondre

3

Je ne sais pas pourquoi la vôtre ne fonctionne pas, mais quand j'ai écrit l'implémentation de SHA512 ci-dessous, j'ai eu quelques problèmes avec le hachage. Il ne sort pas comme vous le verriez normalement pour les humains. Pour cette raison, votre type de données doit être binaire dans la base de données. Voici également la mise en œuvre que j'utilise (avec le sel modifié) qui utilise SHA512. L'utilisation de ByteArrayToHexString le place dans un format humain reconnaissable. Ensuite, vous pouvez utiliser un varchar dans la base de données.

/// <summary> 
    /// Takes a string as input, SHA512 hashes it, and returns the hexadecimal representation of the hash as a string. 
    /// </summary> 
    /// <param name="toHash">string to be hashed</param> 
    /// <returns>hexadecimal representation of the hash as a string</returns> 
    private string GetHash(string toHash) 
    { 
     /* As of this writing, both the –Cng and –CryptoServiceProvider implementation classes are FIPS-certified, 
     * but –Managed classes are not. http://msdn.microsoft.com/en-us/magazine/ee321570.aspx 
     */ 
     // Salt the string 
     toHash = "%my" + toHash.Insert(Convert.ToInt16(toHash.Length/2), "!secret") + ".sauce#"; 
     SHA512CryptoServiceProvider hasher = new SHA512CryptoServiceProvider(); 
     byte[] hashBytes = hasher.ComputeHash(Encoding.Unicode.GetBytes(toHash)); 
     hasher.Clear(); 
     return ByteArrayToHexString(hashBytes); 
    } 

    /// <summary> 
    /// Takes a byte[] and converts it to its string hexadecimal representation 
    /// </summary> 
    /// <param name="ba">Array of bytes[] to convert</param> 
    /// <returns>string, hexadecimal representation of input byte[]</returns> 
    private string ByteArrayToHexString(byte[] ba) 
    { 
     StringBuilder hex = new StringBuilder(ba.Length * 2); 
     foreach (byte b in ba) 
      hex.AppendFormat("{0:x2}", b); 
     return hex.ToString(); 
    } 
0

L'octet [] est-il utilisé dans le db? Pouvez-vous vous connecter le hachage quand il va dans la base de données et l'enregistrer quand vous l'apportez ici et voir si elles sont égales? De plus, notez que MD5 est considéré comme faible et que vous ne salez pas les mots de passe. Si une violation de données devait se produire, cela pourrait facilement entraîner des comptes compromis. Envisagez d'utiliser SHA1 avec du sel aléatoire.

0

Le mot de passe était-il en UTF8 avant d'être haché et enregistré dans UserDAO?