C# encryption and decryption
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 (*.*)
catch (Exception ex)
MessageBox.Show(ex.Message);
private void button2_Click(object sender, EventArgs e)
//Decrypt File
try
";
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);
2 Answers
2
Try the following code and let me know if it resolves the problem.
//EncryptFile();
try
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "All Files (*.*)
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:
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.
I have change your encryption code a bit just copy it to your encryption button click and then try again
– koolprasad2003
Apr 19 '14 at 6:16
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
same error sir , didn't help
– user3518032
Apr 19 '14 at 5:19