简体   繁体   中英

C# encryption and decryption

In c#.net, when im trying to decrypt the file it shows me this error but it works for encryption.

ERROR: The process cannot access the file SecureDownloadManager.log because it is being used by another process

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.

You can use the following code that uses FileStream instead:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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