0

I'm encrypting values and adding them to the URL. My problem though is that after I run it through HttpUtility.URLEncode I sometimes come up with bad URLs. I get stuff like

www.mysite.com/page.aspx?id=+jdghogjhior==

I think the it's the leading plus that's giving me trouble. Any ideas? What do you guys do?

Similar problems to what this guy talks about: Server.UrlEncode vs. HttpUtility.UrlEncode

1
  • Please post your code, what the input to Encode is and what you expect as result. Commented Oct 27, 2011 at 15:29

4 Answers 4

1

You can try to take an encrypted string, convert it to base64 string and send it via url. When you receive it, decode from base64 and decrypt it.

Encoding:

byte[] bt = Encoding.Unicode.GetBytes("5dnjnbf&&#jnb3=+");
string str = Convert.ToBase64String(bt);

Decode:

byte[] bt = Convert.FromBase64String(str);
string s = Encoding.Unicode.GetString(bt);
Sign up to request clarification or add additional context in comments.

Comments

0

Is the thing you're encoding by chance already base64 encoded?

If so you'll need to use a "modified" base64 which is compatible with URLs:

... a modified Base64 for URL variant exists, where no padding '=' will be used, and the '+' and '/' characters of standard Base64 are respectively replaced by '-' and '_'

See Code for decoding/encoding a modified base64 URL for example.

1 Comment

yup, that was it. I was double encoding / decoding
0

I think the + is the only problem, so habitualy I replace it with something else that I know won't be in the query like space (%20)

But in somme case you need spaces

so have you try : Uri.EscapeDataString

Like the link you have posted ?

Comments

0

I tried this and it brought the id across correctly:

<a href='Page.aspx?ID=
    <%=server.urlencode(server.htmlencode("+jdghogjhior==")) %>'>Click here</a>

1 Comment

So, really, htmlEncoding it, then urlEncoding it did the trick.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.