Encrypt File on Mac Terminal: OpenSSL and GPG, Step by Step
Encrypt file on Mac Terminal with OpenSSL or GPG: exact commands, the risks (shell history, no integrity check, SSDs), and a right-click alternative.
You can encrypt a file in Terminal with openssl enc, which is already on your Mac, or with GnuPG, which you install separately (for example with Homebrew). Both read one file and write an encrypted copy that needs a password to open. Neither removes the original for you, and both have pitfalls that are easy to miss. This guide covers the exact commands and the downsides, then shows the same job as a right-click in Finderlock.
Check which OpenSSL you have first
macOS ships /usr/bin/openssl, but it is LibreSSL, not OpenSSL. The two share most options, not all. Check what you have:
openssl version
If the output starts with LibreSSL, the flags below may behave differently or be rejected, depending on your macOS version. If you see unknown option for -pbkdf2 or -iter, do not drop those flags and carry on. Without them, enc falls back to an old, weak key derivation. Install OpenSSL instead:
brew install openssl@3
"$(brew --prefix openssl@3)/bin/openssl" version
Then use that full path in place of openssl in the commands below.
Encrypt a file with OpenSSL
Open Terminal and go to the folder that holds the file:
cd ~/DocumentsEncrypt it. This example uses
report.pdf:openssl enc -aes-256-cbc -pbkdf2 -iter 600000 -salt -in report.pdf -out report.pdf.encType the password when prompted, then type it again to confirm. Let the prompt ask for it. Do not add
-kor-pass pass:yourpassword, because the password would then sit in your shell history and in the process list.Test the result before you touch the original (see the next section).
What the flags do: -aes-256-cbc picks the cipher and mode, -pbkdf2 -iter 600000 turns your password into a key with 600,000 rounds of PBKDF2 (slowing down password guessing), and -salt adds random data so the same password gives a different key each time.
Decrypt an OpenSSL file
Use the matching command with -d, and the same cipher and key derivation flags:
openssl enc -d -aes-256-cbc -pbkdf2 -iter 600000 -in report.pdf.enc -out report-decrypted.pdf
Then compare it with the original:
cmp report.pdf report-decrypted.pdf && echo "identical"
If you use the wrong password, you will often get a bad decrypt error. Sometimes you get garbage output instead. Plain CBC cannot reliably tell you the password was wrong, which leads to the next problem.
Encrypt with GnuPG (symmetric)
GnuPG is not part of macOS. The simplest install is Homebrew:
brew install gnupg
Then:
Encrypt with a passphrase, no key pair needed:
gpg --symmetric --cipher-algo AES256 report.pdfEnter a passphrase in the dialog that appears (twice). GnuPG writes
report.pdf.gpgnext to the original.Decrypt:
gpg --output report-decrypted.pdf --decrypt report.pdf.gpg
GnuPG adds a modification detection code by default, so a damaged or altered file is detected on decryption. That is a real advantage over plain openssl enc. One thing to know: gpg-agent may cache your passphrase for a while, so a second decryption shortly afterwards may not ask for it again.
Encrypt a folder: archive it first
Both tools work on a single file. For a folder, create an archive first, then encrypt the archive:
tar -czf project.tar.gz project
openssl enc -aes-256-cbc -pbkdf2 -iter 600000 -salt -in project.tar.gz -out project.tar.gz.enc
To get the folder back:
openssl enc -d -aes-256-cbc -pbkdf2 -iter 600000 -in project.tar.gz.enc -out project.tar.gz
tar -xzf project.tar.gz
The unencrypted project.tar.gz is still on disk after encrypting. It is easy to forget that this archive is as readable as the folder itself.
Delete the original (carefully)
Neither command removes your source file. Once you have decrypted a test copy and confirmed it matches, you can delete the original.
Warning:
rmis permanent. There is no Trash and no undo. Run it only after you have verified that you can decrypt the encrypted copy, and remember the password.
rm report.pdf
Do not assume this erases the data. On an SSD, deleting a file does not reliably overwrite the underlying storage, and overwrite tricks such as rm -P cannot be relied on there either. Copies can also survive in Time Machine backups, local snapshots, or cloud sync folders. If the plaintext was ever stored somewhere, treat it as possibly recoverable.
Where the Terminal approach falls short
The commands work, and for scripting or a one-off transfer they are a reasonable choice. The weak points are practical:
- Shell history. Typing a password inside a command (
-k,-pass pass:...) can store it in plain text in~/.zsh_history. Use the interactive prompt. - No integrity check with plain CBC.
openssl enc -aes-256-cbcencrypts but does not authenticate. Someone could alter the ciphertext and you may not be told. GnuPG does better here. - You manage the cleanup. Originals, temporary archives and decrypted copies are all your responsibility.
- No Finder integration. No right-click, no icon showing that a file is locked, no automatic re-locking after you close it.
- A forgotten password means lost data. There is no recovery. Store the password in a password manager.
- Flag drift. LibreSSL versus OpenSSL differences mean a command from a tutorial can fail or behave differently on your machine.
For protecting a whole disk, macOS already has FileVault, which does that job well. It does not protect individual files from someone who is logged in as you, which is the gap these tools fill. For a wider comparison of built-in options, see 4 ways to password protect a folder on Mac.
Terminal tools compared with Finderlock
openssl enc (CBC) |
GnuPG symmetric | Finderlock | |
|---|---|---|---|
| Installed by default | Yes (LibreSSL; flags may differ) | No (e.g. Homebrew) | App from finderlock.com |
| Cipher and mode | AES-256-CBC | AES-256 | AES-256-GCM (authenticated) |
| Tamper detection | No | Yes (modification detection code) | Yes (GCM authentication) |
| Password storage | You remember it | You remember it | macOS Keychain, or Touch ID on paid plans |
| Folders | Archive with tar first |
Archive with tar first |
Archived and sealed automatically |
| Finder right-click | No | No | Yes (macOS 14 or later) |
| Original file | You delete it | You delete it | Replaced by a placeholder when locked |
The iteration counts differ too. The OpenSSL command above uses 600,000 PBKDF2 iterations, and Finderlock uses 150,000 with PBKDF2-HMAC-SHA256 and a random 32-byte salt. In every case, a long, unique password matters more than the tool.
Do the same from a right-click with Finderlock
Finderlock is a Mac app that does this without a command line. Each protected item becomes its own .secured container, encrypted with AES-256-GCM through Apple CryptoKit. For a folder, it is archived first and sealed in independent 4 MB chunks. The key comes from your password through PBKDF2-HMAC-SHA256 with 150,000 iterations and a random salt.
- Install Finderlock from finderlock.com. It needs macOS 13.5 or later, and the Finder right-click menu needs macOS 14.
- Right-click a file or folder in Finder and choose Protect.
- Set a password for that item. It is stored in your macOS Keychain, so you do not type it into a command. On Personal and above you can use Touch ID instead.
- To open it later, double-click the placeholder file left at the original location. It unlocks without opening the main app window. Lock seals it again, and Unlock is also in the right-click menu.
You can also set auto-lock on inactivity or sleep, and Finderlock can lock an item again when the app that opened it quits. On the Lifetime plan, Shred does multi-pass secure deletion, but like any overwrite method it should not be relied on to wipe data from an SSD.
There are limits. A forgotten item password cannot be recovered by anyone, so keep it somewhere safe. Finderlock can back up encrypted containers to iCloud or Google Drive, and its recovery tool works with the master password if you set one. Everything runs locally, apart from the license check, optional opt-in analytics, updates and any backup you choose to enable. The details are on the security page and in the docs.
The Free plan covers 3 protected items, so you can try it on real files. Personal, Pro and Lifetime are one-time purchases. If you are weighing options, see best Mac encryption apps.
FAQ
How do I encrypt a file on Mac from Terminal?
Run openssl enc -aes-256-cbc -pbkdf2 -iter 600000 -salt -in file -out file.enc and enter a password at the prompt. Or install GnuPG and run gpg --symmetric --cipher-algo AES256 file. Check openssl version first, because the built-in LibreSSL may not accept every flag.
Is openssl enc secure enough?
The cipher is strong, but plain CBC has no integrity check, so tampering may go unnoticed. It is fine for a private file you will decrypt yourself, as long as the password is strong and you never put it on the command line. For tamper detection, use GnuPG or an authenticated mode such as GCM.
How do I decrypt a file encrypted with OpenSSL on Mac?
Use the same command with -d added and the same cipher and key derivation flags: openssl enc -d -aes-256-cbc -pbkdf2 -iter 600000 -in file.enc -out file. If the flags differ from the ones used to encrypt, decryption fails.
Can I encrypt a whole folder in Terminal?
Not directly. Create an archive with tar -czf folder.tar.gz folder, then encrypt the archive. Afterwards, remember to deal with the unencrypted archive and the original folder.
Does deleting the original file make it unrecoverable?
No, not reliably. rm removes the directory entry, and on SSDs you cannot count on overwriting the old data. Backups and snapshots may also hold copies. Verify your decryption first, then delete, and treat the plaintext as possibly recoverable.
Next step
If you only need to encrypt a file occasionally, the OpenSSL or GnuPG commands above will do the job, as long as you use the password prompt and test a decryption before deleting anything. If you protect files regularly and want authenticated encryption, Keychain storage and a right-click workflow, try the free plan at finderlock.com with up to 3 protected items.