PDA

View Full Version : Encrypting files with a key. [C#]



alexgeek
January 28th, 2008, 06:07 PM
I'd like to know how to encrypt a text file with a key (e.g. "Alwokdo3443k##@~!es!) so that it can only be unencrypted by providing that key again.
How can I do this with C#?
thnx :D

Jeff Wheeler
January 28th, 2008, 06:40 PM
You might try PGP — it’s a great way of encrypting files. Check out the Wikipedia page; it uses public/private keys and is really neat.

I found a couple of resources regarding using PGP in C#:
http://www.codeproject.com/KB/security/gnupgdotnet.aspx
http://www.eldos.com/sbb/net-pgp.php
http://www.codeproject.com/KB/security/sharpprivacy.aspx?df=100&forumid=15716&exp=0&select=573797

alexgeek
January 28th, 2008, 06:50 PM
Thanks, if someone could show me some pseudo code that would be great.

Jeff Wheeler
January 28th, 2008, 06:57 PM
There’s tons of sample code on the first and third links; about half-way down on the former, and just a few paragraphs down on the latter.

TheColonial
February 26th, 2008, 01:04 AM
WTF? PGP!?

He's has asked for a way of encrypting and decrypting with the same key. That means symmetric encryption, not asymmetric. So anything like PGP or RSA which uses public/private key pairs isn't an option.

There are a few symmetric ciphers built into the .NET framework, including DES, TripleDES, RC2 and Rijndael.

I did a quick search (http://www.google.com.au/search?hl=en&q=.net+symmetric+encryption) and found this (http://www.c-sharpcorner.com/UploadFile/Suprotim/CryptographyInDotNET11282005053700AM/CryptographyInDotNET.aspx) which should get you going.

Good luck.

alexgeek
February 26th, 2008, 06:39 PM
Thanks mate. I'm going to see if I can create a class with it all built in so then I can do something like:


FileEncrypter enc = new FileEncrypter();
enc.EncryptWithKey([streamwriterobject], "password");

bluefootedpig
March 6th, 2008, 04:08 PM
Rijndael

go with that. if you are using C#, which i hope you are, there is a c# class for it. You can search google for like "msdn rijndael" and they have examples of it. basically what you will end up doing is something like..

using (stream s = new filestream(stuff))
{
using (StreamWriter sw = new StreamWriter(s)
{
//then i believe you need teh Rijndael stream
//so when you use the stream with rinjdeal, you do a .Write, the Rinjdeal stream will encripte it, then the streamwriter will write it to the file stream, which then when you finish it will flush it to the file.
}
}