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

Community
  • 1
  • 1
John
  • 2,653
  • 4
  • 36
  • 57
  • Please post your code, what the input to `Encode` is and what you expect as result. – Oded Oct 27 '11 at 15:29

4 Answers4

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);
Dan
  • 304
  • 1
  • 4
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 ?

GregM
  • 2,634
  • 3
  • 22
  • 37
0

I tried this and it brought the id across correctly:

<a href='Page.aspx?ID=
    <%=server.urlencode(server.htmlencode("+jdghogjhior==")) %>'>Click here</a>
Steve
  • 531
  • 5
  • 16
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.

Community
  • 1
  • 1
Craig
  • 4,323
  • 2
  • 29
  • 32