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.






same error sir , didn't help

– user3518032
Apr 19 '14 at 5:19






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.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế