1

The padding char for the official base64 is '=', which might need to be percent-encoded when used in a URL. I'm trying to find the best padding char so that my encoded string can be both url safe (I'll be using the encoded string as parameter value, such as id=encodedString) AND filename safe (I'll be using the encoded string directly as filename).

Dot ('.') is a popular candidate, it's url safe but it's not exactly filename safe: Windows won't allow a file name which ends with a trailing dot.

'!' seems to be a viable choice, although I googled and I've never seen anybody using it as the padding char. Any ideas? Thanks!

Update: I replaced "+" with "-" (minus) and replaced "/" with "_" (underscore) in my customized base64 encoding already, so '-' or '_' is not available for the padding char any more.

bobince
  • 528,062
  • 107
  • 651
  • 834
SamS
  • 1,561
  • 2
  • 15
  • 22

4 Answers4

4

The best solution (I've spent last month working on this problem with an email sending website) is to not use padding character (=) at all

The only reason why padding character is there is because of "lazy" decoders. You can extremely easy add missing = -> just do %4 on text and subtract the number you get from 4 and that is how many = you need to add in string end. Here is C# code:

    var pad = 4 - (text.Length % 4);
    if (pad < 4)
        text = text.PadRight(text.Length + pad, '=');

Also, most people who do this are interested in replacing + and / with other URL safe character... I propose:

  • replace with - / replace with _

DO NOT USE . as it can produce crazy results on different systems / web servers (for example on IIS Base64 encoded string can't end with . or IIS will search for the file)

Young Bob
  • 733
  • 3
  • 9
nikib3ro
  • 20,366
  • 24
  • 120
  • 181
  • 1
    Not quite correct, the result of the padding is not divisible by 4. Need to append (4-pad) number of '=' characters not pad number. Otherwise neat solution. – Young Bob Feb 11 '14 at 13:51
  • @YoungBob I've edited answer, please edit it if you can propose better code. Thanks! – nikib3ro Feb 11 '14 at 18:16
  • In Python you can do simply `text + (-len(text) % 4) * "="` but this way of calculating it cannot be used in other languages (the % operator in Python gives a positive value for a positive divisor). – Tronic Nov 10 '21 at 14:15
2

The RFC 2396 unreserved characters in URIs are:

"-" | "_" | "." | "!" | "~" | "*" | "'" | "(" | ")"

It's worth pointing out, though, that the Microsoft article also says "Do not assume case sensitivity." Perhaps you should just stick with base 16 or 32?

Miles
  • 31,360
  • 7
  • 64
  • 74
1

The Wikipedia article states;

a modified Base64 for URL variant exists, where no padding '=' will be used

bob
  • 11
  • 1
0

I would go with '-' or '_'
They're URL and file safe, and they looks more or less like padding

cobbal
  • 69,903
  • 20
  • 143
  • 156
  • Sorry I should have mentioned this: I replaced "+" with "-" (minus) and replaced "/" with "_" (underscore) in my customized base64 encoding already, so '-' or '_' is not available for the padding char any more. – SamS Mar 18 '09 at 19:31