약간의 암호화를 수행하는 몇 가지 방법이 있으며 항상 비교할 값에 \n
을 추가합니다.SHA256이 값에 n을 더합니다.
줄 바꿈 문자 때문일까요?
public static class Security
{
public static string CreateHash(this string unHashed)
{
SHA256CryptoServiceProvider x = new SHA256CryptoServiceProvider();
byte[] data = Encoding.UTF8.GetBytes(unHashed);
data = x.ComputeHash(data);
return Encoding.UTF8.GetString(data);
}
public static bool MatchHash(this string hashData, string hashUser)
{
if (hashUser == hashData)
{
return true;
}
else
{
return false;
}
}
}
I (모든 테스트) 하나 개의 텍스트 상자에 값을 입력하고 다른 입력란에 값을 입력 CreateHash()
을 실행하고 있습니다. 그런 다음 다른 텍스트 상자에서 내 비교 검사를 실행합니다.
protected void btn_dehash_Click(object sender, EventArgs e)
{
// get value entered and hash it
var matchPass = txt_password.Text.CreateHash();
// populate lbl with true or false
lbl_Name.Text = txt_hashed.Text.MatchHash(matchPass) ? "true" : "false";
}
MD5
에서 얻은 결과는 매우 짧습니다. 더 안전한 방법을 사용하여 SHA256을 사용한 결과가이 비교 결과입니다. 이런 일이 왜
는 사람은 알고 있나요?
문자열을 비교하는 데'=='을 사용하지 말고'.equals()' –