文件名的 Base64 编码字符串

Base64 Encoded String for Filename

我想不出 OS(Linux、Windows、Unix)这会导致问题,但也许有人可以告诉我这种方法是否不可取。

我想使用 base64 编码的字符串作为文件名。类似于 gH9JZDP3+UEXeZz3+ng7Lw==。这可能会在任何地方引起问题吗?

编辑:我可能会将其限制为最多 24 个字符

编辑:看起来我有一个会引起问题的角色。生成我的字符串的函数提供如下字符串:J2db3/pULejEdNiB+wZRow== 您会注意到这有一个 / 会导致问题。

根据 this site/ 是一个有效的 base64 字符,所以我不能使用 base64 编码的字符串作为文件名。

在Windows中,只要符合Windows的命名规范就可以了: https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file#naming-conventions.

据我所知,任何 base64 编码的字符串都不包含任何保留字符。

可能会出现问题的是文件名的长度。

官方RFC 4648声明:

An alternative alphabet has been suggested that would use "~" as the 63rd character. Since the "~" character has special meaning in some file system environments, the encoding described in this section is recommended instead. The remaining unreserved URI character is ".", but some file system environments do not permit multiple "." in a filename, thus making the "." character unattractive as well.

我也在我发现的serverfault stackexchange上找到了this:

There is no such thing as a "Unix" filesystem. Nor a "Windows" filesystem come to that. Do you mean NTFS, FAT16, FAT32, ext2, ext3, ext4, etc. Each have their own limitations on valid characters in names.

Also, your question title and question refer to two totally different concepts? Do you want to know about the subset of legal characters, or do you want to know what wildcard characters can be used in both systems?

http://en.wikipedia.org/wiki/Ext3 states "all bytes except NULL and '/'" are allowed in filenames.

http://msdn.microsoft.com/en-us/library/aa365247(VS.85).aspx describes the generic case for valid filenames "regardless of the filesystem". In particular, the following characters are reserved < > : " / \ | ? *

Windows also places restrictions on not using device names for files: CON, PRN, AUX, NUL, COM1, COM2, COM3, etc.

Most commands in Windows and Unix based operating systems accept * as a wildcard. Windows accepts % as a single char wildcards, whereas shells for Unix systems use ? as single char wildcard.

this other one

Base64 only contains A–Z, a–z, 0–9, +, / and =. So the list of characters not to be used is: all possible characters minus the ones mentioned above.

For special purposes . and _ are possible, too.

这意味着您应该使用 _. 而不是标准的 / base64 字符;在 UNIX 和 Windows.

许多编程语言允许您将所有 / 替换为 _.,因为它只是一个字符,可以通过简单的循环完成。

没有。您不能使用 base64 编码的字符串作为文件名。这是因为 / 字符对 base64 字符串有效,这将导致文件系统出现问题。

https://base64.guru/learn/base64-characters

备选方案:

您可以使用 base64 然后替换不需要的字符,但 更好的选择是使用像 bin2hex(). 这样的函数对原始字符串进行十六进制编码