简体   繁体   English

Base64编码对文件名安全吗?

[英]Base64 Encoding safe for filenames?

Is Base64 encoding safe to use for filenames on Windows and Linux systems? Base64编码是否可以安全地用于Windows和Linux系统上的文件名? From my research I have found that replacing all / characters of the result with - or _ should resolve any issues. 根据我的研究,我发现用-_替换结果的所有/字符应解决任何问题。

Can anyone provide more details on this? 任何人都可以提供更多细节吗?

Currently in Java I am using the following peice of code: 目前在Java中我使用以下代码:

MessageDigest md5Digest = MessageDigest.getInstance("MD5");
md5Digest.reset();
md5Digest.update(plainText.getBytes());

byte[] digest = md5Digest.digest();

BASE64Encoder encoder = new BASE64Encoder();
hash = encoder.encode(digest);
hash.replace('/','_');

Modified Base64 (when / , = and + are replaced) is safe to create names but does not guarantee reverse transformation due to case insensitivity of many file systems and urls. 修改后的Base64(当替换/=+ )可以安全地创建名称,但由于许多文件系统和URL不区分大小写,因此无法保证反向转换。

Base64 is case sensitive, so it will not guarantee 1-to-1 mapping in cases of case insensitive file systems (all Windows files systems, ignoring POSIX subsystem cases). Base64区分大小写,因此在不区分大小写的文件系统(所有Windows文件系统,忽略POSIX子系统情况)的情况下,它不保证一对一映射。 Most urls also case insensitive preventing 1-to-1 mapping. 大多数网址也不区分大小写,防止1对1映射。

I would use Base32 in this case - you'll get names a bit longer, but Base32 encoded values are 100% safe for file/uri usage without replacing any characters and guarantees 1-to-1 mapping even in cases of insensitive environment (FAT/Win32 NTFS access). 在这种情况下我会使用Base32 - 你会得到更长的名字,但Base32编码值对于文件/ uri的使用是100%安全的,无需替换任何字符,即使在环境不敏感的情况下也能保证1对1映射(FAT / Win32 NTFS访问)。

Unfortunately there is usually no built-in support for this encoding in frameworks. 不幸的是,在框架中通常没有对此编码的内置支持。 On other hand code is relatively simple to write yourself or find online. 另一方面,编写代码或在线查找相对简单。

http://en.wikipedia.org/wiki/Base32 . http://en.wikipedia.org/wiki/Base32

RFC 3548 suggests not only to replace the / character. RFC 3548建议不仅要替换/字符。 The URL and Filename safe Alphabet replaces: URL和文件名安全字母替换:

  • the 63:nd / character with the underscore _ 带下划线的63:nd /字符_
  • the 62:nd + character with the minus - . 带有减号的62:nd +字符-

But maybe you better use a HEX-String. 但也许你最好使用HEX-String。 It is been a while, when i stored a hash value in a filename. 有一段时间,我将哈希值存储在文件名中。 I started with using Base64 String but switched to a Hex-String. 我开始使用Base64 String,但切换到Hex-String。 I don't remember why i switched, maybe because Windows makes no difference between 'a' and 'A' as AndiDog said. 我不记得为什么我换了,也许是因为Windows在'a'和'A'之间没有区别,正如AndiDog所说。

I'm not sure what you are using the encoding for, but consider percent encoding file names. 我不确定您使用的是什么编码,但请考虑编码文件名的百分比

  • It works in every file system 它适用于每个文件系统
  • It keeps file names human readable as long as they're within the ASCII range 只要文件名在ASCII范围内,它就可以保持文件名的可读性

Usually MD5 hashes (hashes in general) are represented as hexadecimal strings instead of Base64, which then only contain [a-f0-9]. 通常MD5哈希值(一般为哈希值)表示为十六进制字符串而不是Base64,后者只包含[a-f0-9]。 Those names would be supported by all filesystems. 所有文件系统都支持这些名称。

If you really want to use Base64, your solution (replacing slashes) will not work correctly as Windows filesystems don't make a difference between 'A' and 'a'. 如果你真的想使用Base64,你的解决方案(替换斜杠)将无法正常工作,因为Windows文件系统在'A'和'a'之间没有区别。 Maybe you want to use Base32 instead? 也许你想用Base32代替? But mind that Base32 makes 8 bits out of 4, so it will be easier to just take the hexadecimal representation. 但请注意,Base32在4中产生8位,因此更容易采用十六进制表示。

In general, the following characters are not allowed in Windows and/or Linux: \\ / : * ? 通常,Windows和/或Linux中不允许使用以下字符:\\ /:*? " < > | “<> |

One-liner for C#: C#的单线程:

String filename = Convert.ToBase64String(new SHA256Managed().ComputeHash(Encoding.UTF8.GetBytes("UTF-8 string with snowmen"))).Replace("+", "_").Replace("/", "-").Replace("=","");

Needs the following to the beginning of the file: 需要以下内容到文件的开头:

using System.Security.Cryptography
using System.Text

A filename created by Base64 is only safe if you use a different character from /, which you do, as NTFS does not allow that character to be used in file names. 如果您使用与/不同的字符,则Base64创建的文件名是唯一安全的,因为NTFS不允许在文件名中使用该字符。 As long as you do that, pretty much all commonly used file systems in common use will be OK. 只要你这样做,几乎所有常用的常用文件系统都可以。

However, if the filesystem is case-insensitive , as is the case on Windows, you can get collisions because the Base64 alphabet contains both upper and lower-case. 但是,如果文件系统不区分大小写 (如Windows上的情况),则可能会发生冲突,因为Base64字母包含大写和小写。

You might want to consider using the hexadecimal representation of your MD5 hash instead, since this is a fairly standard way of representing those as a string. 您可能需要考虑使用MD5哈希的十六进制表示,因为这是将它们表示为字符串的相当标准的方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM