2014-05-11 9 views
5

ho salato e hash i miei dati di accesso according to this Code Project articlepassword hashing utilizzato per il lavoro, ora non posso entrare nel mio software

Quando ho fatto questo, la password e il sale campi del database erano colonne appena varchar. I dati sono stati visualizzati in SQL Server Management Studio come punti interrogativi.

Un amico è arrivato e ha cambiato le colonne in tipo di dati nvarchar e ora i dati sembrano essere una raccolta di lettere/parole asiatiche - presumo che non abbia alcun senso alle spalle nemmeno a qualcuno che legge qualunque lingua sia.

Il problema è che ora, anche quando ho creato un nuovo utente, ogni tentativo di accesso non riesce.

Inoltre l'utente ed effettuare il login

public User Login() // returns an instance of its own class 
    { 

     // get the salt value for the user 
     var auser = ie.users.FirstOrDefault(u => String.Compare(u.emailaddress, emailaddress, false) == 0); 
     if (auser == null) 
     { 
      throw new ValidationException("User not found"); 
     } 

     // hash the login for comparison to stored password hash 
     HashGenerator h = new HashGenerator(password, auser.usersalt); 
     string hash = h.GetHash(); 

     var us = ie.users.FirstOrDefault(u => String.Compare(u.emailaddress, emailaddress, false) == 0 && String.Compare(u.password, password, false) == 0); 

     if (us == null) 
     { 
      throw new Exception("Invalid email address and/or password."); // seems here's where it goes wrong 
     } 
     User user = new User(); 

     user.userid = us.userid; 
     user.storeid = us.storeid; 
     user.role = us.role; 

     return user; // login succeeded, return the data to the calling method 
    } 

    public void Add() 
    { 
     user newuser = new user(); 
     newuser.storeid = storeid; 
     newuser.emailaddress = emailaddress; 

     // Generate password hash 
     string usersalt = SaltGenerator.GetSaltString(); 
     HashGenerator hash = new HashGenerator(password, usersalt); 
     newuser.password = hash.GetHash(); 

     newuser.role = role; 
     newuser.usersalt = usersalt; 

     ie.users.Add(newuser); 
     ie.SaveChanges(); 
    } 

Hash e Salt Generator

public class HashGenerator 
{ 
    public string pass { get; protected set; } 
    public string salt { get; protected set; } 

    public HashGenerator(string Password, string Salt) 
    { 
     pass = Password; 
     salt = Salt; 
    } 

    public string GetHash() 
    { 
     SHA512 sha = new SHA512CryptoServiceProvider(); 
     byte[] data = CryptUtility.GetBytes(String.Format("{0}{1}", pass, salt)); 
     byte[] hash = sha.ComputeHash(data); 

     return CryptUtility.GetString(hash); 
    } 
} 

public static class SaltGenerator 
{ 
    private static RNGCryptoServiceProvider provider = null; 
    private const int saltSize = 128; 

    static SaltGenerator() 
    { 
     provider = new RNGCryptoServiceProvider(); 
    } 

    public static string GetSaltString() 
    { 
     byte[] saltBytes = new byte[saltSize]; 

     provider.GetNonZeroBytes(saltBytes); 

     return CryptUtility.GetString(saltBytes); 
    } 
} 

utilità Crypt per ottenere le stringhe e byte

class CryptUtility 
{ 
    public static byte[] GetBytes(string Str) 
    { 
     byte[] bytes = new byte[Str.Length * sizeof(char)]; 
     System.Buffer.BlockCopy(Str.ToCharArray(), 0, bytes, 0, bytes.Length); 
     return bytes; 
    } 

    public static string GetString(byte[] Bytes) 
    { 
     char[] chars = new char[Bytes.Length/sizeof(char)]; 
     System.Buffer.BlockCopy(Bytes, 0, chars, 0, Bytes.Length); 
     return new string(chars); 
    } 
} 

L'accesso utilizzato per funzionare e questo codice non è cambiato da allora ... L'unica cosa che è stata modificata è che la password e le colonne salate nella tabella utenti sono ora nvarchar anziché varchar.

Detto questo, ho pensato di creare un nuovo utente con il metodo Add sopra, ma l'accesso con l'utente viene a mancare pure.

Sono veramente in perdita quanto a come risolvere questo problema. Non posso procedere allo sviluppo del resto del sistema se non riesco ad accedervi per testare.

Ogni aiuto sarà molto apprezzato!

+0

Hai provato cambiando le colonne torna a 'varchar'? – Tim

+0

La conversione indietro dà un avvertimento sulla possibile perdita di dati, quindi non – Ortund

+0

Fare una copia del database, eseguire la conversione su 'varchar' e vedere se funziona. Molto bene potrebbe essere la perdita di dati, ma l'intero sforzo dovrebbe essere piuttosto veloce e potrebbe funzionare. – Tim

risposta

0

humm non si dovrebbe essere usando la variabile

hash

per il confronto?

// hash the login for comparison to stored password hash 
    HashGenerator h = new HashGenerator(password, auser.usersalt); 
    string hash = h.GetHash(); 

    var us = ie.users.FirstOrDefault(u => String.Compare(u.emailaddress, emailaddress, false) == 0 && String.Compare(u.password, hash, false) == 0); 
+0

La accetterò come risposta perché probabilmente è quello che è andato storto. Da allora mi sono spostato su questo ... https://crackstation.net/hashing-security.htm#aspsourcecode – Ortund

0

Eseguire un backup di db e riconvertire varchar. O rollback al backup prima della conversione. Sembra che il tuo amico abbia fatto qualcosa di sbagliato durante la conversione del tipo di dati in nvarchar.

+0

Ho eseguito il backup del database e cambiato nuovamente in varchar. Non riesco ancora ad accedere – Ortund

+0

Chiedi al tuo amico cosa ha fatto e se ha fatto un backup prima del suo cambiamento. Forse il set di caratteri di db dovrebbe essere cambiato. – Peter

+0

Ho visto cosa ha fatto ...Aveva remoted nel mio sistema con teamviewer mentre stavamo discutendo il mio sistema su una chiamata Skype – Ortund

Problemi correlati