阅读量:112
Debian系统可通过以下工具实现文件加密与解密,以下是具体方法及对应命令:
1. GnuPG(GPG)
适用场景:加密单个文件/目录,支持公钥加密(适合安全传输)。
- 安装:
sudo apt update && sudo apt install gnupg。 - 生成密钥对:
gpg --full-generate-key(按提示输入姓名、邮箱等信息)。 - 加密文件:
gpg --encrypt --recipient "收件人邮箱" file.txt,生成file.txt.gpg。 - 解密文件:
gpg --decrypt file.txt.gpg,输入私钥密码后解密。
2. OpenSSL
适用场景:对称加密(快速加密大文件)或非对称加密。
- 安装:
sudo apt install openssl。 - 对称加密(AES-256-CBC):
- 加密:
openssl enc -aes-256-cbc -salt -in file.txt -out file.enc -pass pass:密码。 - 解密:
openssl enc -d -aes-256-cbc -in file.enc -out file.txt -pass pass:密码。
- 加密:
- 非对称加密(RSA):
- 生成密钥对:
openssl genpkey -algorithm RSA -out private_key.pem(私钥),openssl rsa -pubout -in private_key.pem -out public_key.pem(公钥)。 - 加密:
openssl rsautl -encrypt -pubin -inkey public_key.pem -in file.txt -out file.enc。 - 解密:
openssl rsautl -decrypt -inkey private_key.pem -in file.enc -out file.txt。
- 生成密钥对:
3. eCryptfs/EncFS
适用场景:透明加密目录(无需手动操作加密/解密)。
- eCryptfs:
- 安装:
sudo apt install ecryptfs-utils。 - 加密目录:
sudo mount -t ecryptfs ~/plaintext ~/encrypted(按提示设置加密选项)。 - 卸载:
sudo umount ~/encrypted。
- 安装:
- EncFS:
- 安装:
sudo apt install encfs。 - 加密目录:
encfs ~/encrypted ~/decrypted(按提示设置密码)。 - 卸载:
fusermount -u ~/encrypted。
- 安装:
4. LUKS(磁盘级加密)
适用场景:加密整个磁盘或分区(需手动操作)。
- 安装:
sudo apt install cryptsetup。 - 加密分区:
- 格式化分区:
sudo cryptsetup luksFormat /dev/sdX(sdX替换为实际分区)。 - 打开加密分区:
sudo cryptsetup open /dev/sdX encrypted_partition。 - 格式化并挂载:
sudo mkfs.ext4 /dev/mapper/encrypted_partition && sudo mount /dev/mapper/encrypted_partition /mnt。
- 格式化分区:
- 关闭加密:
sudo umount /mnt && sudo cryptsetup close encrypted_partition。
5. VeraCrypt
适用场景:创建加密容器或虚拟磁盘(跨平台兼容)。
- 安装:
sudo apt install veracrypt。 - 创建加密卷:
veracrypt --create /path/to/volume --encryption AES --hash SHA-512 --size 1G。 - 挂载/卸载:
veracrypt /path/to/volume /mnt/point --password 密码(挂载),veracrypt -d /mnt/point(卸载)。
注意:
- 加密密钥/密码需妥善保管,避免泄露。
- 大文件建议分块加密,或使用压缩工具(如
tar)结合加密命令。 - 系统级加密(如LUKS)需谨慎操作,确保备份重要数据。[1,2,3,4,5,6,7,8,9,10]