要在Linux系统中加密或解密文件,可以采用多种方法,下面介绍一些常见工具及其用法:
作为基于OpenPGP标准的工具,GnuPG可以对数据进行加密和解密。
sudo apt-get install gpg# Debian/Ubuntusudo yum install gpg# CentOS/RHELsudo dnf install gpg# Fedoragpg --output encrypted_file.gpg --encrypt --recipient [email protected] original_file也可以采用对称加密:
gpg --output encrypted_file.gpg --symmetric original_file加密文件时,系统将提示你输入密码。
gpg --output decrypted_file --decrypt encrypted_file.gpg采用对称加密时,系统会要求输入先前设置的密码。
OpenSSL不仅是功能强大的加密库,也能用于文件的加密与解密。
openssl enc -aes-256-cbc -salt -in original_file -out encrypted_file.enc -k your_password此处采用AES-256-CBC算法,也可按照实际需要选择其他算法。
openssl enc -d -aes-256-cbc -in encrypted_file.enc -out decrypted_file -k your_passwordLUKS属于磁盘加密标准,可用于加密整个磁盘或指定分区。
sudo apt-get install cryptsetup# Debian/Ubuntusudo yum install cryptsetup# CentOS/RHELsudo dnf install cryptsetup# Fedorasudo cryptsetup luksFormat /dev/sdXsudo cryptsetup open /dev/sdX my_encrypted_partitionsudo mkfs.ext4 /dev/mapper/my_encrypted_partitionsudo mount /dev/mapper/my_encrypted_partition /mntsudo umount /mntsudo cryptsetup close my_encrypted_partitionsudo cryptsetup luksClose my_encrypted_partition作为开源磁盘加密软件,VeraCrypt具备与LUKS相近的能力,同时提供更多功能及更完善的跨平台支持。
sudo apt-get install veracrypt# Debian/Ubuntusudo yum install veracrypt# CentOS/RHELsudo dnf install veracrypt# Fedoraveracrypt --volume-type=standard --encryption=aes --hash=sha-512 --key-size=512 /path/to/encrypted_volume /path/to/passphraseveracrypt /path/to/encrypted_volume /path/to/mount_point --password-file=/path/to/passphrase_file具体需求决定方法选择,包括是否加密整个磁盘、是否要求跨平台支持,以及使用对称还是非对称加密。文件级加密和解密可选GnuPG与OpenSSL,磁盘级加密则更适合采用LUKS和VeraCrypt。