Encrypting multiple files with AES256 using tar command

I tried to encrypt some files at once with AES256 ,but it failed. I could encrypt only 1 file at a time,wery bothersome .There are this tar command which can create a single file from several files - but I failed at this - something wrong with the command line . Is there any clearcut command line how to create a single file from many files ? Then after that I can encrypt all those data with AES256 .


[Re-Titled by Moderator]

iMac 24″, macOS 15.2

Posted on Jan 31, 2025 6:28 AM

Reply
Question marked as Top-ranking reply

Posted on Jan 31, 2025 12:09 PM

Here is one approach in the Terminal.


Encrypt a compressed tar file from a folder SVG on the Desktop:

tar --exclude='.DS_Store' -cvzp SVG/ | openssl enc -aes-256-cbc -md md5 -pass pass:foobar > a.bin

file a.bin

a.bin: openssl enc'd data with salted password


Decrypt and expand a.bin in different folder on Desktop as tar contents
cd tar_expanded
openssl enc -d -aes-256-cbc -md md5 -pass pass:foobar -in ../a.bin | tar -xvf -
x SVG/
x SVG/scanwo_ocr.pdf
x SVG/foo_ocr_stripped.pdf
x SVG/foo.pdf


Tested: macOS Sequoia v15.3, M4 Mac Mini Pro

25 replies
Question marked as Top-ranking reply

Jan 31, 2025 12:09 PM in response to garage4996

Here is one approach in the Terminal.


Encrypt a compressed tar file from a folder SVG on the Desktop:

tar --exclude='.DS_Store' -cvzp SVG/ | openssl enc -aes-256-cbc -md md5 -pass pass:foobar > a.bin

file a.bin

a.bin: openssl enc'd data with salted password


Decrypt and expand a.bin in different folder on Desktop as tar contents
cd tar_expanded
openssl enc -d -aes-256-cbc -md md5 -pass pass:foobar -in ../a.bin | tar -xvf -
x SVG/
x SVG/scanwo_ocr.pdf
x SVG/foo_ocr_stripped.pdf
x SVG/foo.pdf


Tested: macOS Sequoia v15.3, M4 Mac Mini Pro

Jan 31, 2025 7:18 PM in response to garage4996

If you don't want to have the tar archive to be compressed, then just omit the "z" option for the tar command @VikingOSX provided.


If you don't want to include the parent folder containing the files, then you can add "-C SVG . " in place of where @VikingOSX has " SVG/" so that the resulting "tar" archive does not contain the parent "SVG" folder and will just include the three files listed in @VikingOSX's example.

tar --exclude='.DS_Store' -cvp -C SVG  .  | openssl enc -aes-256-cbc -md md5 -pass pass:foobar > a.bin



Or leave use @VikingOSX's original command to include the "SVG" parent folder within the encrypted archive and just extract the contents of the parent folder included in the tar archive by adding the following option immediately after the "tar" command, but before the other options " -xvf - "......" --strip-components 1":

openssl enc -d -aes-256-cbc -md md5 -pass pass:foobar -in ../a.bin | tar --strip-components  1  -xvf -



I tested both of these modified commands (macOS 13.7.1 Ventura) and either one of these modified commands will give you the following output (notice the "SVG" parent folder is not in the output):

x scanwo_ocr.pdf
x foo_ocr_stripped.pdf
x foo.pdf



Of course we are assuming you are dealing with files within a single folder, otherwise things get a lot more complicated.


Otherwise you need to provide more details since you seem to go from wanting to use "tar" and have an encrypted "tar" archive file, but now you just want to encrypt lots of individual files? You need to pick one goal for us to focus on. And are the files you want to encrypt within a single folder or scattered around different locations?

Feb 1, 2025 1:26 PM in response to MrHoffman

ankblomma@iMac-som-tillhor-Ankblomma-2 Downloads % zip -er /Users/ankblomma/Desktop/kacsa.zip /Users/ankblomma/Desktop/kacsa  
Enter password: 
Verify password: 
  adding: Users/ankblomma/Desktop/kacsa/ (stored 0%)
  adding: Users/ankblomma/Desktop/kacsa/.DS_Store (deflated 97%)
  adding: Users/ankblomma/Desktop/kacsa/photo_2024-12-29 17.54.30.jpeg (deflated 3%)
  adding: Users/ankblomma/Desktop/kacsa/photo_2024-12-29 17.55.33.jpeg (deflated 3%)
  adding: Users/ankblomma/Desktop/kacsa/photo_2024-12-29 17.55.24.jpeg (deflated 3%)
  adding: Users/ankblomma/Desktop/kacsa/photo_2024-12-29 17.55.03.jpeg (deflated 3%)
  adding: Users/ferencbanyai/Desktop/kacsa/photo_2024-12-29 17.55.14.jpeg (deflated 2%)
