OpenSSL 是一个功能强大的开源加密工具包,提供了丰富的命令行工具用于加密、证书管理、SSL/TLS 测试等。以下是最常用的 OpenSSL 命令分类整理:
openssl x509 -in certificate.crt -text -noout # 查看证书内容
openssl x509 -in cert.pem -issuer -subject -dates -noout # 查看颁发者、主题、有效期
openssl x509 -in cert.crt -out cert.pem -outform PEM # CRT转PEM
openssl pkcs12 -in cert.pfx -out cert.pem -nodes # PFX转PEM(含私钥)
openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 365 -nodes
# 交互式输入信息生成证书
openssl genrsa -out private.key 2048 # 生成RSA私钥
openssl ecparam -genkey -name secp256k1 -out ec.key # 生成ECC私钥
openssl rsa -in private.key -pubout -out public.key
openssl rsa -in private.key -check -noout
openssl enc -aes-256-cbc -salt -in file.txt -out file.enc # 加密
openssl enc -d -aes-256-cbc -in file.enc -out file.txt # 解密
openssl rsautl -encrypt -inkey public.key -pubin -in data.txt -out data.enc # 公钥加密
openssl rsautl -decrypt -inkey private.key -in data.enc -out data.txt # 私钥解密
openssl req -new -key private.key -out request.csr # 使用现有私钥
openssl req -new -newkey rsa:2048 -nodes -keyout server.key -out server.csr # 新建私钥+CSR
openssl req -in request.csr -text -noout
openssl s_client -connect example.com:443 -showcerts # 获取服务器证书链
openssl s_client -connect example.com:443 -tls1_2 # 指定TLS版本
openssl s_client -connect example.com:443 2>/dev/null | openssl x509 -noout -dates
openssl dgst -sha256 file.txt # SHA256哈希
openssl sha256 -r file.txt # 简写形式
openssl dgst -sha256 -sign private.key -out signature.bin file.txt # 签名
openssl dgst -sha256 -verify public.key -signature signature.bin file.txt # 验证
openssl pkcs12 -export -out bundle.p12 -inkey private.key -in cert.crt # 生成PKCS12文件
openssl pkcs12 -in bundle.p12 -info -nodes # 查看PKCS12内容(含私钥)
-in
:输入文件-out
:输出文件-nodes
:不加密私钥(无密码)-noout
:不输出编码内容-text
:以文本形式显示内容secp384r1
或更高安全曲线。-rand
或 -writerand
增强随机性(如:-rand /dev/urandom
)。如果需要更具体的场景示例,可以告诉我您的具体需求(如HTTPS证书生成、双向SSL配置等)。