在Linux系统中,有多种方法可以实现数据加密和解密。以下是几种常用的方案:
GPG (GNU Privacy Guard) 是Linux中最常用的加密工具之一。
gpg -c filename
系统会提示输入密码,加密后会生成 filename.gpg
文件
gpg filename.gpg
输入密码后解密文件
# 生成密钥对
gpg --gen-key
# 导出公钥
gpg --export -a "User Name" > public.key
# 导出私钥
gpg --export-secret-key -a "User Name" > private.key
# 使用公钥加密
gpg --encrypt --recipient "recipient@email.com" filename
# 使用私钥解密
gpg --decrypt filename.gpg > filename
OpenSSL提供了多种加密算法选择。
openssl enc -aes-256-cbc -salt -in file.txt -out file.txt.enc
openssl enc -d -aes-256-cbc -in file.txt.enc -out file.txt
# 生成私钥
openssl genrsa -out private.pem 2048
# 生成公钥
openssl rsa -in private.pem -out public.pem -outform PEM -pubout
# 使用公钥加密
openssl rsautl -encrypt -inkey public.pem -pubin -in file.txt -out file.enc
# 使用私钥解密
openssl rsautl -decrypt -inkey private.pem -in file.enc -out file.txt
ccrypt是专门为加密文件设计的工具。
sudo apt-get install ccrypt # Debian/Ubuntu
sudo yum install ccrypt # CentOS/RHEL
ccrypt file.txt
ccrypt -d file.txt.cpt
eCryptfs提供了目录级别的加密。
sudo apt-get install ecryptfs-utils # Debian/Ubuntu
sudo yum install ecryptfs-utils # CentOS/RHEL
mount -t ecryptfs /path/to/private /path/to/private
按照提示设置加密选项
适用于整个磁盘或分区的加密。
sudo cryptsetup luksFormat /dev/sdX
sudo cryptsetup luksOpen /dev/sdX encrypted_volume
sudo mkfs.ext4 /dev/mapper/encrypted_volume
sudo mount /dev/mapper/encrypted_volume /mnt/encrypted
选择哪种加密方法取决于您的具体需求:文件级加密(GPG/OpenSSL)、目录级加密(eCryptfs)或全盘加密(LUKS)。