2012-10-28 99 views
1

我是一名學生,我是編程新手。這是我試圖編寫的第一個C#程序。該程序應該以明文形式讀入文本文件,然後輸出帶有MD5和SHA-1散列的文本文件。該程序工作,據我所知,但由我的程序創建的哈希不匹配在線哈希生成器生成的哈希。據我所知,如果輸入的單詞是相同的,哈希應該匹配。我的代碼有什麼明顯的錯誤會導致這種情況?CryptoServiceProvider散列輸出與在線散列生成器不匹配?

using System; 
using System.Text; 
using System.IO; 
using System.Security.Cryptography; 

namespace CreateHash 
{ 
class Program 
{ 
    const string INPUTFILE = "C:\\ClearTextPasswords.txt"; 
    const string OUTPUTFILEMD5 = "C:\\EncryptedMD5Passwords.txt"; 
    const string OUTPUTFILESHA1 = "C:\\EncryptedSHA1Passwords.txt"; 

    static void Main(string[] args) 
    { 
     string input; 
     int counter = 0; 

     DeleteWriteFile(); 

     StreamReader readFile = new StreamReader(INPUTFILE); 

     while ((input = readFile.ReadLine()) != null) 
     { 
      if(AppendFile(input)) 
       counter++; 
     } 

     readFile.Close(); 

     Console.WriteLine("\nSuccessfully wrote {0} encrypted password(s) to: \n{1} and\n{2} files.\n\nPress any key to exit...", counter.ToString(), OUTPUTFILEMD5, OUTPUTFILESHA1); 
     Console.ReadKey(); 
    } 

    static void DeleteWriteFile() 
    { 
     //Example of try/catch block 
     try 
     { 
      System.IO.File.Delete(OUTPUTFILEMD5); 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine("There was an error deleting the MD5 Output file: " + ex.Message); 
     } 

     try 
     { 
      System.IO.File.Delete(OUTPUTFILESHA1); 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine("There was an error deleting the MD5 Output file: " + ex.Message); 
     } 
    } 

    static bool AppendFile(string str) 
    { 
     int x = 0; 
     int y = 0; 
     bool result = false; 

     StreamWriter writeFile1; 
     StreamWriter writeFile2; 

     HashToolkit hashToolkit = new HashToolkit(); 

     //Example of try/catch/finally block 
     try 
     { 
      writeFile1 = new StreamWriter(OUTPUTFILEMD5, true); 
      writeFile1.WriteLine(hashToolkit.GetMd5(str)); 
      writeFile1.Close(); 
      x = 1; 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine("There was an error writing to the MD5 Output file: " + ex.Message); 
     } 
     finally 
     { 
      writeFile1 = null; 
     } 

     try 
     { 
      writeFile2 = new StreamWriter(OUTPUTFILESHA1, true); 
      writeFile2.WriteLine(hashToolkit.GetSha1(str)); 
      writeFile2.Close(); 
      y = 1; 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine("There was an error writing to the SHA1 Output file: " + ex.Message); 
     } 
     finally 
     { 
      writeFile2 = null; 
     } 

     if (x * y != 0) 
      result = true; 

     return result; 
    } 
} 

public class HashToolkit 
{ 
    public HashToolkit() 
    { 
    } 

    public string GetMd5(string str) 
    { 
     MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); 
     string input = string.Empty; 

     byte[] hashedData = md5.ComputeHash(Encoding.Unicode.GetBytes(str)); 

     foreach (byte b in hashedData) 
     { 
      input += String.Format("{0,2:X2}", b); 
     } 
     return input; 
    } 

    public string GetSha1(string str) 
    { 
     SHA1 sha = new SHA1CryptoServiceProvider(); 
     string input = string.Empty; 

     byte[] hashedData = sha.ComputeHash(Encoding.Unicode.GetBytes(str)); 

     foreach (byte b in hashedData) 
     { 
      input += String.Format("{0,2:X2}", b); 
     } 
     return input; 
    } 
} 
} 

任何幫助將不勝感激。感謝您的時間。

+0

您在散列之前將文件轉換爲UTF-16-LE。爲什麼你解碼首先是作爲文本,而不是直接使用字節?或者,您可以使用'Encoding.UTF8'而不是'Encoding.Unicode'。 – CodesInChaos

+0

而不是遍歷字節,只需使用['ToBase64String'](http://msdn.microsoft.com/en-us/library/dhx0d524%28v=vs.100%29.aspx) –

回答

3

這是因爲你不能散列字符串,你只能散列字節。出於這個原因,你需要先轉換爲字節。你正在使用UTF16。在線工具可能使用UTF8或某種ASCII風格的編碼。找出它使用和使用它。