Posted on 1245136491|%e %b %Y, %H:%M %Z|agohover under cryptography .net
Cryptography is a vast subject. Its true that "devil is in details" but devil is at its best when it comes to cryptography. When you start learning encryption, hashing and signing; for the most of initial phase you just learn one thing "you know very little about it". Don't even consider me when some exceptional programmer make serious mistakes.
I managed to avoid cryptography for most part of my 4 year programming career but it eventually hit me in last project. And it hit me hard. I had to go through quiet a few concepts and use them in a real project. The fact that I was starting from basics, made it worse.
When I started learning about some of the basic concepts, I started playing with those concepts using .NET framework library. I was pleased to see that it has good support of cryptography.
I was wrong! .NET support of cryptography is too good.
Why? For following reasons:
- .NET framework has multiple implementation of almost all algorithms.
- Availability of an algorithm doesn't just depend on framework you are using, it also depends on the operating system.
Sometimes more is less
I use Visual Studio 2008 on Windows Vista machine, so I faced little problem in using any algorithm but when I proudly demoed my application to my friend who happened to be using Windows XP, my application simply crashed. I started exploring the MSDN documentation about the support of algorithms and it was not long before I started to loose track of things. After digging things a bit, I found out that there are three types of implementations:
- Managed: These are the classes that has "Managed" suffix. (e.g. SHA512Managed, AesManaged) As the name suggests, these classes are fully managed implementation of cryptographic algorithms)
- CryptoServiceProvider: These are the classes which end with "CryptoServiceProvider". (e.g. SHA512CryptoServiceProvider, DESCryptoServiceProvider) These classes are just managed wrapper of cryptographic service provider libraries (also known as CryptoAPI).
- CNG: These classes ends with "Cng" which means "Cryptography Next Generation". (e.g. SHA512Cng, ECDiffieHellmanCng) It is the strategic replacement of CryptoAPI.
What makes it more confusing is:
- Not all algorithms has implementation of all three types. For example SHA1 has all three implementations: SHA1Managed, SHA1CryptoServiceProvider, SHA1Cng whereas MD5 has only 2: MD5Cng, MD5CryptoServiceProvider
- For a given type, implementation of some algorithm are available in one version and other are available in different one. For example MD5CryptoServiceProvider is available in all framework versions but SHA512CryptoServiceProvider is available in only Framework 3.5.
Here is the complete table containing details of each algorithm:
Key: 128, 192
|Random Number Generator|
- All Cng implementations are relatively new and if you are planning to run your application on Windows XP (which is quiet likely) then don't use Cng classes.
- Use CryptoServiceProvider implementation for MD5.
- Use Managed implementation for SHAx.
- Aes implementation is not available in .NET framework if you target Window 2000 OS also.
- RSA is the only choice for Asymmetric encryption (public key encryption) if you are willing to use .NET framework 2.0 or Windows XP. You have the option of external libraries though!
Next: Where is this folder?