Getting "The payload was invalid" CryptographicException

Viewed 46

In my .NET 5 Server Application I add IDataProtection to my services using the following line in my Startup.cs

services.AddDataProtection()
                  .UseCryptographicAlgorithms(
                new AuthenticatedEncryptorConfiguration()
                {
                    EncryptionAlgorithm = EncryptionAlgorithm.AES_256_GCM,
                    ValidationAlgorithm = ValidationAlgorithm.HMACSHA256
                }
            )
        .SetApplicationName("ApplicationName");

And In Controller the following code

public LeaveRequest(IDataProtectionProvider Provider)
    {
        _protector = Provider.CreateProtector("Key");
    }
public async Task<IActionResult> Edit(string id)
    {
        string TranID = _protector.Unprotect(id);
    }

The above code is working to "protect" and "unprotect" between pages in my application, when send the page url with encrypted query string to email, when click a link the page is opened and redirect to the correct page, but when decrypt by using "unprotect" give me an exception "The payload was invalid".

I want to use the encryption without stored the key ring in a file or database because I have many transactions with encryption query string,

Note: I am working on the HR System to send email to manager if any request created by employee to Approve/Reject.

What am I doing wrong?

0 Answers
Related