ankblomma@iMac-som-tillhor-Ankblomma-2 Downloads % 

Now getting this message deflated ? I see that the files are encrypted , but I would encrypt them without compression . What is that deflated means ? It sounds not good. What is the encryption strength here ? AES256 would be the best .97 % deflated ,that means nothing remains of those files? Or wery small in size ,that is not good.

Feb 1, 2025 11:54 AM in response to garage4996

You’re less likely to have issues with the built-in DMG tools or with encrypted removable storage, as Apple goes to some effort to ensure compatibility.


If the Apple tooling is somehow not working reliably in your environment, I’d wonder about and might expect that using command-line scripting is going to be equally problematic.


As for your current path: The openssl command is still around on macOS, but OpenSSL was deprecated and replaced with LibreSSL. This as OpenSSL itself has a history of making breaking changes. This in addition to the pitfalls.


The previously-linked age tool (available vis Homebrew) avoids most of the common pitfalls of OpenSSL (or the LibreSSL version of OpenSSL) and provides robust capabilities.


If your security threat is not advanced, zip encryption with a robust passphrase can be entirely serviceable.


More secure:

zip --encrypt filearchive.zip inputfile otherfile anotherfile


Less secure, as the password is in the shell history:

zip --password your-password-here filearchive.zip inputfile otherfile anotherfile

Feb 1, 2025 3:53 AM in response to garage4996

Okay, given your seeming unfamiliarity with UNIX (a folder is a directory is a folder), and given your hesitance around posting commands and errors, and given the sometimes subtle pitfalls with OpenSSL, I’d suggest using an encrypted disk image, or an encrypted removable storage device. That works, that can use AES-128 or AES-256, and that also avoids needing to use the command shell. And if you pick an encrypted sparse disk image, the encrypted disk image file can grow as needed,



If you really want to use the command line for whatever reason including scripting, hdiutil is available and can create encrypted sparse disk images.


Feb 1, 2025 9:58 AM in response to garage4996

garage4996 wrote:

.dmg files I used before and I failed to open it after sometimes ,it simply refused to open . That File Vault is ok ,I use that too . But I may need to know some other ways to encrypt files too .


Encrypted DMG files can and do work, and are widely used. What command and what errors are you encountering?


Encrypted sparse bundles will usually be the easiest (and most portable across Macs) way, potentially scripted with hdiutil and ilk where appropriate.


(a folder is a directory is a folder), how to understand this ? A folder is a directory in a folder ? Or a folder is also a directory?


Synonym.


I could not find anything on internet how to encrypt , in terminal , with openssl several files at one command ,that is why I cannot post anything useful here ,it is only a waste of time ,it is only a guessing - that it maybe so or not.


That seems a vastly different starting point from “I tried to encrypt some files at once with AES256 ,but it failed.”


Over the years I learned ,not to waste my time on stuffs which is not clear to me ,or which I cannot in any way understand .


That means increasing your familiarity with UNIX and zsh as a starting point, and then later some more experience around cryptography, as too many of the OpenSSL examples around are old or otherwise insecure.


There are books and documents and videos on these topics, depending on how you best learn.


Or maybe you have an idea how to use the terminal to encrypt several files at once with openssl ? That would be a great help.


Okay. Please post your code, and please post the errors. We’ll start from there. It’s usually easier to start from a non-working example than to start anew, particularly as some context can usually be acquired from the failing code, after all.


As for alternatives to OpenSSL, I’ve suggested using hdiutil and encrypted sparse images earlier, and have also suggested age. age avoids various of the pitfalls that can arise with other tools.


Some background on the subtleties of “encrypting a file”, this in the context of age:

https://words.filippo.io/dispatches/age-authentication/


Some docs:

https://htmlpreview.github.io/?https://github.com/FiloSottile/age/blob/main/doc/age.1.html


Exceedingly quick intro to zip:


zip -r filename.zip /path/to/directory


