Team LiB
Previous Section Next Section

Cryptography

Cryptography is used to provide the following:

Technical Choices

  • Use a hash when you want a way of verifying that data has not been tampered with in transit.

  • Use a keyed hash when you want to prove that an entity knows a secret without sending the secret back and forth, or you want to defend against interception during transit by using a simple hash.

  • Use encryption when you want to hide data when being sent across an insecure medium or when making the data persistent.

  • Use a certificate when you want to verify the person claiming to be the owner of the public key.

  • Use symmetric encryption for speed and when both parties share the key in advance.

  • Use asymmetric encryption when you want to safely exchange data across an insecure medium.

  • Use a digital signature when you want authentication and non-repudiation.

  • Use a salt value (a cryptographically generated random number) to defend against dictionary attacks.

Cryptography in .NET

The System.Security.Cryptography namespace provides cryptographic services, including secure encoding and decoding of data, hashing, random number generation, and message authentication.

The .NET Framework provides implementations of many standard cryptographic algorithms and these can be easily extended because of the well defined inheritance hierarchy consisting of abstract classes that define the basic algorithm types — symmetric, asymmetric and hash algorithms, together with algorithm classes.

Table 1: Algorithms for which the .NET Framework provides implementation classes “out of
the box”

Symmetric Algorithms

Asymmetric Algorithms

Hash Algorithms

DES (Data Encryption Standard)

DSA (Digital Signature Algorithm)

HMAC SHA1 (Hash-based Message Authentication Code using the SHA1 hash algorithm)

TripleDES (Triple Data Encryption Standard)

RSA

MAC Triple DES (Message Authentication Code using Triple DES)

Rijndael

 

MD5

RC2

 

SHA1, SHA256, SHA384, SHA512 (Secure Hash Algorithm using various hash sizes)

Symmetric Algorithm Support

.NET provides the following implementation classes that provide symmetric, secret key encryption algorithms:

  • DESCryptoServiceProvider

  • RC2CryptoServiceProvider

  • RijndaelManaged

  • TripleDESCryptoServiceProvider

    Note 

    The classes that end with “CryptoServiceProvider” are wrappers that use the underlying services of the cryptographic service provider (CSP) and the classes that end with “Managed” are implemented in managed code.

Figure 2 shows the inheritance hierarchy adopted by the .NET Framework. The algorithm type base class (for example, SymmetricAlgorithm) is abstract. A set of abstract algorithm classes derive from the abstract type base class. Algorithm implementation classes provide concrete implementations of the selected algorithm; for example DES, Triple-DES, Rijndael and RC2.

Click To expand
Figure 2: The symmetric crypto class inheritance hierarchy

Asymmetric Algorithm Support

.NET provides following asymmetric (public/private key) encryption algorithms through the abstract base class (System.Security.Crytography.AsymmetricAlgorithm):

  • DSACryptoServiceProvider

  • RSACryptoServiceProvider

These are used to digitally sign and encrypt data. Figure 3 shows the inheritance hierarchy.

Click To expand
Figure 3: The asymmetric crypto class inheritance hierarchy

Hashing Algorithm Support

.NET provides following hash algorithms:

  • SHA1, SHA256, SHA384, SHA512

  • MD5

  • HMACSHA (Keyed Hashed algorithm)

  • MACTripleDES (Keyed Hashed algorithm)

Figure 4 shows the inheritance hierarchy for the hash algorithm classes.

Click To expand
Figure 4: The hash crypto class inheritance hierarchy

Team LiB
Previous Section Next Section