security Cheatsheet

OpenSSL 证书与加密速查表

OpenSSL 常用命令一页速查:证书查看与生成、CSR 和自签名证书、私钥处理、TLS 连接测试和常见加密操作等 30+ 高频操作,含参数说明与典型排障场景示例。

把 OpenSSL 日常最常敲的命令按证书查看、生成、私钥处理和 TLS 排查分组整理,HTTPS 排障直接复制改参数即可。

openssltlsssl证书加密

证书查看 Inspect Certificate

  • openssl x509 -in cert.pem -text -noout
    查看证书完整信息(含颁发者、有效期、SAN)
  • openssl x509 -in cert.pem -dates -noout
    只看有效期(notBefore / notAfter)
  • openssl x509 -in cert.pem -issuer -noout
    只看颁发者
  • openssl x509 -in cert.pem -subject -noout
    只看主体(域名)
  • openssl x509 -in cert.pem -ext subjectAltName -noout
    查看 SAN(多域名),证书覆盖的所有域名
  • openssl x509 -in cert.pem -fingerprint -sha256 -noout
    查看 SHA-256 指纹

远程证书检查 Remote Check

  • echo | openssl s_client -connect example.com:443 -servername example.com 2>/dev/null | openssl x509 -noout -dates
    查看远程服务器证书有效期
  • echo | openssl s_client -connect example.com:443 -showcerts 2>/dev/null
    查看完整证书链
  • echo | openssl s_client -connect example.com:443 -servername example.com 2>/dev/null | openssl x509 -text -noout
    查看远程证书完整信息
  • openssl s_client -connect example.com:443 -servername example.com -tls1_2
    指定 TLS 版本测试连接
  • openssl s_client -connect example.com:443 -servername example.com -tls1_3
    测试 TLS 1.3 连接

证书生成 Generate Certificate

  • openssl genrsa -out private.key 2048
    生成 2048 位 RSA 私钥
  • openssl ecparam -genkey -name prime256v1 -out private.key
    生成 EC 私钥(更小更快)
  • openssl req -new -key private.key -out request.csr
    用私钥生成 CSR(证书签名请求)
  • openssl req -new -newkey rsa:2048 -nodes -keyout private.key -out request.csr
    一步生成私钥和 CSR
  • openssl req -x509 -newkey rsa:2048 -nodes -keyout private.key -out cert.pem -days 365 -subj "/CN=localhost"
    生成自签名证书(365 天)
  • openssl req -x509 -newkey rsa:2048 -nodes -keyout key.pem -out cert.pem -days 365 -subj "/CN=localhost" -addext "subjectAltName=DNS:localhost,DNS:*.localhost"
    自签名证书含 SAN 多域名

私钥处理 Private Key

  • openssl rsa -in private.key -check -noout
    检查私钥是否有效
  • openssl rsa -in encrypted.key -out plain.key
    去除私钥密码保护(需输入原密码)
  • openssl rsa -in plain.key -aes256 -out encrypted.key
    给私钥加 AES-256 密码保护
  • openssl rsa -in private.key -pubout -out public.key
    从私钥导出公钥
  • openssl pkey -in private.key -noout
    通用私钥查看(兼容 RSA 和 EC)

格式转换 Format Conversion

  • openssl pkcs12 -export -out cert.p12 -inkey private.key -in cert.pem
    PEM 转 PKCS12(.p12/.pfx)
  • openssl pkcs12 -in cert.p12 -out cert.pem -nodes
    PKCS12 转 PEM(含私钥,-nodes 不加密)
  • openssl pkcs12 -in cert.p12 -info -noout
    查看 PKCS12 内容
  • openssl crl2pkcs7 -nocrl -certfile cert.pem -out cert.p7b
    PEM 转 PKCS7(.p7b)

加密与哈希 Encrypt & Hash

  • echo -n "hello" | openssl md5
    计算 MD5 哈希
  • echo -n "hello" | openssl sha256
    计算 SHA-256 哈希
  • openssl dgst -sha256 file.txt
    计算文件 SHA-256
  • openssl enc -aes-256-cbc -in file.txt -out file.enc -pass pass:secret
    AES-256-CBC 加密文件
  • openssl enc -d -aes-256-cbc -in file.enc -out file.txt -pass pass:secret
    AES-256-CBC 解密文件
  • openssl rand -hex 32
    生成 32 字节随机十六进制字符串
  • openssl rand -base64 32
    生成 32 字节随机 Base64 字符串

容易踩的坑

  • 查远程证书有效期加 -servername(SNI),否则多域名证书可能返回默认证书。
  • Let's Encrypt 证书到期前用 echo | openssl s_client 确认实际部署的证书已更新,不要只看文件时间戳。
  • PKCS12 转 PEM 时加 -nodes 否则导出的私钥会被密码保护,Nginx/Traefik 无法直接读取。