I am trying to create a static class and methods to encrypt and decrypt data in asp.net core.
But the problem is that I have to get the "IDataProtectionProvider provider" in constructor with DI and then pass it to the methods so that a CreateProtector be used.
I donot want that and directly want to instanciate the IDataProtectionProvider provider in the method it self.
The controller code:
private readonly IDataProtectionProvider _provider;
public addMDL(IDataProtectionProvider provider)
{
_provider = provider;
}
public IActionResult OnGet()
{
DataProProvider.decData(0, "ABC", _provider)
}
and the static class is :
public static class DataProProvider
{
public static string encData(int intData, string strData, IDataProtectionProvider provider)
{
string str;
IDataProtector dataProtector;
dataProtector = provider.CreateProtector("AA");
if (!string.IsNullOrEmpty(strData))
{
str = dataProtector.Protect(strData);
}
else
{
str = dataProtector.Protect(intData.ToString());
}
return str;
}
public static string decData(int intData, string strData, IDataProtectionProvider provider)
{
string str;
IDataProtector dataProtector;
dataProtector = provider.CreateProtector("A3");
if (!string.IsNullOrEmpty(strData))
{
str = dataProtector.Unprotect(strData);
}
else
{
str = dataProtector.Unprotect(intData.ToString());
}
return str;
}
}
[UPDATE]
As per suggestion I have moved to a smpler approch using Encrypting & Decrypting a String in C# enter link description here