In this article I will explain with an example, how to use encryption and decryption (Cryptography) in ASP.Net MVC.
This article makes use of System.Security.Cryptography class and AES algorithm for encryption and decryption in ASP.Net MVC.
Note: For beginners in ASP.Net MVC, please refer my article ASP.Net MVC Hello World Tutorial with Sample Program example.
 
 

Controllers

The Controller consists of following Action methods.

Action method for handling GET operation

Inside this Action method, simply the View is returned.
 

Action method for handling POST operation – Form 1

This Action method gets called when Encrypt button is clicked or when the Form is submitted.
Note: For more details on calling multiple Action Methods using single Form, please refer my article Calling multiple Action methods using Single Form in ASP.Net MVC.
 
Inside this Action method, the value of the TextBox is encrypted using Encrypt method and set into a TempData object.
Note: For details on Encryption method, please refer my article AES Encryption Decryption (Cryptography) Tutorial with example in ASP.Net using C# and VB.Net.
 
Action method for handling POST operation – Form 2
This Action method gets called when Decrypt button is clicked or when the Form is submitted.
Inside this Action method, the encrypted text is decrypted using Decrypt method and set into a TempData object.
Note: For details on Decryption method, please refer my article AES Encryption Decryption (Cryptography) Tutorial with example in ASP.Net using C# and VB.Net.
 
public class HomeController : Controller
{
    // GET: Home
    public ActionResult Index()
    {
        return View();
    }
 
 
    [HttpPost]
    public ActionResult Encryption(string plainText)
    {
        TempData["EncryptedValue"] = this.Encrypt(plainText);
        return RedirectToAction("Index");
    }
 
    [HttpPost]
    public ActionResult Decryption(string encryptedValue)
    {
        TempData["DecryptedValue"] = this.Decrypt(encryptedValue);
        return RedirectToAction("Index");
    }
 
    private string Encrypt(string plainText)
    {
        //Secret Key.
        string secretKey = "$ASPcAwSNIgcPPEoTSa0ODw#";
 
        //Secret Bytes.
        byte[] secretBytes = Encoding.UTF8.GetBytes(secretKey);
 
        //Plain Text Bytes.
        byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
 
        //Encrypt with AES Alogorithm using Secret Key.
        using (Aes aes = Aes.Create())
        {
            aes.Key = secretBytes;
            aes.Mode = CipherMode.ECB;
            aes.Padding = PaddingMode.PKCS7;
 
            byte[] encryptedBytes = null;
            using (ICryptoTransform encryptor = aes.CreateEncryptor())
            {
                encryptedBytes = encryptor.TransformFinalBlock(plainTextBytes, 0, plainTextBytes.Length);
            }
 
            return Convert.ToBase64String(encryptedBytes);
        }
    }
 
    private string Decrypt(string encryptedText)
    {
        //Secret Key.
        string secretKey = "$ASPcAwSNIgcPPEoTSa0ODw#";
 
        //Secret Bytes.
        byte[] secretBytes = Encoding.UTF8.GetBytes(secretKey);
 
        //Encrypted Bytes.
        byte[] encryptedBytes = Convert.FromBase64String(encryptedText);
 
        //Decrypt with AES Alogorithm using Secret Key.
        using (Aes aes = Aes.Create())
        {
            aes.Key = secretBytes;
            aes.Mode = CipherMode.ECB;
            aes.Padding = PaddingMode.PKCS7;
 
            byte[] decryptedBytes = null;
            using (ICryptoTransform decryptor = aes.CreateDecryptor())
            {
                decryptedBytes = decryptor.TransformFinalBlock(encryptedBytes, 0, encryptedBytes.Length);
            }
 
            return Encoding.UTF8.GetString(decryptedBytes);
        }
    }
}
 
 

View

HTML Markup

The View consists of two HTML Form which has been created using the Html.BeginForm method with the following parameters.
ActionName – Name of the Action.
ControllerName – Name of the Controller. In this case the name is Home.
FormMethod – It specifies the Form Method i.e. GET or POST. In this case it will be set to POST.

Encryption Form

The Form consists of a TextBox created using Html.TextBox method and a Submit button.
It also consists of an HTML SPAN element which is used to display the encrypted value.
 

Decryption Form

This Form consists of a HiddenField created using Html.Hidden method which is used to store the encrypted value.
It also consists of a Submit button which when clicked the decrypted value is displayed suing HTML SPAN element.
@{
    Layout = null;
}
 
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    @using (Html.BeginForm("Encryption", "Home", FormMethod.Post))
    {
        <span>Plain Text:</span>
        @Html.TextBox("PlainText")
        <input type="submit" value="Encrypt" />
        <br />
        <span>Encrypted Text: @TempData["EncryptedValue"]</span>
    }
    <hr />
    @using (Html.BeginForm("Decryption", "Home", FormMethod.Post))
    {
        @Html.Hidden("EncryptedValue", TempData["EncryptedValue"])
        <input type="submit" value="Decrypt" />
        <br />
        <br />
        <span>Decrypted Value: @TempData["DecryptedValue"]</span>
    }
</body>
</html>
 
 

Screenshot

ASP.Net MVC: AES Encryption Decryption (Cryptography) Tutorial with example
 
 

Demo

 
 

Downloads