2016-11-21 55 views
0

傢伙,我是新來的這個網站,我是一個真正的新編程任何幫助將是太棒了!謝謝以下是我的加密和解密代碼。圖像和PDF文件得到加密和解密沒有問題,但文本文件我不知道它解密後沒有在文件中顯示任何內容。當我解密文本文件的文本文件變空

private void encrypt (string input, string output, string strHash) 

{ 
    FileStream inStream, outStream; 
    CryptoStream CryStream; 

    TripleDESCryptoServiceProvider TDC = new TripleDESCryptoServiceProvider(); 
    MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); 

    byte[] byteHash, byteTexto; 

    inStream = new FileStream(input, FileMode.Open, FileAccess.Read); 
    outStream = new FileStream(output, FileMode.OpenOrCreate, FileAccess.Write); 

    byteHash = md5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(strHash)); 
    byteTexto = File.ReadAllBytes(input); 

    md5.Clear();enter code here 

    TDC.Key = byteHash; 
    TDC.Mode = CipherMode.ECB; 

    CryStream = new CryptoStream(outStream, TDC.CreateEncryptor(), CryptoStreamMode.Write); 

    int bytesRead; 
    long length, position = 0; 
    length = inStream.Length; 

    while(position < length) 
    { 
     bytesRead = inStream.Read(byteTexto, 0, byteTexto.Length); 
     position += bytesRead; 

     CryStream.Write(byteTexto, 0, bytesRead); 

    } 

    inStream.Close(); 
    outStream.Close(); 
} 

private void Form1_Load(object sender, EventArgs e) 
{ 

} 

private void btnDecrypt_Click(object sender, EventArgs e) 
{ 
    OpenFileDialog open = new OpenFileDialog(); 
    open.ShowDialog(); 

    txtLocalDecFile.Text = open.FileName; 

    SaveFileDialog save = new SaveFileDialog(); 
    save.ShowDialog(); 

    txtNewDecFile.Text = save.FileName; 

    decrypt(txtLocalDecFile.Text, txtNewDecFile.Text, key); 

} 

private void decrypt(string input, string output, string strHash) 
{ 
    FileStream inStream, outStream; 
    CryptoStream CryStream; 
    TripleDESCryptoServiceProvider TDC = new TripleDESCryptoServiceProvider(); 
    MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); 

    byte[] byteHash, byteTexto; 

    inStream = new FileStream(input, FileMode.Open, FileAccess.Read); 
    outStream = new FileStream(output, FileMode.OpenOrCreate, FileAccess.Write); 

    byteHash = md5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(strHash)); 
    byteTexto = File.ReadAllBytes(input); 

    md5.Clear(); 

    TDC.Key = byteHash; 
    TDC.Mode = CipherMode.ECB; 

    CryStream = new CryptoStream(outStream, TDC.CreateDecryptor(), CryptoStreamMode.Write); 

    int bytesRead; 
    long length, position = 0; 
    length = inStream.Length; 

    while (position < length) 
    { 
     bytesRead = inStream.Read(byteTexto, 0, byteTexto.Length); 
     position += bytesRead; 

     CryStream.Write(byteTexto, 0, bytesRead); 

    } 

    inStream.Close(); 
    outStream.Close(); 
} 
+0

除了一個例子:什麼是與未使用'strHash' /'byteHash'? – Cameron

回答

2

您忘記關閉您的CryptoStream,這意味着它無法將所有數據都刷新到文件。

可以像關閉inStream和outStream一樣關閉流,也可以在CryStream上調用FlushFinalBlock()。

文檔中有一個example

+0

當我插入CryStream.close(); mscorlib.dll發生未處理的類型'System.ObjectDisposedException'的異常。 – Tantrix1

+0

我從字面上甚至沒有任何錯誤處理太即時太糟糕了..即時通訊將不得不學習如何做到這一點。 – Tantrix1