Implementing Crypto is deceptively easy, and actually quite tedious, there are a lot of details, and the details are wrong, as a rule, they use a security vulnerability. The best practice is to use a high-level encryption system that hides this ivs, salt, mac data, comparisons, additions, key rotation, and although it is unlikely for high-level frameworks that the details are incorrect when they do, they are detected and fixed, fragments code when stack overflows usually do not work.
I ported the Google Keyczar framework , so such a high-level library would exist for C #.
Keyczar-dotnet
And it can be used to encrypt and decrypt io streams.
Install nuget in your project
PM> Install-Package Keyczar -Pre
Then create your own set of keys. (Having a separate key set file, it gives you the ability to rotate keys in the future and prevents you from accidentally hard-coding something that should never be hard-coded.)
PM> KeyczarTool.exe create --location=path_to_key_set --purpose=crypt PM> KeyczarTool.exe addkey --location=path_to_key_set --status=primary
Then in your code, you can use any I / O stream you want for both encryption:
using(var encrypter = new Encrypter("path_to_key_set")) { encrypter.Encrypt(plaintextStream, ciphertextStream); }
and decryption:
using(var crypter = new Crypter("path_to_key_set")) { crypter.Decrypt(ciphertextStream, plaintextStream); }