A practical, no-nonsense guide from beginner to advanced.
All commands assumeopensslis installed and in your$PATH.
- Key = secret or public data used to encrypt/sign things.
- Private key = keep it safe, never share.
- Public key = can be shared with others.
- CSR (Certificate Signing Request) = “Hey CA, please sign this public key for this domain.”
- Certificate (
.crt,.pem,.cer) = public key + identity info + signature. - CA (Certificate Authority) = entity that signs certs (root or intermediate).
Common file formats:
.pem= Base64 text with-----BEGIN .../-----END ....key= private key (usually PEM).crt/.cer= certificate (PEM or DER).pfx/.p12= PKCS#12 bundle (cert + key + chain), usually password protected
- 0. Quick Concepts
- 1. Check OpenSSL Version
- 2. Generate Private Keys
- 3. Create a CSR
- 4. Self-Signed Certificates
- 5. View and Inspect Files
- 6. File Formats: PEM, DER, PFX
- 7. Hashing & Checksums
- 8. Symmetric Encryption (AES)
- 9. Asymmetric Crypto: Sign & Verify
- 10. Certificate Chain & Expiry
- 11. Debugging TLS Connections
- 12. Diffie-Hellman Parameters
- 13. Password & Random Tools
- 14. Identify Unknown Files
- 15. Useful One-Liners
- 16. Security Best Practices
- 17. Quick Reference Table
openssl version
openssl version -a # with build info# 2048-bit RSA key (enough for most uses)
openssl genrsa -out server.key 2048
# 4096-bit (stronger but heavier)
openssl genrsa -out server-4096.key 4096openssl genpkey -algorithm RSA -out server.key -pkeyopt rsa_keygen_bits:2048# List available curves
openssl ecparam -list_curves
# Generate EC key using prime256v1 (aka secp256r1)
openssl ecparam -name prime256v1 -genkey -noout -out server-ec.key# Add password to private key (PEM)
openssl rsa -aes256 -in server.key -out server-protected.key
# Remove password (be careful!)
openssl rsa -in server-protected.key -out server-nopass.keyopenssl req -new -key server.key -out server.csrYou’ll be asked for:
- Country Name (C)
- State or Province (ST)
- Locality (L)
- Organization (O)
- Organizational Unit (OU)
- Common Name (CN) → usually your domain, e.g.
example.com - Email Address
openssl.cnf example snippet:
[ req ]
default_bits = 2048
default_md = sha256
prompt = no
distinguished_name = dn
req_extensions = req_ext
[ dn ]
C = US
ST = Some-State
L = Some-City
O = My Company
OU = IT
CN = example.com
[ req_ext ]
subjectAltName = @alt_names
[ alt_names ]
DNS.1 = example.com
DNS.2 = www.example.comGenerate CSR:
openssl req -new -key server.key -out server.csr -config openssl.cnfopenssl req -x509 -newkey rsa:2048 -keyout server.key -out server.crt \
-days 365 -nodes-x509: make cert, not CSR-newkey rsa:2048: create key + cert in one go-nodes: do NOT encrypt the private key (no password)
openssl x509 -req -in server.csr -signkey server.key -out server.crt -days 365openssl x509 -in server.crt -noout -textopenssl req -in server.csr -noout -text# RSA key
openssl rsa -in server.key -check -noout
# EC key
openssl ec -in server-ec.key -check -noout# From private key
openssl pkey -in server.key -pubout -out server.pub
# From certificate
openssl x509 -in server.crt -pubkey -noout > server.pub# PEM cert -> DER
openssl x509 -in server.crt -outform der -out server.der
# DER cert -> PEM
openssl x509 -in server.der -inform der -out server.pem# Key + cert -> PFX
openssl pkcs12 -export -out server.pfx -inkey server.key -in server.crt
# Key + cert + chain -> PFX
openssl pkcs12 -export -out server.pfx \
-inkey server.key \
-in server.crt \
-certfile chain.crt# Extract private key
openssl pkcs12 -in server.pfx -nocerts -out server.key
# Extract certificate
openssl pkcs12 -in server.pfx -clcerts -nokeys -out server.crt
# Extract everything
openssl pkcs12 -in server.pfx -nodes -out all.pemopenssl dgst -md5 file.txt
openssl dgst -sha1 file.txt
openssl dgst -sha256 file.txt
openssl dgst -sha512 file.txtopenssl dgst -sha256 -hmac "secretkey" file.txtNote: built-in
encis ok for quick stuff, but for serious security use modern tools/libs.
# AES-256-CBC encryption
openssl enc -aes-256-cbc -salt -in plain.txt -out encrypted.binYou’ll be asked for a password.
openssl enc -d -aes-256-cbc -in encrypted.bin -out decrypted.txt# Encrypt and output Base64
openssl enc -aes-256-cbc -salt -in plain.txt -out encrypted.b64 -base64
# Decrypt from Base64
openssl enc -d -aes-256-cbc -in encrypted.b64 -out decrypted.txt -base64# Create signature.sig using private key
openssl dgst -sha256 -sign private.key -out file.sig file.txt# Verify with public key
openssl dgst -sha256 -verify public.pem -signature file.sig file.txt(Exit code 0 = success, non-zero = failure.)
openssl x509 -in server.crt -noout -subject -issuer -datesopenssl verify -CAfile ca.crt server.crt# ca-chain.crt contains intermediate + root
openssl verify -CAfile ca-chain.crt server.crtopenssl s_client -connect example.com:443openssl s_client -connect example.com:443 -showcertsopenssl s_client -connect example.com:443 -servername example.comopenssl s_client -starttls smtp -connect mail.example.com:587Other -starttls options: http, imap, pop3, ftp, etc.
(Used in some older configs; modern setups often use ECDHE instead.)
openssl dhparam -out dhparam.pem 2048# 16 random bytes as hex
openssl rand -hex 16
# 32 random bytes, base64
openssl rand -base64 32# SHA-512 based password hash (interactive)
openssl passwd -6Common switches:
-1= MD5 (old, don’t use)-5= SHA-256-6= SHA-512
# Try as certificate
openssl x509 -in unknown-file -noout -text
# Try as CSR
openssl req -in unknown-file -noout -text
# Try as private key
openssl rsa -in unknown-file -check -noout # RSA
openssl ec -in unknown-file -check -noout # EC
# Try to see if it's a PKCS#12 bundle
openssl pkcs12 -in unknown-file -infoIf one of those works → you know what you have 🙂
echo | openssl s_client -connect example.com:443 -servername example.com 2>/dev/null \
| openssl x509 -noout -datesopenssl x509 -in server.crt -noout -subject
# then look for CN=# Combine leaf + intermediate into one chain file
cat server.crt intermediate.crt > fullchain.crt-
Use at least RSA 2048 or EC prime256v1 for new keys.
-
Prefer SHA-256 over older hashes like SHA-1 or MD5.
-
Protect private keys:
- File permissions:
chmod 600 server.key - Store in safe location (and backups).
- File permissions:
-
For production certificates:
- Always include the full chain (leaf + intermediate).
- Regularly check expiry dates (monitoring/alerts).
| Task | Command (short version) |
|---|---|
| Generate RSA key | openssl genrsa -out key.pem 2048 |
| Generate EC key | openssl ecparam -name prime256v1 -genkey -out key.pem |
| Create CSR | openssl req -new -key key.pem -out req.csr |
| Self-signed cert | openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 365 -nodes |
| View cert | openssl x509 -in cert.pem -noout -text |
| View CSR | openssl req -in req.csr -noout -text |
| Cert → DER | openssl x509 -in cert.pem -outform der -out cert.der |
| Make PFX | openssl pkcs12 -export -out cert.pfx -inkey key.pem -in cert.pem |
| Verify cert | openssl verify -CAfile ca.pem cert.pem |
| Hash file (SHA-256) | openssl dgst -sha256 file |
| Random 32 bytes (base64) | openssl rand -base64 32 |
| Connect to HTTPS | openssl s_client -connect example.com:443 |
Feel free to copy this file as OPENSSL-CHEATSHEET.md into your GitHub repo.
::contentReference[oaicite:0]{index=0}