File Encryption in C#

Soldato
Joined
18 Oct 2002
Posts
16,080
Location
The land of milk & beans
I've managed to find loads of code to encrypt text files and literal strings, but googling has turned up nothing which is able to encrypt Word documents, images, videos (avi, mpg, wmv) amongst others.

Could anyone point me in the right direction of what I should be looking at?

Ta muchly.
 
.NET's encryption libs work against byte arrays or streams (depending on the cipher) so all you need to do is open the file as a stream and pass either the stream or the read bytes off the stream in the required blocksize to the cipher. Then collect your encrypted bytes or stream and write it out to a new file.
 
Why not use a pre-existing, certified encryption library and not reinvent the wheel? Plus I doubt that you (or anyone else) would find a better encryption algorithm than those already available. :)

If it's for practice though, then fine :)
 
.NET's encryption libs work against byte arrays or streams (depending on the cipher) so all you need to do is open the file as a stream and pass either the stream or the read bytes off the stream in the required blocksize to the cipher. Then collect your encrypted bytes or stream and write it out to a new file.

This.

An easy one to start with is MD5; there's loads of guides floating around and it will introduce you to the crypto libraries and reading in byte arrays etc.
 
Depending on what you want to do - you could just XOR each byte against a key string unless you need industrial grade encryption.
 
Depending on what you want to do - you could just XOR each byte against a key string unless you need industrial grade encryption.

Not sure that's really a good idea, c# makes it really easy to encrypt stuff with strong ciphers in just a few lines of code. Messing about with xor without it being a perfect one time pad is fairly useless and you might as well just ROT13.

OP, look into the crypto classes as said and read your file in bit by bit and pass it to a block cipher like AES using CBC mode. Ill dig up an example if you want, but it's very little code and worth doing it properly.

MD5 is not encryption. It is an irreversible hash. :)

Irreversible is a maybe a bit too stronger word to use with md5 these days :p
 
Back
Top Bottom