Security is key consideration for many applications. Providing authentication and authorization services to your application is just one part of the overall security. What about the data that is being used and transferred in the application? That is where cryptography comes into picture. Cryptography is a huge topic by itself. In this series of articles I am going to confined our discussion only to .NET Framework and cryptographic classes provided by it.
Why use Cryptography?
Many times application provide security features such as login forms and role based security. However, what if someone intercepts the data that is being flown over the network? What if someone plays with the data that is being transmitted over the network? What if someone opens SQL Server database that is storing passwords? Cryptography provides solution to such questions. Using .NET Cryptographic classes you can encrypt the data that is being flown in your system or network and then decrypt when you want authenticated user to modify or read it. In short Cryptography provides following features:
- Protect data being transferred from reading by third parties
- Protect data being transferred from any modification
- Make sure that data is arriving from the intended location
Types of Cryptographic classes
The overall Cryptographic classes available in .NET framework can be classified in four categories:
- Classes that deal with secret key encryption (also called as Symmetric Cryptography)
- Classes that deal with public key encryption (also called Asymmetric Cryptography )
- Classes that deal with digital signatures (also called cryptographic signatures)
- Classes that deal with cryptographic hashes
All the cryptography related classes can be found in System.Security.Cryptography namespace.
Secret Key Encryption
In Secret Key Cryptography the data being protected is encrypted using a single secret key. This key is known only to sender and receiver. The sender encrypts the data using the secret key. The receiver decrypts the data using the same secret key. It is very important to keep the key secret otherwise anybody having the key can decrypt the data.
.NET Framework provides following classes to work with Secret Key Cryptography:
Public Key Encryption
Unlike secret key encryption, public key encryption uses two keys. One is called public key and the other is called as private key. The public key is not kept secret at all where as private key is kept confidential by the owner of that key. The data encrypted by private key can be decrypted only using its corresponding public key and data encrypted using public key can be decrypted using its private key. Naturally, in order to encrypt the data being transmitted you need to use public key. This data can be decrypted only with the corresponding private key.
.NET Framework provides following classes to work with public key encryption:
Digital signatures are used to verify identity of the sender and ensure data integrity. They are often used along with public key encryption. Digital signature work as follows:
- Sender applies hash algorithm to the data being sent and creates a message digest. Message digest is compact representation of the data being sent.
- Sender then encrypts the message digest with the private key to get a digital signature
- Sender sends the data over a secure channel
- Receiver receives the data and decrypts the digital signature using public key and retrieves the message digest
- Receiver applies the same hash algorithm as the sender to the data and creates a new message digest
- If sender's digest and receiver's digest match then it means that the message really came from the said sender.
The classes DSACryptoServiceProvider and RSACryptoServiceProvider are used to create digital signatures.
Hash algorithms create a fixed length output for a given variable length data. If somebody changes the original data even slightly then the hash generated will be different than original hash. They are often used with digital signatures.
Some of the classes in .NET that deal with hashes are:
Random Number Generators
While working with cryptography classes many times you need to generate cryptographic keys. Random number generators are used for this purpose. .NET provides a class called RNGCryptoServiceProvider to generate such random numbers.
In the next article we will see how the secret key encryption can be done using the relevant classes.