2012-11-30 141 views
1

我可以使用以下代碼加密位於桌面上的文本文件。文件加密和解密c#Rijndael密碼

private void btnEncrypt_Click(object sender, EventArgs e) 
    { 
     //EncryptFile(); 
     try 
     { 
      OpenFileDialog dialog = new OpenFileDialog(); 
      dialog.Filter = "All Files (*.*)|"; 
      dialog.InitialDirectory = @"Desktop"; 
      dialog.Title = "Please select a file to encrypt."; 

      dialog.ShowDialog(); 

      inputFile = dialog.FileName; 

      outputFile = inputFile; 

      string password = @"myKey123"; // Your Key Here 
      UnicodeEncoding UE = new UnicodeEncoding(); 
      byte[] key = UE.GetBytes(password); 

      string cryptFile = outputFile; 
      FileStream fsCrypt = new FileStream(cryptFile, FileMode.Create); 

      RijndaelManaged RMCrypto = new RijndaelManaged(); 

      CryptoStream cs = new CryptoStream(fsCrypt, 
       RMCrypto.CreateEncryptor(key, key), 
       CryptoStreamMode.Write); 

      FileStream fsIn = new FileStream(inputFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); 

      int data; 
      while ((data = fsIn.ReadByte()) != -1) 
       cs.WriteByte((byte)data); 


      fsIn.Close(); 
      cs.Close(); 
      fsCrypt.Close(); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
     } 

這個偉大的工程,但是當我嘗試解密同一個文本文件沒有任何反應,沒有錯誤消息被拋出,讓我知道是怎麼回事,我用下面的代碼解密

private void btnDecrypt_Click(object sender, EventArgs e) 
    { 
     try 
     { 
      OpenFileDialog dialog = new OpenFileDialog(); 
      dialog.Filter = "All Files (*.*)|"; 
      dialog.InitialDirectory = @"Desktop"; 
      dialog.Title = "Please select a file to decrypt."; 

      dialog.ShowDialog(); 

      inputFile = dialog.FileName; // "C:\\Users\\daniel\\Desktop\\text.txt"; 
      outputFile = inputFile; 


       string password = @"myKey123"; // Your Key Here 

       UnicodeEncoding UE = new UnicodeEncoding(); 
       byte[] key = UE.GetBytes(password); 

       FileStream fsCrypt = new FileStream(inputFile, FileMode.Open); 

       RijndaelManaged RMCrypto = new RijndaelManaged(); 

       CryptoStream cs = new CryptoStream(fsCrypt, 
        RMCrypto.CreateDecryptor(key, key), 
        CryptoStreamMode.Read); 

       FileStream fsOut = new FileStream(outputFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); 

       int data; 
       while ((data = cs.ReadByte()) != -1) 
        fsOut.WriteByte((byte)data); 

       fsOut.Close(); 
       cs.Close(); 
       fsCrypt.Close(); 

      } 
     catch(Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
     } 

     } 
    } 

任何人都可以幫忙嗎?

+0

在這種情況下,「偉大工程」意味着「運行但完全不安全」。 – CodesInChaos

+0

當'outputFile!= inputFile'時它解密嗎? –

+0

這是一個後續http://stackoverflow.com/questions/13647456/rijndael-cipher-error-in-c-sharp-windows-form – CodesInChaos

回答

0

fsOut作爲只讀流打開。試試這個。

FileStream fsOut = new FileStream(
     outputFile, 
     FileMode.Open, 
     FileAccess.Write, 
     FileShare.ReadWrite); 

:我也寫一個臨時文件並複製了原來的完成時,但我沒有很大理由。

+0

嗨奧斯汀,我會稍後嘗試,因爲我沒有我的筆記本電腦,我會讓你知道我如何得到 – user1849946