2014-04-19 138 views
0

在c#.net中,当我试图解密文件时,它向我显示这个错误,但它适用于加密。C#加密和解密

错误:该进程无法访问该文件SecureDownloadManager.log,因为它正被另一个进程使用

代码:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.Security; 
using System.Security.Cryptography; 
using System.IO; 
using System.Text.RegularExpressions; 
using System.Runtime.InteropServices; 

namespace Encryption 
{ 
public partial class Form1 : Form 
{ 
string inputFile; 
string outputFile; 
public Form1() 
{ 
InitializeComponent(); 

} 

private void button1_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 button2_Click(object sender, EventArgs e) 
{ //Decrypt File 
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; 
outputFile = inputFile; 

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

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

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

RijndaelManaged RMCrypto = new RijndaelManaged(); 

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

FileStream fsOut = new FileStream(outputFile, FileMode.Open, FileAccess.Write, 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

同样的错误先生,没有帮助。 – user3518032

回答

0

尝试用下面的代码,只是删除你的加密代码并添加以下代码,并让我知道它是否解决

//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; 
       using (FileStream fsCrypt = new FileStream(cryptFile, FileMode.Create)) 
       { 
        RijndaelManaged RMCrypto = new RijndaelManaged(); 

        using (CryptoStream cs = new CryptoStream(fsCrypt, 
         RMCrypto.CreateEncryptor(key, key), 
         CryptoStreamMode.Write)) 
        { 
         using (FileStream fsIn = new FileStream(inputFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) 
         { 
          int data; 
          while ((data = fsIn.ReadByte()) != -1) 
           cs.WriteByte((byte)data); 
         } 
        } 

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

的故障原因,由于对象是不完全设置,您需要在使用后使用'using'子句释放对象。 你可以用下面的代码,而不是使用“FILESTREAM”

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

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

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

使用它来代替上面的代码

System.IO.File.WriteAllBytes(outputFile); 

希望解决您的问题

+0

同样的错误先生,没有帮助 – user3518032

+0

我已经改变你的加密代码有点复制它到你的加密按钮点击,然后再试一次 – koolprasad2003