歷史記錄用戶輸入和存儲的數據之間的不同:Password hashing used to work, now I can't get into my software字節值
自寫上面的問題(基本上,只是無法登錄,不知道爲什麼),我確定因爲當我在當我創建的用戶記錄我輸入密碼登錄我輸入的密碼字節數組,是完全不同的:
Here's the bytes from the database
Here's the bytes from the password when I failed to logged in
現在我知道該值被傳遞到數據庫(或存儲在中的值)數據庫)是錯誤的,我不知道該如何修復它...
我甚至不知道爲什麼這用於工作,但突然中斷。
在此先感謝您的任何建議!
EDIT
這裏的用戶登錄和註冊代碼
public User Login() // returns an instance of its own class
{
var auser = ie.users.FirstOrDefault(u => String.Compare(u.emailaddress, emailaddress, false) == 0);
if (auser == null)
{
throw new ValidationException("User not found");
}
// get bytes for password in the database
byte[] passwordbytes = CryptUtility.GetBytes(auser.password);
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.");
}
User user = new User();
user.userid = us.userid;
user.storeid = us.storeid;
user.role = us.role;
return user;
}
public void Add() // user registration
{
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();
}
這裏的安全碼以生成散列和鹽值和它們的字節/字符串值以及
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);
}
}
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);
}
}
我們需要更多的細節,添加您的代碼進行登錄和註冊。 –
我猜想你用它的lib應該已經改變了!但是你需要支持更多關於你如何做的信息。 – prnjanuario
@BertrandC。添加了代碼ya – Ortund