SECURITY

Programming Symmetrical Encryption (part 1)

1/10/2011 2:46:37 PM
The .NET Framework takes the same basic approach to defining symmetric algorithms; abstract classes extend the System.Security.Cryptography.SymmetricAlgorithm class for each of the supported algorithms. Individual implementations of the algorithms extend the abstract class, supporting the possibility of more than one implementation of an algorithm, as represented by Figure 1.
Figure 1. The .NET Framework class hierarchy for symmetric encryption algorithms

1. The SymmetricAlgorithm Class

The SymmetricAlgorithm class allows you to configure an algorithm (select the block size, padding mode, etc.) and create instances of the classes that encrypt and decrypt data; this class, and the derived implementation classes, are not used to process data directly;

Table 1. Members of the SymmetricAlgorithm Class
Member Description
Properties  
BlockSize Gets or sets the block size used by the cipher function.
FeedbackSize Gets or sets the block size used to create feedback when encrypting data.
KeySize Gets or sets the size in bits of the secret key used by the algorithm.
IV Get and set the values of the secret key and initialization vector, expressed as an array of bytes.
Key  
LegalBlockSizes Return the range of block and secret key sizes that the algorithm supports.
LegalKeySizes  
Mode Gets and sets the cipher mode used to prepare data.
Padding Gets or sets the padding mode that will fill out partial blocks of data.
Methods  
Create Creates a new instance of the SymmetricAlgorithm class by name. See the following section for further details.
CreateEncryptor Create instances of the classes used to encrypt and decrypt data. See Section 14.3.4 for details.
CreateDecryptor  
GenerateIV Generate random secret keys and initialization vectors.
GenerateKey  
ValidKeySize Determines if a key of a given length is valid for the algorithm.

2. Instantiating the Algorithm

You can instantiate the implementation classes for symmetric algorithms in the same way. The preferred way to create an instance is by using the Create method of the SymmetricAlgorithm class:

# C#

SymmetricAlgorithm x_alg = SymmetricAlgorithm.Create("RC2");

# Visual Basic .NET

Dim x_alg As SymmetricAlgorithm = SymmetricAlgorithm.Create("RC2")

The Create method instantiates an implementation class based on the value of the argument; Table 14-3 shows the list of supported argument strings and the implementation classes that they create. If no argument is supplied, the Create method will instantiate the RijndaelManaged class and return null (C#) or NothingTable 14-3. The system administrator configures the mapping between the string values and the instantiated classes. (Visual Basic .NET) if the argument is not one of those listed in

Algorithms can be instantiated directly, an approach that is useful if you wish to ensure that a specific implementation class is used or if you are using algorithms for which no mappings are available:

# C#

SymmetricAlgorithm x_alg = new RC2CryptoServiceProvider( );

# Visual Basic .NET

Dim x_alg As SymmetricAlgorithm = New RC2CryptoServiceProvider( )

Table 2. Mapping string values to algorithm classes
String value Algorithm-implementation class
DES DESCryptoServiceProvider
System.Security.Cryptography.DES DESCryptoServiceProvider
3DES TripleDESCryptoServiceProvider
TripleDES TripleDESCryptoServiceProvider
Triple DES TripleDESCryptoServiceProvider
System.Security.Cryptography.TripleDES TripleDESCryptoServiceProvider
RC2 RC2CryptoServiceProvider
System.Security.Cryptography.RC2 RC2CryptoServiceProvider
Rijndael RijndaelManaged
System.Security.Cryptography.Rijndael RijndaelManaged
Other  
  •  Programming .NET Security : Symmetric Encryption Explained (part 3)
  •  Programming .NET Security : Symmetric Encryption Explained (part 2) - Cipher Modes
  •  Programming .NET Security : Symmetric Encryption Explained (part 1) - Creating the Encrypted Data
  •  Hashing Algorithms: Extending the .NET Framework (part 1)
  •  Hashing Algorithms: Extending the .NET Framework (part 1)
  •  Programming Keyed Hashing Algorithms
  •  Programming .NET Security : Keyed Hashing Algorithms Explained
  •  Programming Hashing Algorithms (part 5) - Validating Hash Codes
  •  Programming Hashing Algorithms (part 4) - Hashing Streamed Data
  •  Programming Hashing Algorithms (part 3) - Hashing Data from Memory
  •  Programming Hashing Algorithms (part 2) - Instantiating the Algorithm
  •  Programming Hashing Algorithms (part 1) - The HashAlgorithm Class
  •  Programming .NET Security : Hashing Algorithms Explained
  •  Programming .NET Security : Cryptography Explained (part 2)
  •  Programming .NET Security : Cryptography Explained (part 1) - Confidentiality
  •  .NET security : Administering Isolated Storage
  •  .NET security : Programming Isolated Storage
  •  .NET security : Isolated Storage Explained
  •  Programming Role-Based Security
  •  Role-Based Security Explained
  •  
    Most View
    Plot Thickens As Us Sues Apple To Protect Amazon's Monopod Y In Ebooks
    WCF Services : Versioning
    On-Scene Portrait Photography Techniques
    Performing mySAP.com Component Installations : Installation Details for mySAP Components
    Ultrabook Supertest (Part 5) - HP Envy 4
    Giants Of The Phone World (Part 2) : Micromax A110 Canvas 2, Lenovo K860, Micromax Canvas HD
    Defensive Database Programming with SQL Server: The Ticket-Tracking System (part 1) - Enforcing business rules using constraints only
    Programming Microsoft SQL Server 2005 : FOR XML Commands (part 1) - FOR XML RAW & FOR XML AUTO
    SharePoint 2010 : Creating and Managing Workflows - Workflows in SharePoint 2010
    Paul Allen: In technology, management is not the promotion
    Top 10
    Nvidia GeForce GTX Titan 6 GB Graphics Card Review (Part 6)
    Nvidia GeForce GTX Titan 6 GB Graphics Card Review (Part 5)
    Nvidia GeForce GTX Titan 6 GB Graphics Card Review (Part 4)
    Nvidia GeForce GTX Titan 6 GB Graphics Card Review (Part 3)
    Nvidia GeForce GTX Titan 6 GB Graphics Card Review (Part 2)
    Nvidia GeForce GTX Titan 6 GB Graphics Card Review (Part 1)
    Nook HD - A High-Definition Tablet With The Heart Of A Reader (Part 4)
    Nook HD - A High-Definition Tablet With The Heart Of A Reader (Part 3)
    Nook HD - A High-Definition Tablet With The Heart Of A Reader (Part 2)
    Nook HD - A High-Definition Tablet With The Heart Of A Reader (Part 1)