首页
学习
活动
专区
圈层
工具
发布

C#加解密
EN

Stack Overflow用户
提问于 2014-04-19 05:02:18
回答 2查看 1K关注 0票数 0

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

错误:进程无法访问文件SecureDownloadManager.log,因为它正被其他进程使用。

代码:

代码语言:javascript
复制
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);
}

}
}
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-04-19 06:23:44

试试下面的代码,如果它解决了问题,请告诉我。

代码语言:javascript
复制
//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
EN

Stack Overflow用户

发布于 2014-04-19 05:12:45

此错误是由于未完全释放对象造成的。您需要在使用后使用“using”子句来释放对象。

您可以使用以下使用FileStream的代码:

代码语言:javascript
复制
System.IO.File.WriteAllBytes(outputFile);

它取代了:

代码语言:javascript
复制
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();

我希望它能解决你的问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23166077

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档