Using Cryptography Classes to Encrypt and Decrypt Data
Cryptography is a way to encrypt and decrypt data. By encrypting data you are protecting your data from other curious users who would like to know the data that is present. Once you encrypt the data it is in a unreadable form for humans. You need to decrypt the data to read it again.
Thus a person who intercepts the encrypted data will find it difficult to decrypt it. .Net provides a namespace for the classes that are used to encrypt and decrypt data. The namespace Cryptography is used for accessing the classes to encrypt and decrypt data. Classes like AysmmetricAlgorithm, SymmetricAlgorithm, and HashAlgorithm are used for this purpose. These are abstract classes.
The following are the different types of cryptographic primitives that are used in .Net to encrypt and decrypt data.
Private-key encryption or Symmetric Cryptography
The Private-key encryption uses a single shared key to encrypt and decrypt data whereas the Asymmetric cryptography uses public/private key pair for that purpose. Cryptographic signing uses digital signatures to ensure that the data originates from the intended user. The digital signatures are unique to a particular party. Cryptographic hashes are another method of cryptography where data is mapped from any length to fixed-length byte sequence.
The following are the classes that are provided to implement the private-key algorithms. DESCryptoServiceProvider, RC2CryptoServiceProvider, RijndaelManaged, and TripleDESCryptoServiceProvider. For implementing the public-key encryption algorithms DSACryptoServiceProvider and RSACryptoServiceProvider classes are provided. These classes in public-key algorithms can also be used for Cryptographic signing. Classes like HMACSHA1, MACTripleDES, MD5CryptoServiceProvider, SHA1Managed, SHA256Managed, SHA384Managed, and SHA512Managed are used in Cryptographic hashes algorithms / digital signature algorithms.
We will see some code for symmetric cryptography. Symmetric cryptography uses a private key and an initialization vector for processing the encryption of data. You know that encryption is done using key (or password) that is provided by you. The intended party also should know that key to decrypt the data. Initialization vector is used when the mode of encryption used is CipherMode.CBC (Cipher Block Chaining). Using this mode the data is encrypted in blocks. The third block of data is encrypted using the output of second block and the second block is encrypted using the output of first block. If this chaining process happens, what data is used for encrypting the first block? Hence we give an initialization vector which is used to encrypt the first block of data.
- 1) As Byte
The above code can be used to encrypt some value entered in a textbox. The above code uses the RC2 algorithm. You can also use any other algorithm like DES or Rijndael. The data that is entered in a text box is encrypted upon clicking a button in the form. All the above code is written under the click even of the button. The encrypted data is displayed in a message box. You can also display it in another textbox in the form and then use an decryption code to decrypt the data in the other textbox.
The code above is used to decrypt the data that is encrypted. In the above algorithms we have not specified any key or initialization vector. This is because that .Net uses the default key for encryption.
You can use other types of cryptographic encryption and decryption to protect your data. The type of algorithm used for that purpose depends on the scenario of the application that is created.
to our mailing list and receive new articles Note
: We never rent, trade, or sell my email lists to Visit
.NET Programming Tutorial Homepage ______________________________________________________
to our mailing list and receive new articles
: We never rent, trade, or sell my email lists to
Visit .NET Programming Tutorial Homepage