什么是最好的候选填充字符的url安全和文件名安全base64? [英] What's the best candidate padding char for url-safe and filename-safe base64?

查看:177
本文介绍了什么是最好的候选填充字符的url安全和文件名安全base64?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

官方base64的padding char为'=',在URL中使用时可能需要进行百分比编码。我试图找到最好的填充字符,所以我的编码字符串可以是url的安全(我将使用编码的字符串作为参数值,如id = encodedString)和文件名安全(我将使用编码字符串直接作为文件名)。

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('。')是一个受欢迎的候选人,它的网址安全,但并不完全是文件名安全: Windows不会允许以尾随点结尾的文件名

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.

'!'似乎是一个可行的选择,虽然我googled,我从来没有看到任何人使用它作为填充字符。有任何想法吗?谢谢!

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

更新:我用 - (减号)替换+,将/替换为_(下划线)在我自定义的base64编码中,所以' - '或'_'不再适用于填充字符。

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.

推荐答案

最好的解决方案(我上个月在电子邮件发送网站上处理这个问题)是不要使用填充字符(=)在所有

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

唯一的原因是因为懒的解码器是填补字符。你可以非常容易地添加缺少= - >只是在文本上做%4,并减去你从4获得的数字,这是多少=你需要添加到字符串结尾。这是C#代码:

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, '=');

此外,大多数人都有兴趣用其他URL安全字符替换+和/我建议:

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


  • 替换为 -
    /替换为_

不要使用。因为它可以在不同的系统/ Web服务器上产生疯狂的结果(例如在IIS Base64编码的字符串不能结束,或IIS将搜索文件)

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)

这篇关于什么是最好的候选填充字符的url安全和文件名安全base64?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