In this article I will explain with an example, how to use encryption and decryption (Cryptography) in ASP.Net Core MVC.
This article makes use of System.Security.Cryptography class and AES algorithm for encryption and decryption in ASP.Net Core MVC.
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.
Inside this Action method, the value of the TextBox is encrypted using
Encrypt method and set into a
TempData object.
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.
public class HomeController : Controller
{
// GET: Home
public IActionResult Index()
{
return View();
}
[HttpPost]
public IActionResult Encryption(string plainText)
{
TempData["EncryptedValue"] = this.Encrypt(plainText);
return RedirectToAction("Index");
}
[HttpPost]
public IActionResult 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
Inside the View, first the ASP.Net TagHelpers is inherited.
The View consists of an HTML Form which has been created using the ASP.Net TagHelpers with the following attributes.
asp-action – Name of the Action.
asp-controller – Name of the Controller. In this case the name is Home.
method – 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 an HTML INPUT TextBox 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 an HTML INPUT HiddenField 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.
@addTagHelper*, Microsoft.AspNetCore.Mvc.TagHelpers
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width"/>
<title>Index</title>
</head>
<body>
<form method="post" asp-controller="Home" asp-action="Encryption">
<span>Plain Text:</span>
<input type="text" name="plainText"/>
<input type="submit" value="Encrypt"/>
<br/>
<span>Encrypted Text: @TempData["EncryptedValue"]</span>
</form>
<hr />
<form method="post" asp-controller="Home" asp-action="Decryption">
<input type="hidden" name="encryptedValue" value="@TempData["EncryptedValue"]" />
<input type="submit" value="Decrypt" />
<br />
<br />
<span>Decrypted Value: @TempData["DecryptedValue"]</span>
</form>
</body>
</html>
Screenshot
Demo
Downloads