2013-01-01 1 views
2

저는 Visual Studio C# windows form 프로젝트를 만들고 있습니다. 그러나 SHA256 + 소금물이 어떻게 작용하는지 혼란 스럽습니다. 온라인에서 몇 가지 예를 발견했지만 어떻게이 함수를 호출 할 수 있는지 이해할 수 없습니다.소금으로 SHA256 해시를 만드는 방법은 무엇입니까?

데이터베이스 (Microsoft Access 2010)에 연결하는 로그인 양식에서이 함수를 호출하고 싶습니다.

  • 은 어떻게 버튼을 클릭하고 Textbox에서 암호를 읽어이 함수를 호출합니까?
  • 어떻게 해시 값을 Messagebox.Show 방법으로 표시합니까? (내 테스트 목적으로)
  • 두 텍스트 (해시 및 소금에 절인)를 비교하고 긍정적 인 결과를 줄 수 있습니까?

    public static string sha256encrypt(string phrase, string UserName) 
    { 
        string salt = CreateSalt(UserName); 
        string saltAndPwd = String.Concat(phrase, salt); 
        UTF8Encoding encoder = new UTF8Encoding(); 
        SHA256Managed sha256hasher = new SHA256Managed(); 
        byte[] hashedDataBytes =  sha256hasher.ComputeHash(encoder.GetBytes(saltAndPwd)); 
        string hashedPwd = String.Concat(byteArrayToString(hashedDataBytes), salt); 
        return hashedPwd; 
    } 
    
    public static string byteArrayToString(byte[] inputArray) 
    { 
        StringBuilder output = new StringBuilder(""); 
        for (int i = 0; i < inputArray.Length; i++) 
        { 
         output.Append(inputArray[i].ToString("X2")); 
        } 
        return output.ToString(); 
    } 
    
    private static string CreateSalt(string UserName) 
    { 
        string username = UserName; 
        byte[] userBytes; 
        string salt; 
        userBytes = ASCIIEncoding.ASCII.GetBytes(username); 
        long XORED = 0x00; 
    
        foreach (int x in userBytes) 
         XORED = XORED^x; 
    
        Random rand = new Random(Convert.ToInt32(XORED)); 
        salt = rand.Next().ToString(); 
        salt += rand.Next().ToString(); 
        salt += rand.Next().ToString(); 
        salt += rand.Next().ToString(); 
        return salt; 
    } 
    

어떻게 소금과 SHA256 해시를 만들려면 어떻게해야합니까?

shavalue = (sha256encrypt("password", "username"); 
saltedandhashtext = CreateSalt(shavalue); 
+4

은 내가 읽지 않고이 코드 조각을 신뢰하는 것입니다 경우 * 모르겠어요과 이해 * 그것, 그것은 SHA-256 "암호화"를 호출하고 256 개 다른 소금 값까지 사용하기 때문에 ... –

+4

나는이 코드를 작동 시키는데 시간을 낭비하지 않을 것이다. 그것을 버리고 새로운 코드로 시작하십시오. 특히 확장 된 해시 함수를 사용하고 자체적으로 소금을 만들 수있는'Rfc2898DeriveBytes'를 사용하십시오. – CodesInChaos

+2

암호를 해싱 했으므로 CSHarp에서 PBKDF2와 같은 느린 키 파생 기능을 사용하는 것이 좋습니다. [Rfc2898DeriveBytes] (http://msdn.microsoft.com/en-us/library/system)로 구현할 수 있습니다. .security.cryptography.rfc2898derivebytes % 28v = vs.100 % 29) 클래스로, CodesInChaos가 이미 언급했듯이. – martinstoeckli

답변

2

을 CC Inc의의 대답에 첫 번째 질문의보기를 들어. 두 번째 점으로

: MessageBox.Show(sha256encrypt(textBox1.Text, "SampleUserName"));

3) 네, 그렇습니다.

== 또는 string.Equals() 두 문자열을 비교할 수 있습니다.

public bool compareHashs(string hash1, string hash2){ 
    if(hash1.Equals(hash2) //or hash1 == hash2 
     return true; 
    }else{ 
     return false; 
    } 
} 
4

버튼의 클릭에있다 할 것입니다 무엇을, 예를 들어, sha256encrypt 함수에 텍스트 상자 값과 이름을 전달합니다, 두 번째 질문에 대해

private void button1_Click(object sender, EventArgs e) 
    { 
     sha256encrypt(textBox1.Text, "SampleUserName"); 
    } 

를 동일한 작업을 수행하지만,

private void button1_Click(object sender, EventArgs e) 
    { 
     MessageBox.Show(sha256encrypt(textBox1.Text, "SampleUserName")); 
    } 

세 번째 점 : 당신이 무슨 뜻 정확히 잘 모르겠지만, 당신은 소금에 텍스트를 원하는 경우 해시 된 텍스트와 비교 :

Messagebox.Show와
if(sha256encrypt("password", "username") == CreateSalt("password")) 
    return true; 
else 
    return false; 

또는 수동으로 비교하려는 경우 :

MessageBox.Show(sha256encrypt("password", "username") + "\n\r" + CreateSalt("password")); 
+0

@ JanesAbouChleih 실제 질문에 게시 하시겠습니까? –

+0

글쎄, 내 대답을 게시 한 후에 David이 자신의 게시물을 편집하지 않았다면 ... –

관련 문제