Asp.Net Core provides encryption and decryption feature for API data protecting.
It's easy to use encryption and decryption provides by Asp.Net Core framework.
Let us see the sample of the code with encryption and decryption in Asp.Net Core API application.
For the demo, I have created an Asp.Net Core API.
On Values Controller, inject IDataProtector interface for encryption and decryption.
Once _protector variable assigned from the constructor then you can use for the encryption and decryption.
From the _protecor variable, you can use encryption using Protect method and decryption using Unprotect method.
The full source code looks like below and I have used dummy string for the demo.
Hope you like Encryption and Decryption in Asp.Net Core.
It's easy to use encryption and decryption provides by Asp.Net Core framework.
Let us see the sample of the code with encryption and decryption in Asp.Net Core API application.
For the demo, I have created an Asp.Net Core API.
On Values Controller, inject IDataProtector interface for encryption and decryption.
private readonly IDataProtector _protector; public ValuesController(IDataProtectionProvider provider) { _protector = provider.CreateProtector(GetType().FullName); }
Once _protector variable assigned from the constructor then you can use for the encryption and decryption.
From the _protecor variable, you can use encryption using Protect method and decryption using Unprotect method.
The full source code looks like below and I have used dummy string for the demo.
using System.Collections.Generic; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.DataProtection; namespace AspNetCoreEncryptionAndDescription.Controllers { [Route("api/[controller]")] public class ValuesController : Controller { private readonly IDataProtector _protector; public ValuesController(IDataProtectionProvider provider) { _protector = provider.CreateProtector(GetType().FullName); } // GET api/values [HttpGet] public IEnumerable<string> Get() { var encrypted = _protector.Protect("This is for testing!!"); var decrypted = _protector.Unprotect(encrypted); return new string[] { encrypted, decrypted }; } } }
Hope you like Encryption and Decryption in Asp.Net Core.