Simple Text File Encryption (C#)

Can someone provide me an example of how to encrypt a text file that already exists... and also an example to unencrypt it? It doesnt have to be the strongest thing in the world... I just want it to be a step above setting the file parameter to "hidden". Thanks.
Pretzel_JesusAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jandromedaCommented:
jandromedaCommented:
Here you go. I have tested this code.
// Encrypt file
			DESCryptoServiceProvider desCrypto = (DESCryptoServiceProvider) DESCryptoServiceProvider.Create();
			string key = ASCIIEncoding.ASCII.GetString(desCrypto.Key);
			
			FileStream fsInput = new FileStream(@"C:\InputFile.txt", FileMode.Open, FileAccess.Read);
			FileStream fsEncrypted = new FileStream(@"C:\EncryFile.enc", FileMode.Create, FileAccess.Write);
			
			ICryptoTransform desEncrypt = desCrypto.CreateEncryptor();
			CryptoStream cryptoStream = new CryptoStream(fsEncrypted, desEncrypt, CryptoStreamMode.Write);
			
			byte[] byteArrayInput = new byte[fsInput.Length - 1];
			fsInput.Read(byteArrayInput, 0, byteArrayInput.Length);
			cryptoStream.Write(byteArrayInput, 0, byteArrayInput.Length);
			cryptoStream.Close();
			fsEncrypted.Close();
			fsInput.Close();
			
			// Decrypt the file
			FileStream fsRead = new FileStream(@"C:\EncryFile.enc", FileMode.Open, FileAccess.Read);
			
			ICryptoTransform desDecrypt = desCrypto.CreateDecryptor();
			CryptoStream decryptoStream = new CryptoStream(fsRead, desDecrypt, CryptoStreamMode.Read);
			
			StreamWriter fsDecrypted = new StreamWriter(@"C:\OutputFile.txt");
			StreamReader reader = new StreamReader(decryptoStream);
			string fileText = reader.ReadToEnd();
			fsDecrypted.Write(fileText);
			fsDecrypted.Close();
			decryptoStream.Close();
			fsRead.Close();

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Pretzel_JesusAuthor Commented:
Exactly what I needed thanks!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.