什么是对的网址安全和文件名安全base64的最佳人选填充字符? [英] What's the best candidate padding char for url-safe and filename-safe base64?
问题描述
有关官方的base64的填充字符是'=',这可能需要在URL中使用时,必须%的恩codeD。我试图找到最好的填充字符,使我的恩codeD字符串既可以是URL安全的(我将使用EN codeD字符串作为参数值,比如ID = EN codedString )和文件名安全(我会直接使用EN codeD字符串作为文件名)。
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).
点(。)是一个热门人选,它的网址安全的,但它不是完全安全的文件名:的 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.
!似乎是一个可行的选择,虽然我用Google搜索,并使用它作为填充字符我从来没有见过任何人。有任何想法吗?谢谢!
'!' 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
的唯一原因填充字符是有,因为懒得codeRS的。你可以非常容易添加缺少= - >只是做文字上的4%和减去您从4得到的数量,这是多少=你需要在字符串末尾加。下面是C#code:
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:
- 替换为 -
/替换为_
不要使用。因为它可以产生不同的系统/网络服务器疯狂的结果(例如IIS上的Base64 EN codeD字符串不能结束。或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)
这篇关于什么是对的网址安全和文件名安全base64的最佳人选填充字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!