Prompting for a passphrase encryption and decryption:


$ age --passphrase filearchive.zip > filearchive.zip.age
$ age —-decrypt filearchive.zip > filearchivedecrypted.zip



Your unfamiliarity with UNIX and your familiarity with MD5 is an interesting juxtaposition here, too. Someone unfamiliar with files and folders and directories can get into trouble with tools such as OpenSSL or pgp (and from various cryptographic attacks), while someone familiar with he issues of MD5 undoubtedly already knows this.



Some light reading on pitfalls and considerations: https://research.checkpoint.com/2024/modern-cryptographic-attacks-a-guide-for-the-perplexed/

Jan 31, 2025 10:08 AM in response to garage4996

garage4996 wrote:

I tried to encrypt some files at once with AES256 ,but it failed.

I could encrypt only 1 file at a time,wery bothersome .There are this tar command which can create a single file from several files - but I failed at this - something wrong with the command line .

Is there any clearcut command line how to create a single file from many files ? Then after that I can encrypt all those data with AES256 .

[Re-Titled by Moderator]


you do not say what is your reference here...(?) nor what exact "command line"...


you can read from the Terminal.app reference the manual:

man tar | more

Jan 31, 2025 7:59 PM in response to garage4996

What were the tar and encryption commands used, and what were the resulting error messages shown?


I’m guessing OpenSSL was involved. That tends to be complex, and can get cryptic, and some of the defaults (particularly around md5) can be quite bad. And re-using the salt can get you in deep trouble, too. (Do not use md5!)


(This is why I’d asked for the commands used snd errors produced.)


Use of tar or zip is a means to separate the file management, packaging, and compression from the encryption and decryption.


Alternatives to OpenSSL with some better esse-of-use and default security include age, which is available via Homebrew.

Feb 1, 2025 12:01 PM in response to MrHoffman

zip -r filename.zip /path/to/directory



filename « mydogbernie.jpg.zip » how to overwrite this ? mydogbernie.zip ? /path/to/directory


most be


/Users/ankblomma/Desktop/retep.png   

    
    /Users/  most be the path ?  /to/ most be ankblomma ?    /directory  most be  /Desktop 




Then will the file be encrypted ? It is not that ,that it is difficult to learn the CLI ,but there are no clear cut instructions to this computing .

Feb 1, 2025 2:33 PM in response to garage4996

garage4996 wrote:


MrHoffman wrote:
Encrypted DMG files can and do work, and are widely used. What command and what errors are you encountering?

I typed the passwords in and it did not opened - it is a right password as Bitwarden newer fails in this respect . Or it simply not opening ,it does not asking for a password at all. But I like .dmg files - wery easy to create them in minute - but how to ensure that they will always open ? What to do ,so that I can keep them always in good condition? Or those keys goes out of validity ? Maybe I most change passwords 1 a year ? Why is so that I cannot open a .dmg file on a new Mac ? I have the same Apple ID ,so I cannot understand what is the fault .

Most likely the DMG archive was unlocked, but would not mount. Think of the DMG as an "external" drive you are connecting when you double-click on the DMG file. You will encounter the same issues with them as you would any external drive since the DMG contains a file system like any drive. I've had the same thing happen with Filevaulted APFS volumes as well.


Thing is you must make sure to have good backups of that archive and the data contained within it. Keep in mind that if anything happens to an encrypted file/image, then it may be impossible to access it. So it is best to keep a copy of the data outside of a DMG archive as well.


From everything you have posted here, I agree with @MrHoffman and think you need to either utilize a Filevaulted external drive, or look at using some third party software, otherwise you have a lot of research & experimentation to do. One thing which could help you use just the built-in macOS tools & utilities would be to create an Apple Script and/or Automator action (probably will still need to use one of the command line options as well) in order to scan a folder to encrypt any files that are not encrypted.....again it will take some research & experimentation.


FYI, I have had trouble creating ZIP archives at times for reasons I could never pinpoint exactly (never looked too deeply...it happened with the macOS Compress option and even the command line tools & using 7-zip even on Linux), so I have resorted to utilizing TAR because it just works.


Also with 2018+ Macs, the data on the internal SSD is always hardware encrypted. If you enable Filevault on the internal boot SSD, then the data is protected & requires that password to unlock the data no matter how you try to access the system (at least as good as the password you use, but the same goes for any other option). And Time Machine can encrypt your backups as well.


