How to encrypt a file with OpenSSL on Windows (password or public key)
OpenSSL is a free command-line toolkit that can encrypt a file with a password or with someone's public key. A few options decide how well the file is protected, and some defaults are old. These commands were tested with OpenSSL 3.2 on Windows 11 and checked against OpenSSL's manual.
openssl enc -aes-256-cbc -pbkdf2 -iter 600000 -salt -in report.pdf -out report.pdf.enc, type a password, and decrypt later with the same options plus -d. Always include -pbkdf2, which makes password guessing much slower. openssl enc has no tamper check and no AES-GCM; for that, or to use a public key, use openssl cms. Lose the password or private key and the file can't be recovered.1. Get OpenSSL on Windows
In PowerShell, run openssl version. If the command isn't recognised:
- Git for Windows includes it. Its release notes list the bundled OpenSSL (3.5.9 in October 2026). In Git Bash,
openssljust works. To use it from PowerShell, print its path in Git Bash withcygpath -w "$(command -v openssl)", then runSet-Alias openssl "that path"in PowerShell (it lasts until you close the window). - A signed installer. The OpenSSL project's wiki points to signed Windows installers from the OpenSSL Corporation (x64, x86 and Arm64, EXE or MSI) on the openssl/installer releases page. The installer can add OpenSSL to your PATH.
OpenSSL's downloads page lists 4.1, 4.0 and 3.5 (long-term support) and says every other version is out of support, so update if you see anything older than 3.5 (for example 1.1.1, 3.0 or 3.2). Many Linux distributions include OpenSSL as a package; see file encryption on Linux.
2. Encrypt a file with a password
openssl enc -aes-256-cbc -pbkdf2 -iter 600000 -salt -in report.pdf -out report.pdf.enc
-aes-256-cbc: AES with a 256-bit key in CBC mode, which the manual suggests for beginners.-pbkdf2 -iter 600000: PBKDF2 with 600,000 iterations (the manual's default is 10,000). More iterations slow down each guess.-salt: a random salt; the default, and the manual says always use it with passwords.
OpenSSL asks for the password with typing hidden. The original stays: test-decrypt first (step 3), then delete it.
3. Decrypt it
openssl enc -d -aes-256-cbc -pbkdf2 -iter 600000 -in report.pdf.enc -out report-check.pdf
Decrypt to a new name. OpenSSL overwrites the -out file without asking, even when decryption fails, so a wrong password can replace a file with garbage. Open the copy, then rename or delete it. Repeat the cipher, -pbkdf2 and -iter: the file stores only its salt. A mismatch or a wrong password normally gives bad decrypt. Note the command (not the password) with your backups.
Why -pbkdf2 matters
Without -pbkdf2 or -iter, OpenSSL derives the key the old way; OpenSSL 3.2 warned *** WARNING : deprecated key derivation used. in our test. With -pbkdf2 (OpenSSL 1.1.1 and later), high iteration counts "increase the time required to brute-force the resulting file". A long, unique password still matters most. Old files: the default digest changed from MD5 to SHA-256 in OpenSSL 1.1.0, so a file made by 1.0.2 or older without -pbkdf2 needs -md md5 to decrypt.
openssl enc can't detect tampering
The manual says enc "does not support authenticated encryption modes like CCM and GCM, and will not support such modes in the future", because it streams output before a tag could be checked. So an altered file can decrypt without complaint, as one changed byte did in our test. The padding check that usually catches a wrong password "isn't a very good test" either: random data passes it more often than 1 time in 256. The manual recommends openssl cms.
Passwords in scripts
Typing at the prompt is safest. For scripts, -pass accepts:
file:C:\keys\backup-pass.txt(Git Bash:file:C:/keys/backup-pass.txt): the first line. Make it readable only by your account, keep it out of synced folders, and save it as plain text: Windows PowerShell 5.1's>andOut-Filewrite UTF-16, which OpenSSL misreads, so useSet-Content -Encoding ASCII. Test it on a file you encrypted by typing the password.env:BACKUP_PASS: an environment variable. Other processes can see environments on some systems, so the manual says use it "with caution".stdin: the first line of standard input. A password piped from Windows PowerShell 5.1 didn't match in our test.pass:Blue-Fox-42: visible to other programs; the manual says use it only "where security is not important". PowerShell saves typed commands to a history file under%APPDATA%\Microsoft\Windows\PowerShell\PSReadLine, skipping lines with words like password or secret, so-pass pass:…can be saved. Git Bash keeps history in~/.bash_history.
Encrypt a file with a public key
RSA only encrypts small blocks: OpenSSL's documentation limits RSA with OAEP padding to the key size minus 42 bytes (with OAEP's default SHA-1; less with SHA-256), 342 bytes for a 3072-bit key. Try openssl pkeyutl -encrypt on a real file and you get data too large for key size. The answer is hybrid encryption: a random key encrypts the file, and RSA encrypts that key. openssl cms does both, in a standard format.
- The person who will decrypt makes a key pair and certificate, once:
It asks for a pass phrase for the private key;openssl req -x509 -newkey rsa:3072 -keyout private-key.pem -out certificate.pem -days 3650 -subj "/CN=My backup key"-days 3650means about ten years (the default is 30 days). Sharecertificate.pem; keep the private key safe. In Git Bash, write"//CN=My backup key", or Git for Windows turns the leading slash into a Windows path. - Anyone with certificate.pem encrypts:
Always name the cipher: before OpenSSL 3.5 the default was triple DES. The manual covers AES-GCM inopenssl cms -encrypt -binary -aes-256-gcm -in archive.zip -out archive.zip.p7m -outform DER -recip certificate.pem -keyopt rsa_padding_mode:oaepcmsfrom 3.0; on 1.1.1, use-aes256. For very large files, add-streamso the whole file isn't held in memory. - The key owner decrypts:
Withopenssl cms -decrypt -binary -inform DER -in archive.zip.p7m -inkey private-key.pem -out archive.zip-aes-256-gcm, an altered file fails (Error decrypting CMS structurein our test), but OpenSSL still writes the output file, overwriting any existing one. Delete it if you see that error.
The manual way, with pkeyutl:
openssl rand -hex -out file-key.txt 32
openssl enc -aes-256-cbc -pbkdf2 -in archive.zip -out archive.zip.enc -pass file:file-key.txt
openssl pkeyutl -encrypt -certin -inkey certificate.pem -pkeyopt rsa_padding_mode:oaep -pkeyopt rsa_oaep_md:sha256 -in file-key.txt -out file-key.txt.enc
Delete file-key.txt and send both .enc files. The key owner reverses it, then deletes file-key.txt:
openssl pkeyutl -decrypt -inkey private-key.pem -pkeyopt rsa_padding_mode:oaep -pkeyopt rsa_oaep_md:sha256 -in file-key.txt.enc -out file-key.txt
openssl enc -d -aes-256-cbc -pbkdf2 -in archive.zip.enc -out archive.zip -pass file:file-key.txt
OpenSSL on Windows wrote the key file with a Windows line ending in our test. On Linux or macOS, run tr -d '\r' < file-key.txt > k && mv k file-key.txt before the enc step, or you'll get bad decrypt. This route still has no tamper check, so cms is usually better.
What can go wrong
- A lost password or private key means lost files. OpenSSL has no recovery; back up the private key.
- Decrypting over a file replaces it, even with a wrong password.
- The original stays until you delete it, and may also be in backups or cloud version history.
- Redirecting with >. Use
-out. Microsoft says PowerShell 7.4 changed redirection to keep a program's output byte for byte; older versions didn't promise that.
What each option can and can't do
| Question | openssl enc | openssl cms | Clavis Encrypt |
|---|---|---|---|
| Unlocks with | A password | The private key (and its pass phrase) | Your vault password, or the recovery key |
| Detects changes to the file | No | Yes, with -aes-256-gcm | Yes, on every decrypt |
| Hides the file name | Only if you pick a meaningless output name | Same | Yes, the real name is stored inside the encrypted file |
| Forgotten password or lost key | File is lost | File is lost | Recovery key, if you kept it; otherwise lost |
| Command line and cost | Free | Free | App free for files; clavis-cli in Premium ($29 once) |
If you'd rather not manage the options
Clavis Encrypt makes these choices for you: AES-256-GCM with a key derived from your password by Argon2id, a tamper check on every decrypt, and the real file name hidden inside the .enc file. Encrypting files in the app is free on Windows and Linux. For scripts, clavis-cli comes with Clavis Premium ($29 once, tax included) in the downloads from clavisenc.com, not the Microsoft Store version; it never takes a password as an argument. See encrypting files from the command line and the clavis-cli reference. Clavis is closed source and hasn't been independently audited; its file format is public, with an open-source decryptor.
Questions
How do I encrypt a file with OpenSSL using a password?
Run openssl enc -aes-256-cbc -pbkdf2 -iter 600000 -salt -in file -out file.enc and type a password. To decrypt, add -d, use the .enc file as input and keep the same cipher, -pbkdf2 and -iter. Decrypt to a new file name: OpenSSL overwrites the output file without asking, even when the password is wrong.
Why does OpenSSL say "deprecated key derivation used"?
You left out -pbkdf2 and -iter, so OpenSSL used its old way of turning the password into a key. Use -pbkdf2 with a high -iter for new files; old files must be decrypted with the options they were made with.
Can openssl enc use AES-256-GCM?
No. OpenSSL's manual says enc doesn't support GCM or CCM and won't in the future. Use openssl cms with -aes-256-gcm (OpenSSL 3.0 or later) if you need a tamper check.
How do I encrypt a large file with an RSA public key in OpenSSL?
Use openssl cms -encrypt with the recipient's certificate, not RSA directly, which only handles a few hundred bytes. cms encrypts the file with a random key and encrypts that key with RSA; the recipient decrypts with openssl cms -decrypt.
What does "bad decrypt" mean in OpenSSL?
Usually a wrong password, or options that differ from the ones used to encrypt: cipher, -pbkdf2, -iter, or -md for files from very old versions. A damaged file can cause it too.
Sources
- OpenSSL 3.5 manual: openssl-enc (-pbkdf2 and -iter, 10,000 default iterations, salt, AES in CBC mode for beginners, no GCM or CCM, padding check, cms recommended, MD5 to SHA-256 change)
- OpenSSL 1.1.1 manual: enc (-pbkdf2 and -iter in 1.1.1)
- OpenSSL 3.5 manual: openssl-passphrase-options (pass:, env:, file:, fd:, stdin and their risks)
- OpenSSL 3.5 manual: openssl-cms (encrypt and decrypt, AES-GCM, default cipher changed from 3DES to AES-256 in 3.5, RSA-OAEP, -stream)
- OpenSSL 3.0 manual: openssl-cms (AES-GCM supported, triple DES default)
- OpenSSL 1.1.1 manual: cms (triple DES default)
- OpenSSL 3.5 manual: openssl-pkeyutl (-encrypt, -certin, RSA OAEP options)
- OpenSSL 3.5 manual: RSA_public_encrypt (input limit of key size minus 42 bytes with OAEP)
- OpenSSL 3.5 manual: openssl-req (-x509, -newkey, -days default of 30)
- OpenSSL 3.5 manual: openssl-rand (-hex, -out)
- OpenSSL downloads (supported releases: 4.1, 4.0, 3.5 LTS)
- OpenSSL wiki: Binaries (Windows installers from the OpenSSL Corporation; third-party builds)
- OpenSSL Windows installer README (signed, x64, x86 and Arm64, EXE and MSI, Adjust PATH option)
- Git for Windows release notes (comes with OpenSSL 3.5.9; path conversion of arguments starting with a slash)
- Microsoft Learn: about_PSReadLine (command history file and the words it filters)
- Microsoft Learn: about_Character_Encoding, Windows PowerShell 5.1 (Out-File and > write UTF-16LE; Unicode encodings add a BOM)
- Microsoft Learn: about_Redirection (byte-for-byte redirection of native commands from PowerShell 7.4)
- GNU Bash manual: Bash variables (HISTFILE defaults to ~/.bash_history)
Commands tested with OpenSSL 3.2.1 on Windows 11; facts checked against the OpenSSL manuals on November 11, 2026. Options can change between versions; openssl enc -help on your own PC is the final word.
Related: encrypting a file with GPG on Windows, encrypting a file with PowerShell and 7-Zip from the command line with a password.
One-time payment, tax included, no subscription. The command line comes with the installer from clavisenc.com, not the Microsoft Store version, so start with the free installer.