在 Linux 下使用 OpenSSL 加密/解密文件,常见有两种方式:对称加密(如 AES) 和 非对称加密(RSA)。日常最常用的是 对称加密。
下面按使用场景说明。
openssl enc -aes-256-cbc -salt -in 原文件 -out 加密文件.enc
示例:
openssl enc -aes-256-cbc -salt -in test.txt -out test.txt.enc
执行后会提示输入密码。
✅ 参数说明:
enc:加密命令-aes-256-cbc:加密算法-salt:增加安全性(推荐)-in:输入文件-out:输出文件openssl enc -d -aes-256-cbc -in 加密文件.enc -out 解密文件
示例:
openssl enc -d -aes-256-cbc -in test.txt.enc -out test.txt
openssl enc -aes-256-cbc -salt \
-in file.txt \
-out file.enc \
-pass pass:你的密码
⚠️ 注意:命令行密码可能被 history 或 ps 看到,不推荐生产环境使用
openssl enc -aes-256-cbc -salt -a -in file.txt -out file.enc
-a:输出 Base64 格式解密:
openssl enc -d -aes-256-cbc -a -in file.enc -out file.txt
openssl genrsa -out private.pem 2048
openssl rsa -in private.pem -pubout -out public.pem
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
⚠️ RSA 不能直接加密大文件,通常只用来加密 AES 密钥
bad magic number如果你告诉我:
我可以给你更合适的示例。