I just thought of something that may be the easiest all native macOS solution:

Create a new APFS volume next to your macOS installation within the same APFS Container. You can mount & unmount this new APFS volume anytime you want, plus you can enable Filevault on it as well I believe. Even if you cannot, then just unmount the volume when you don't need access to that data as that will give you a bit of protection for it. If the APFS volume is not encrypted with Filevault, then you can configure macOS to automatically prevent that volume from mounting, but you can always use Disk Utility to mount it at any time you wish. Here is an Apple article with details about creating a new APFS volume within the same Container as your macOS installation:

Add, delete, or erase APFS volumes in Disk Utility on Mac - Apple Support


Jan 31, 2025 4:09 PM in response to VikingOSX

Last login: Thu Jan 30 22:14:21 on ttys001
ankblomma@iMac-som-tillhor-ankblomma-2 ~ % openssl aes-256-cbc -in ~/kiruna.jpg -out ~/hidekiruna.file
enter aes-256-cbc encryption password:
Verifying - enter aes-256-cbc encryption password:
ankblomma@iMac-som-tillhor-Ankblomma-2 ~ % 


I used this command for encrypting a file ,but to encrypt many files in one stroke I could not - I looked at that man details ,but it is a tons of information so I could not see anything interesting there about tar .

There most be some command line where I could collect all the files and encrypt them in one slap . I tried with a folder ,but it was an error.There are some 100 files I want to encrypt ,and to do that is one by one is wery trouble some . I can do this with an AES tool ,or GPG or VeraCrypt or simply creating a .dmg file ,but I prefer the Terminal as it is wery local and not using any other tools is preferable for me. A .dmg file is also ok ,but I could not open that file last time ,so maybe the terminal is better . I heard that even this is not a good idea ,as it backfires ,that is cannot be opened on, say another Mac .


Feb 1, 2025 3:28 AM in response to MrHoffman

MD5 ? That was used in the 1950 ?? That is Stone Age ...


I still cannot figure out what to do .I have some 100 documents ,photos ,contracts, bank details ,heritage letters and so on. Now after a lots of job I collected all those in one folder -in Documents. True they were scattered all over ,as HWTech said - in Finder ,but now all documents are in one folder -in Documents . Then I found that command and and was happy in the beginning for that ,then after writing all those files into that formula ,it rejected all files and my job was meaningless .Only 1 file at a time was possible to encrypt ,but that is a chore ,it will take months before all those files are encrypted. I do not want to use any 3rd party apps for this.


Similiarly ,I had fear for creating QR codes on someone's website ,I simply do not trust them . Then I could generate a QR code from Shortcuts ,everything is locally made and I no need to fear anything .

The same most be here with the Open SSL . I found a formula to encrypt 1 file at a time . OpenSSL is great ,it uses AES256 too.


TOUCH creates a FILE and MKDIR creates a DIRECTORY - But what commands creates a FOLDER ?


I want to encrypt this Folder only . 1 file at a time is not a problem ,but a Folder yes .I will try it again and see.

Feb 1, 2025 3:36 AM in response to HWTech

They was scattered all over Finder ,now I collected them in one folder . I do not know if "tar" is neccessary to use for this ,I just Googled and found that command line . Probably it is meaningless to use tar. I have 1 folder with some 100 files or more inside it . It is this Folder I want to encrypt with OpenSSL . 1 File at a time is not a problem ,but many at a time it is.

Feb 1, 2025 8:48 AM in response to MrHoffman

ankblomma@iMac-som-tillhor-ankblomma-2 ~ % openssl enc -aes-256-cbc -e -in /Users/ankblomma/Downloads/0089895/WhatsApp\ Image\ 2025-01-19\ at\ 08.26.49.jpeg -out /Users/ankblomma/Downloads/0089895/WhatsApp\ Image\ 2025-01-19\ at\ 08.26.49_enc.jpeg
enter aes-256-cbc encryption password:
Verifying - enter aes-256-cbc encryption password:
ankblomma@iMac-som-tillhor-ankblomma-2 ~ % 


This one is the only one more way I found to encrypt a file copy and paste the user of a file ,then change to _enc.jpeg in the end . I looked at several websites ,but nothing I found. Strange that you cannot encrypt a folder in one action . One by one that you can ,but not several files at once.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Encrypting multiple files with AES256 using tar command

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.