我想加密文件夹

50 *_*bie 6 20.04

我刚刚切换到 linux(5 小时前)我仍在尝试加密我最重要的文件夹。

似乎没有简单的解决方案。

你有什么建议加密几个文件夹?

Fed*_*eli 5

如果您不想为加密文件创建单独的分区(卷),那么您应该使用eCryptfs

eCryptfs 在写入的每个文件的头中存储加密元数据,以便在主机之间复制加密文件;该文件将可以使用正确的密钥解密,除了加密文件本身中已有的信息外,无需跟踪任何其他信息。

eCryptfs 的安装:

$ sudo apt install ecryptfs-utils
Run Code Online (Sandbox Code Playgroud)

设置很容易。首先,创建将包含加密文件和子目录的“私有”目录。例如:

$ mkdir ~/Documents/private
Run Code Online (Sandbox Code Playgroud)

当此目录未“挂载”时,您可以查看其中文件的内容,但您看不到任何有意义的内容,因为所有内容都将被加密。要使用(读取和写入)它的未加密版本,您应该“挂载”这个目录。您可以像这样将此目录挂载到自身上:

$ sudo mount -t ecryptfs ~/Documents/private ~/Documents/private
Run Code Online (Sandbox Code Playgroud)

由于这是您第一次尝试使用 eCryptfs 挂载此目录,因此您将回答以下几个问题:

  • 首先,输入一个您永远不会忘记的密码。
  • 密码:aes(默认)
  • 关键字节:32
  • 明文透传:n(默认)
  • 文件名加密:n(默认)

现在,重要的警告:

WARNING: Based on the contents of [/root/.ecryptfs/sig-cache.txt],
it looks like you have never mounted with this key 
before. This could mean that you have typed your 
passphrase wrong.

Would you like to proceed with the mount (yes/no)? :
Run Code Online (Sandbox Code Playgroud)

由于这是您第一次挂载此目录,您将回答yes以便~root/.ecryptfs/sig-cache.txt创建一个名为的新文件,该文件将包含密码短语的“签名”。

Would you like to append sig [xxxxxxxxxxxx] to
[/root/.ecryptfs/sig-cache.txt] 
in order to avoid this warning in the future (yes/no)? : 
Run Code Online (Sandbox Code Playgroud)

也回答yes这个问题,以便~root/.ecryptfs/sig-cache.txt填充文件。

后来,当我们重新挂载这个目录时,我们应该不会收到这个警告,除非我们输入了错误的密码

在私有目录中创建一个文件。例如:

$ ls -al > ~/Documents/private/1.txt
Run Code Online (Sandbox Code Playgroud)

注意文件的长度:

$ wc -c ~/Documents/private/1.txt
4935 ...
Run Code Online (Sandbox Code Playgroud)

卸载目录:

$ sudo umount ~/Documents/private
Run Code Online (Sandbox Code Playgroud)

查看卸载~/Documents/private目录下文件的加密版本:

$ less  ~/Documents/private/1.txt
Run Code Online (Sandbox Code Playgroud)

它看起来像一个二进制文件,因为它的内容是加密的。

查看文件的长度:

$ wc -c ~/Documents/private/1.txt
16384 ...
Run Code Online (Sandbox Code Playgroud)

它比未加密的文件大几千字节,因为它包含一些元数据。这是 eCryptfs 在您的文件系统上的全部开销:每个文件将比未加密的文件大 8 到 16KB。

使用以下命令重新挂载目录:

$ sudo mount -t ecryptfs ~/Documents/private ~/Documents/private \
-o ecryptfs_cipher=aes,ecryptfs_key_bytes=32,ecryptfs_passthrough=no,\
ecryptfs_enable_filename_crypto=no,\
ecryptfs_sig=$(sudo cat ~root/.ecryptfs/sig-cache.txt)
Run Code Online (Sandbox Code Playgroud)

输入正确的密码。如果您输入错误的密码,您将收到以下消息:

WARNING: Based on the contents of [/root/.ecryptfs/sig-cache.txt],
it looks like you have never mounted with this key 
before. This could mean that you have typed your 
passphrase wrong.

Would you like to proceed with the mount (yes/no)? : 
Run Code Online (Sandbox Code Playgroud)

在这种情况下,请回答no此问题并使用正确的密码重试。

再次检查内容。

$ less ~/Documents/private/1.txt
$ wc -c ~/Documents/private/1.txt
4935 ...
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请查看man ecryptfs