Skip to content
James Craig edited this page Aug 28, 2014 · 3 revisions

The encryption system, deals with encrypting strings and byte arrays. Generally there are only a couple of extension methods that you will care about when dealing with this system. First is the Encrypt extension method:

"This is a test of the system.".Encrypt(new PasswordDeriveBytes("MyPassword".ToByteArray(), "MySalt".ToByteArray(), "SHA1", 2));

The encrypt extension attaches to either a string or byte array and will encrypt it using the DerivedBytes key that you pass in. In this case we're using a PasswordDeriveBytes object that has MyPassword for the password, MySalt for the salt, hashes the password using SHA1, and uses 2 iterations. This is then used for the AES encryption algorithm, using the system's defaults. Ideally you would want to set your own. Similarly you can use decrypt to get the string back:

"This is a test of the system.".Encrypt(new PasswordDeriveBytes("MyPassword".ToByteArray(), "MySalt".ToByteArray(), "SHA1", 2)).Decrypt(new PasswordDeriveBytes("MyPassword".ToByteArray(), "MySalt".ToByteArray(), "SHA1", 2));

The code above will encrypt and then unencrypt the string. The system is generic enough that you can specify any of the supported algorithms in .Net, so AES, DES, Triple DES, Rijndael, and RC2 are all supported. You can implement your own also, as long as it implements the SymmetricAlgorithm class in .Net.

Along with encryption, hashing is also made easier by a couple of extension methods. The first is Hash. The Hash extension attaches to a string or byte array and will hash that using the algorithm that you specify. All you have to do is specify the name of the hash algorithm. All hash algorithms implemented in .Net are supported including SHA1, SHA256, SHA384, SHA512, HMACSHA1, HMACSHA256, HMAC384, HMAC512, HMACMD5, HMACRIPEMD160, MACTRIPLEDES, MD5, and RIPEMD160. By default the system uses SHA1:

"This is a test of the system".Hash();

This returns a hash of the string using the SHA1 algorithm.

Clone this wiki locally