1

I have a folder with several files:

file1
file2
file3

I want to encrypt them all with AES including the file names so the output should be something like this:

kjk437fjk437
3k4jn34jk
j34nkj34

But I do not want to apply any compression at all.

Is it possible to do this with 7zip? I am using Debian and looking for Terminal based solutions only.

Edit: I also want to be able to get the filename back after decryption.

Vesa
  • 418
  • 2
  • 9
  • 21

3 Answers3

2

You probably just want to hide the filename instead of to encrypt it, so something like the following should do:

for file in ./*;do 
  7z a $RANDOM-$RANDOM.7z -m1=copy -mhe -psecret "$file"; 
  rm "$file" 
done

-m1=copy means use copy method, so no compression.
-mhe means encrypt header, so without password one cannot view filenames inside the 7z file.
-psecret sets password to secret

SparedWhisle
  • 4,025
  • 1
  • 20
  • 30
  • Interesting. But how can I get the filename back when I want to decrypt again? – Vesa Jan 21 '19 at 10:03
  • @Vesa the original filename is stored in the archive. You get the original file + filename back just by extracting it. – SparedWhisle Jan 21 '19 at 21:05
  • And this also AES encrypts the content of the file? – Vesa Jan 21 '19 at 21:31
  • @Vesa I believe it does. – SparedWhisle Jan 21 '19 at 21:48
  • I will test it soon – Vesa Jan 22 '19 at 00:10
  • Seems to work. Will apply some further testing. By the way I read somewhere that it's not recommended to use 7zip for backups because owner/group is not stored. What does it mean and can I still use 7zip for backup? – Vesa Jan 22 '19 at 02:23
  • @Vesa you are probably right. I believe 7zip was originally created for windows, then later ported to Linux, so it's support for permission/ownership might not be very good. But I'm not an expert on that, so cannot provide more details. I would recommend you do some research yourself, I'm sure there are already plenty of resources out there(or here on stackexchange) . – SparedWhisle Jan 22 '19 at 02:33
0

Is 7zip a must? Choose the right tool. EncFS seems to be it.

  1. Install it. In Debian: apt-get install encfs.
  2. Create two directories: mkdir encrypted mountpoint.

  3. Run the tool:

    encfs "$PWD/encrypted" "$PWD/mountpoint"
    

    Note you need $PWD/ instead of ./ because encfs doesn't accept relative paths (unless -f is used).

  4. Proceed as instructed to choose encryption, password.

  5. Copy or move all directories and files you want to encrypt to ./mountpoint. Encrypted directories and files will appear in the ./encrypted directory.

  6. Unmount:

    fusermount -u ./mountpoint
    

You can now copy/move/rename/tar/whatever the ./encrypted directory as a whole. Note there is a hidden .xml file inside. The file includes the (password protected) key which is crucial, so don't lose it. It's possible to store the file separetely (read about ENCFS6_CONFIG variable in man 1 encfs).

To access the original files, repeat step 3, provide the right password. Work with files under the chosen mountpoint: read, add, remove, modify, anything goes. Finally unmount with fusermount -u like in step 6.

Notes:

Kamil Maciorowski
  • 69,815
  • 22
  • 136
  • 202
  • Cannot use encfs on this device, otherwise I would as I used to before. – Vesa Jan 21 '19 at 10:02
  • @Vesa It's somewhat disappointing you knew about `encfs` and you knew you can't use it now, still you didn't explicitly ruled it out. I did notice you're asking about a 7zip solution; but sticking to some tool is often a Y in [XY problem](https://meta.stackexchange.com/a/66378/355310), while good answers should concentrate on X and this is what I was trying to do. The answer will stay (with other users in mind), unless you change the question so it clearly says `encfs` is not an option. Regardless, I do hope you'll get an answer that fits *you*. Good luck. – Kamil Maciorowski Jan 21 '19 at 10:45
0

You could encrypt your folder twice to hide your file names:

type="zip"; file="your-folder"; password="your-password"; # You also could use other formats.

7z a -t"$type" "$file.$type" -p"$password" "$file"
7z a -t"$type" "$file.$type.$type" -p"$password" "$file.$type"

Then the file "your-folder.zip.zip" is what you want.

Big Shield
  • 101
  • 1