简体   繁体   English

C#加密和解密

[英]C# encryption and decryption

In c#.net, when im trying to decrypt the file it shows me this error but it works for encryption. 在c#.net中,当我尝试解密文件时,它向我显示此错误,但可用于加密。

ERROR: The process cannot access the file SecureDownloadManager.log because it is being used by another process 错误:该进程无法访问文件SecureDownloadManager.log,因为它正在被另一个进程使用

Code: 码:

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);
}

}
}
}

The error is due to the objects not being completely disposed. 该错误是由于对象未完全处置而引起的。 You need to use a 'using' clause to release objects after use. 您需要使用“ using”子句在使用后释放对象。

You can use the following code that uses FileStream instead: 您可以使用以下使用FileStream代码代替:

System.IO.File.WriteAllBytes(outputFile);

which replaces: 替换为:

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();

I hope it resolves your issue. 希望它能解决您的问题。

Try the following code and let me know if it resolves the problem. 请尝试以下代码,让我知道它是否可以解决问题。

//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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM