When I try to encrypt & decrypt sample text in my WebAPI project the message encrypt successfully but in decryption it dose not work . I have already set in my startup.cs => services.AddDataProtection(); Here is error message:
System.Security.Cryptography.CryptographicException: The provided payload cannot be decrypted because it was not protected with this protection provider.
[Route("api/[controller]")]
[ApiController]
public class SecurityController : ControllerBase
{
private readonly IDataProtector _protector;
public SecurityController(IDataProtectionProvider protectionProvider)
{
_protector = protectionProvider.CreateProtector("SampleKey");
}
[HttpGet]
public IActionResult Get()
{
string text = "sample text";
string protectedtext = _protector.Protect(text);
string unprotectedtext = _protector.Unprotect(text);
return Ok(new { text = text, Protected = protectedtext, unprotexted = unprotectedtext });
}
}