.NET Cryptography

Quick Overview

Create a utility class with shared members so that nobody can create an instance of it.
Use the AlgorithmName property to specify the name of the algorithm (DES, TripleDES, RijnDael, or RC2). It supports generating a new key by reading this key from the file specified directly into the key property of an algorithm instance, and encrypting and decrypting data. You must set the algorithm name appropriately and then generate a key if none exists already to use this class.
Call the EncryptData and DecryptData methods, which will call the ReadKey method for initializing the algorithm. Specify whether the key should be protected through the DPAPI by using the ProtectKey property. Generate encryption keys through the algorithm classes.

To learn more about Cryptography in the .NET Framework, click here

Before we get started encrypting and decrypting, lets see what the setup looks like. I started by creating a blank solution. Then I added a new project with the Empty ASP.NET Web Application template. Next I added another new project with the Class Library template to hold my classes that will be performing the cryptography operations.

We will first need a few helper classes. I put them in MyCrypto.Utility:

Be sure to set your references accordingly

Now that we have the helper classes, lets setup our asymmetric encryption page. Inside of your empty ASP.NET web application, create a new web form.
For this example, you will need 3 textboxes and 4 asp controls (buttons,linkButtons...) to handle our click_Event.
You can see the html here

  • Declare a private string named KeyFileName
  • import: MyCrypto.Utility, System.IO, and System.Web.Security
Load Event
                            
     KeyFileName = Server.MapPath("~/") & "\asymmetric_key.config"
                                
                                
Generate the key
                            
     Try
         PublicKeyText.Text =
         AsymmetricEncryptionUtility.GenerateKey(KeyFileName)
         Response.Write("Key generated successfully! ")
     Catch
         Response.Write("Exception occured when encrypting key!")
     End Try
                                    
                                
Encrypt the data
                            
     If Not File.Exists(KeyFileName) Then
         Response.Write("Missing encryption key. Please generate key!")
     End If
     Try
         Dim data As Byte() = AsymmetricEncryptionUtility.EncryptData(ClearDataText.Text, PublicKeyText.Text)
         EncryptedDataText.Text = Convert.ToBase64String(data)
     Catch
         Response.Write("Unable to encrypt data!")
     End Try
                                
                                
Decrypt the data
                            
     If Not File.Exists(KeyFileName) Then
         Response.Write("Missing encryption key. Please generate key!")
     End If
     Try
         Dim data As Byte() = Convert.FromBase64String(EncryptedDataText.Text)
         ClearDataText.Text = AsymmetricEncryptionUtility.DecryptData(data, KeyFileName)
     Catch
         Response.Write("Unable to decrypt data!")
     End Try
                                
                                

to be continued....