0

I'm using Base64 to encrypt/decrypt links. It works great, except the link is getting two equals signs so:

When a customer gets an email, the whole link is not clickable, the two important "==" isn't reading as a link.

How can I make it read the encrypted text as a whole link with the two equals signs?

Also, is it possible if you are missing some letter, refer to another page/error message instead of casting an exception (like

Invalid length for a Base-64 char array or string)?

for example now the link is like:

www.blaa/constollername/methodname?base64link=f5HmbS2tfYRozBfcIV9bCUa1YcGmOFp0AR==

but I want it to be:

www.blaa/constollername/methodname?base64link=f5HmbS2tfYRozBfcIV9bCUa1YcGmOFp0AR

Also if you print out like:

www.blaa/constollername/methodname?base64link=f5HmbS2tfYRozBf

in the url you should be given a error or something like: This page is forbidden.... or something like that, instead of an exception (like I said before).

The code is working and I'm using: Encoding.UTF8.GetBytes and ToBase64String

Because the code is working except the equals sign I'm not posting any code yet if not needed.

LunaLuna
  • 61
  • 8
  • 3
    Are you using html formatting to make the link? If the email client is trying to determine what is a link and misses some of the link, that is a different problem than if you are creating the html values in the email. *NOTE:* Base 64 encoding allows up to two equal signs for padding, which is why you are receiving the length/format error. (It could be none, could be 1, or could be 2.) [good answer](https://stackoverflow.com/a/26632221/2084315) for why padding is needed. – ps2goat Mar 20 '18 at 19:32
  • 1
    Padding isn't always needed. The answer you linked explains when padding _is_ needed. – Roger Lipscombe Mar 20 '18 at 19:45

3 Answers3

0

You could take a substring of the url if the last two characters are "==":

string url = "www.blaa/constollername/methodname?base64link=f5HmbS2tfYRozBfcIV9bCUa1YcGmOFp0AR==";
if (url.Count > 2 && url.Substring(url.Count - 2, 2) == "==")
    url = url.Substring(0, url.Count - 2);

For the second part, you'll just have to do some sort of error checking on your end to see if the url is valid or not and respond to it in a seamless way (for example, check and see what the HTTP response code is for the request using that url and redirect to another page if it's bad)

  • The OP is asking two questions: (1) why the email client isn't picking up the equals signs; (2) given that the client isn't treating the '=' as part of the link, how to parse the incoming link to find the relevant ... whatever ... without getting an error. They're not asking how to discard them. – Roger Lipscombe Mar 20 '18 at 19:42
0

The email client is discarding the '=' padding at the end of your base64-encoded string, so it's never passed to your web app.

Fortunately, you're in luck. If you're only dealing with a single base64-encoded string (and you are), you can just put the padding back on before attempting to decode it. This will avoid the exception.

Roger Lipscombe
  • 89,048
  • 55
  • 235
  • 380
0

I've solved this by myself using Replace("==", "") when creating links to users by email, then when reading from the link, I'm adding two equals-signs at the end by using the variable and += "==";

it works great! :)

LunaLuna
  • 61
  • 8
  • 2
    If the length of what you're base64 encoding varies you might end up with 0, 1 or 2 trailing '=' characters. In C# you can use [string].TrimEnd(new []{'='}) on the string, before including it in the email, and this will remove all trailing instances of the character. – Pseudonymous Mar 21 '18 at 20:38