用 `gpg` 加密与解密文件

不用压缩包就能给单个文件加密码的一行命令:GnuPG 的对称加密模式是所有发行版的标配,但语法总被忘掉。

Linux隐私GPG 加密加密低风险
运行 `gpg -c 文件` 用口令加密(生成 .gpg 文件),解密时运行 `gpg -o 文件 -d 文件.gpg`。

01命令

命令
$ gpg -c secret.txt

02作用说明

`gpg -c` 会两次提示输入口令,并在原文件旁生成一个 AES 加密的 .gpg 文件,原文件保持不动——加密后需自行删除。若想用对方公钥而非共享密码加密,可执行 `gpg -e -r 对方邮箱 文件`。还有两个好用的参数:`-a` 输出可直接粘贴进邮件的 ASCII 文本格式;`--batch --yes --passphrase-fd 0` 用于脚本中非交互式调用。

03风险

风险低风险
使用条件Requires gpg (GnuPG, preinstalled on virtually all Linux distros)
!注意: 没有找回途径:一旦忘记口令,.gpg 文件在密码学上无法恢复。

04撤销

撤销
$ gpg -o secret.txt -d secret.txt.gpg

05来源

  • gpg(1) — OpenPGP encryption and signing tool
    Ubuntu Manpages · technical · 2026-09-17
    依据: Documents -c (symmetric encryption with passphrase), -d (decrypt), -e (public-key encrypt) and -o (output file)
  • GnuPG — Debian Wiki
    Debian Wiki · vendor-doc · 2026-09-17
    依据: Walkthrough of symmetric file encryption with gpg -c and decryption with gpg -d

最后验证: 2026-09-17 · 分类: 安全与隐私