locked
Should I validate encrypted form data? RRS feed

  • Question

  • User-1134857695 posted

    Sup! This has been haunting me for a while. I usually pass some Ids encrypted like this:

    public class StringEncrypt : IEncrypt
        {
            public Func<string> GetUserID;
            public StringEncrypt()
            {
                GetUserID = () => HttpContext.Current.User.Identity.GetUserId();
            }
    
            private string Purpose = "The authentication token is";
    
            public string Protect(string unprotectedText)
            {
                var unprotectedBytes = Encoding.UTF8.GetBytes(unprotectedText);
                var protectedBytes = MachineKey.Protect(unprotectedBytes, Purpose + GetUserID);
                var protectedText = Convert.ToBase64String(protectedBytes);
                return protectedText;
            }
    
            public string Unprotect(string protectedText)
            {
                var protectedBytes = Convert.FromBase64String(protectedText);
                var unprotectedBytes = MachineKey.Unprotect(protectedBytes, Purpose + GetUserID);
                var unprotectedText = Encoding.UTF8.GetString(unprotectedBytes);
                return unprotectedText;
            }
        }

    Then I check if there has been any tampering by running the unprotected method. Is this strong enough, or should I validate against it to see if the user is actually allowed to pass that data?

    In other words, if I pass the row id 5, and encrypt it with the method, which would output a huge string. Is it possible for the user to send me a row id of 3? Thanks.

    Wednesday, June 15, 2016 8:27 PM

Answers

  • User1559292362 posted

    Hi superjose,

    Then I check if there has been any tampering by running the unprotected method. Is this strong enough, or should I validate against it to see if the user is actually allowed to pass that data?

    In other words, if I pass the row id 5, and encrypt it with the method, which would output a huge string. Is it possible for the user to send me a row id of 3? Thanks.

    I would suggest that you'd better validate against it, If someone knows your encryption algorithm, it's possible to tamper your data by using some tools (such as proof).

    Best regards,

    Cole Wu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 16, 2016 6:06 AM