Ubuntu文件加密与解密常用方法
GnuPG是基于OpenPGP标准的开源工具,支持对称加密(同一密码加密/解密)和非对称加密(公钥/私钥对)。
sudo apt-get install gnupg(Ubuntu默认仓库已包含)。gpg --output encrypted_file.gpg --symmetric --cipher-algo AES256 original_file(如original_file.txt),输入密码后生成encrypted_file.gpg。gpg --output decrypted_file.txt --decrypt encrypted_file.gpg,输入密码即可还原为decrypted_file.txt。OpenSSL是强大的加密库,支持多种算法(如AES-256-CBC),适合快速加密文件。
openssl enc -aes-256-cbc -salt -in original_file -out encrypted_file.enc -pass pass:your_password(如document.pdf),-salt参数增强安全性,生成encrypted_file.enc。openssl enc -d -aes-256-cbc -in encrypted_file.enc -out decrypted_file -pass pass:your_password,输入密码后还原为decrypted_file。7-Zip是跨平台压缩工具,支持AES-256加密,适合需要压缩+加密的场景。
sudo apt-get install p7zip-full。7z a -pYourPassword -mhe=on encrypted_file.7z original_file(-p指定密码,-mhe=on隐藏文件名),生成encrypted_file.7z。7z x -pYourPassword encrypted_file.7z -ooutput_directory(-o指定解压目录),输入密码后解压到目标文件夹。VeraCrypt是开源磁盘加密工具,支持加密单个文件(容器)或整个分区,适合高安全性需求(如敏感数据)。
sudo apt-get install veracrypt(Ubuntu 22.04及以上版本可直接安装)。veracrypt --create ~/encrypted_container.vc,选择加密算法(如AES)、哈希算法(如SHA-512)、容器大小(如1GB),设置密码后完成创建。veracrypt ~/encrypted_container.vc /mnt/veracrypt,输入密码后容器会挂载到/mnt/veracrypt,可像普通文件夹一样访问。veracrypt -d /mnt/veracrypt,断开挂载后容器内容无法直接访问。Ubuntu默认文件管理器(GNOME Files)提供简单加密功能,适合日常快速使用。
.zip或.7z格式的加密压缩包(如archive.zip)。