Basic Encryption / Decryption example

PlausiblyDamp

Administrator
Joined
Sep 4, 2002
Messages
6,471
Location
Lancashire, UK
The attached sample shows how to use the built in TripleDES provider to encrypt and decrypt a single file.

The application was written under VS 2008 but the code itself will happily run against .Net 2

After selecting a file to encrypt enter a password and hit the Generate IV button to generate a random IV for the encryption. When you decrypt the file you need to make sure both the password and IV are the same.

If you do not specify these then the system will generate these randomly each time and you will never be able to decrypt an encrypted file!

The application itself has no real error handling so make sure you provide a password and an IV otherwise it will just crash.

Depending on the actual encryption provider used there are restrictions on the length of the Key and IV - only certain sized byte arrays work (you can use the LegalKeySizes and LegalBlockSizes properties to discover what is valid), a md5 hash returns a byte array that is the correct length to use for the Key so it makes sense to use a hash of a password rather than force restrictions on password sizes. IV could be done the same way however generating a unique IV is easy and as such both methods are used in the sample.
 

Attachments

Last edited:
Back
Top