1

I have a string which is encrypted which looks like this

Pfx32q+xLq4R+VocXy4IC8aQNdmFqdA284THap54Bl4= 

On encrypting this string we call the HttpUtility.UrlEncode("Pfx32q+xLq4R+VocXy4IC8aQNdmFqdA284THap54Bl4=") passing the encrypted string as a parameter this returns a value

Pfx32q%2BxLq4R%2BVocXy4IC8aQNdmFqdA284THap54Bl4%3D 

where in + is replace with %2 etc and so on on my development system but when the same code is run on a production server the plus does not get encoded instead it remains as + .

We are not able to point out what really is causing this . Is there a scenario or a case where in on calling of the HttpUtility.UrlEncode method the characters like + do not get encoded ?

3
  • + is also a valid character for url that is the reason it is not being encoded here is the base URI definition Commented Sep 9, 2016 at 12:29
  • Are you running the same .NET version on both dev box and server? Is the server maybe running Mono? The Microsoft source code for .NET 4.0 and above definitely escapes + characters. See IsUrlSafeChar here referencesource.microsoft.com/#System/net/System/Net/… The + is explicitly mentioned in the comment as being escaped in spite of the RFC rules. Commented Sep 9, 2016 at 12:33
  • 4
    URI paths and data are encoded in different ways. You should use EscapeDataString for data. You should also ensure you use the same .NET versions on the server and your machine, upgrading whichever fell behind. Commented Sep 9, 2016 at 12:34

1 Answer 1

3

Had the same problem. Converted the encrypted string to Base64 first and then UrlEncoded it.

 string urlOut = HttpUtility.UrlEncode(Base64Encode("Pfx32q+xLq4R+VocXy4IC8aQNdmFqdA284THap54Bl4=")); string urlIn = Base64Decode(HttpUtility.UrlDecode(urlOut)); public static string Base64Encode(string plainText) { var plainTextBytes = Encoding.UTF8.GetBytes(plainText); return Convert.ToBase64String(plainTextBytes); } public static string Base64Decode(string base64EncodedData) { var base64EncodedBytes = Convert.FromBase64String(base64EncodedData); return Encoding.UTF8.GetString(base64EncodedBytes); } 
Sign up to request clarification or add additional context in comments.

1 Comment

It's a workaround, but at least it works. Shouldn't be necessary, but hey.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.