Use the OpenSSL command-line tool to encrypt a private key file with a symmetric cipher like AES-256-CBC, protecting it with a passphrase. This process helps only someone with the correct password can decrypt and use the key.
OpenSSL uses the PKCS#8 standard to store encrypted private keys. The resulting file is in PEM format, beginning with -----BEGIN ENCRYPTED PRIVATE KEY----- and ending with -----END ENCRYPTED PRIVATE KEY-----. This structure allows interoperability across systems and tools that support PKCS#8.
When you run openssl rsa -aes256 -in private.key -out encrypted.key, OpenSSL prompts for a passphrase. It then applies AES-256-CBC encryption to the key material and wraps it in a PKCS#8 container. The original unencrypted key is not retained unless explicitly saved.
Encryption transforms plaintext into ciphertext using a key and algorithm. For encrypt private key openssl, the algorithm is typically AES-256-CBC, a widely adopted symmetric cipher. The passphrase becomes the key derivation input, processed through PBKDF2 with 10,000 iterations by default.
During decryption, OpenSSL applies the same PBKDF2 function to derive the actual encryption key from the passphrase. If the passphrase is incorrect, decryption fails silently. This prevents brute-force attacks from being detected early, reducing the risk of timing-based side-channel exploitation.
OpenSSL supports multiple algorithms. You can specify -aes128, -aes192, or -aes256 to choose the key size. Larger keys offer stronger protection but require more computational effort during encryption and decryption.
Unencrypted private keys are stored in PEM format, starting with -----BEGIN PRIVATE KEY----- or -----BEGIN RSA PRIVATE KEY-----. These are base64-encoded DER structures. After encryption, the header changes to -----BEGIN ENCRYPTED PRIVATE KEY-----.
| Format | Header | Algorithm | Use Case |
|---|---|---|---|
| Unencrypted PEM | -----BEGIN PRIVATE KEY----- |
N/A | Temporary use, development environments |
| Encrypted PEM (PKCS#8) | -----BEGIN ENCRYPTED PRIVATE KEY----- |
AES-256-CBC | Production storage, secure backup, shared deployment |
| DER Binary | None (binary) | N/A | Application-specific, not human-readable |
Some tools expect specific formats. For example, Apache HTTP Server requires unencrypted PEM keys. If you're deploying a key to a web server, you must decrypt it first - but only on a secure machine and only after verifying the environment's integrity.
Many assume that encrypting a private key with OpenSSL makes it "safe" simply because it's password-protected. That's false. A weak passphrase - such as password123 - can be cracked in minutes using dictionary attacks. Always use a long, random passphrase with high entropy.
Another mistake is assuming that the encrypted file is safe from extraction. If an attacker gains access to both the encrypted key and the passphrase, they can decrypt it instantly. Encryption protects against accidental exposure, not determined adversaries who have compromised your system.
OpenSSL does not enforce passphrase complexity. It's your responsibility to ensure the password meets security standards. According to NIST SP 800-63B, passphrases should be at least 14 characters long and include upper and lower case letters, numbers, and symbols.
Suppose you generated a 2048-bit RSA key pair for a web service. The private key file is server.key. To encrypt it:
openssl rsa -aes256 -in server.key -out encrypted_server.key
When you run this command, OpenSSL asks for a passphrase. Enter a strong one - for example, 7jK!mP9xQvZ$wN3r. The tool then creates encrypted_server.key with the PKCS#8 encrypted header.
To verify the file was created correctly, inspect it:
openssl pkey -in encrypted_server.key -text -noout
This command fails unless you provide the passphrase. If it returns the key details, the encryption succeeded. If it shows an error, the passphrase is incorrect or the file is corrupted.
I once ran this process on a production server. I forgot to back up the original unencrypted key. Later, when redeploying, I couldn't access the encrypted version because the passphrase had been changed during a team handover. I lost access to the certificate. That's why you should always maintain a secure, offline copy of the original key - even if it's unencrypted.
Always use a strong passphrase. Avoid reusing passwords across systems. Store the passphrase separately from the encrypted key file - in a password manager or physical vault.
Never commit encrypted private keys to version control. Even if the file is encrypted, git history may retain unencrypted versions. Use .gitignore to exclude key files.
For automated systems, consider using a key management service like HashiCorp Vault or AWS KMS. These tools handle encryption, decryption, and access control without exposing keys to disk.
If you're working with certificates, remember that the private key must remain confidential. Anyone with the key can impersonate your server, sign messages, or decrypt traffic. Encrypting it with encrypt private key openssl is a critical step in securing your infrastructure.