2012-11-30 90 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