How to use Zip & 7zip to Encrypt/Decrypt and Password Protect Files/Folders in Linux

We have covered many articles about files and folder encryption and decryption as of now. In this article we are going to show you, how to encrypt/decrypt files and folders in Linux using native zip & 7zip archives.

We all know about zip & 7zip archives but we didn’t use even a single time for password protection. You can use for emergency purpose when you doesn’t have any utility for encryption.

Encryption is the process of encoding files using standard encryption algorithm which allows only authorized user to view.

Note : zip encryption method is known to insecure so use 7zip instead of zip.

Suggested Read :
(#) Cryptkeeper – An Easy Way To Encrypt And Decrypt Folder or Directory In Linux
(#) GnuPG – An Easy Way To Encrypt/Decrypt Files From Command Line in Linux
(#) SiriKali – GUI Front End To Manage ecryptfs, cryfs, gocryptfs, securefs, and encfs Encrypted Folders
(#) Gnome Encfs Manager – An Ease way to Create a Encrypted Directory in Linux
(#) Steghide – An Easy way to Hide Confidential Data Inside Images and Sound Objects in Linux
(#) Cipher – Two Simple Commands To Encrypt/Decrypt Files and Directories In Linux Command Line
(#) How To Use VIM Editor To Encrypt/Decrypt and Password Protect Files In Linux

Zip encryption is known to be insecure due to PKZIP stream cipher algorithm. 7zip compression methods supports encryption with AES-256 algorithm. This algorithm uses cipher key with length of SHA-256 bits.

What’s ZIP

zip is a compression and file packaging utility for Unix/Linux operating system. The program is useful for packaging a set of files for distribution; for archiving files; and for saving disk space by temporarily compressing unused files or directories.

The zip program puts one or more compressed files into a single zip archive, along with information about the files (name, path, date, time of last modification, protection, and check information to verify file integrity).

How to install Zip/Unzip on Linux

Zip is available in most of the operating systems own repository. These utilities are not pre-installed, so use a distribution official repository to install.

For Debian/Ubuntu, use apt-get command or apt command to install zip.

$ sudo apt-get install zip unzip

For RHEL/CentOS, use yum command to install zip.

$ sudo yum install zip unzip

For Fedora, use dnf command to install zip.

$ sudo dnf install zip unzip

For Arch Linux, use pacman command to install zip.

$ sudo pacman -S zip unzip

For openSUSE, use zypper command to install zip.

$ sudo zypper install zip unzip

How to use zip to Encrypt/Decrypt files

Use the following format to create a encrypted zip file. In this example, we are going to create a encrypted zip file called test.zip with test.txt & test1.txt file.

# zip --encrypt test.zip test.txt test1.txt
Enter password: ******
Verify password: *****
  adding: test.txt (stored 0%)
  adding: test1.txt (stored 0%)

Details :

  • zip : Command
  • –encrypt : To create encryption
  • test.zip : Zip file name
  • test.txt & test1.txt : These are files to be encrypted

To unzip encrypted zip file, just run the following command and input the password once.

# unzip test.zip
Archive:  test.zip
[test.zip] test.txt password: ******
replace test.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
 extracting: test.txt
replace test1.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
 extracting: test1.txt

For floder encryption use -r option with zip command. In this example, we are going to create a encrypted zip file called test1.zip with test.txt, test1.txt file & test-dir folder.

# zip -r --encrypt test1.zip test.txt test1.txt test-dir/
Enter password: ******
Verify password: ******
  adding: test.txt (stored 0%)
  adding: test1.txt (stored 0%)
  adding: test-dir/ (stored 0%)
  adding: test-dir/test1.txt (stored 0%)
  adding: test-dir/test.txt (stored 0%)

To unzip encrypted zip folder, just run the following command and input the password once.

# unzip test1.zip
Archive:  test1.zip
[test1.zip] test.txt password: ******
replace test.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
 extracting: test.txt
replace test1.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
 extracting: test1.txt
replace test-dir/test1.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
 extracting: test-dir/test1.txt
replace test-dir/test.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
 extracting: test-dir/test.txt

What’s 7zip

7z is the new archive format, providing high compression ratio with LZMA and LZMA2 compression. Usually, 7-Zip compresses to 7z format 30-70% better than to zip format. And 7-Zip compresses to zip format 2-10% better than most of other zip compatible programs. 7-Zip also supports encryption with AES-256 algorithm. This algorithm uses cipher key with length of 256 bits.

How to install 7zip on Linux

7zip is available in most of the operating systems own repository. It’s not pre-installed, so use a distribution official repository to install.

For Debian/Ubuntu, use apt-get command or apt command to install 7zip.

$ sudo apt-get install p7zip-full p7zip-rar

For RHEL/CentOS, enable EPEL repository and use yum command to install 7zip.

$ sudo yum install p7zip p7zip-plugins

For Fedora, use dnf command to install 7zip.

$ sudo dnf install p7zip p7zip-plugins

For Arch Linux, use pacman command to install 7zip.

$ sudo pacman -S p7zip

For openSUSE, use zypper command to install 7zip.

$ sudo zypper install p7zip

How to use 7zip to Encrypt/Decrypt Files

Use the following format to create a encrypted zip file using 7zip. In this example, we are going to create a encrypted 7zip file called test.7z with test.txt & test1.txt file.

# 7z a -p -mx=9 -mhe -t7z test.7z text.txt text1.txt

7-Zip [64] 16.02 : Copyright (c) 1999-2016 Igor Pavlov : 2016-05-21
p7zip Version 16.02 (locale=en_US.UTF-8,Utf16=on,HugeFiles=on,64 bits,1 CPU Intel Core Processor (Haswell, no TSX) (306C1),ASM,AES-NI)

Scanning the drive:
2 files, 68 bytes (1 KiB)

Creating archive: test.7z

Items to compress: 2


Enter password (will not be echoed): ******
Verify password (will not be echoed) : ******

Files read from disk: 2
Archive size: 261 bytes (1 KiB)
Everything is Ok

Details :

  • 7z : Command
  • a : Add
  • -p : Set Password
  • -mx=9 : Ultra compression
  • -mhe : Encrypt file names
  • -t7z : File type .7z
  • test.7z : 7zip file name
  • test.txt & test1.txt : Files to be encrypted

To unzip encrypted 7zip file, just run the following command and input the password once.

# 7za e test.7z

7-Zip (a) [64] 16.02 : Copyright (c) 1999-2016 Igor Pavlov : 2016-05-21
p7zip Version 16.02 (locale=en_US.UTF-8,Utf16=on,HugeFiles=on,64 bits,1 CPU Intel Core Processor (Haswell, no TSX) (306C1),ASM,AES-NI)

Scanning the drive for archives:
1 file, 261 bytes (1 KiB)

Extracting archive: test.7z

Enter password (will not be echoed):
--
Path = test.7z
Type = 7z
Physical Size = 261
Headers Size = 213
Method = LZMA2:12 7zAES
Solid = +
Blocks = 1


Would you like to replace the existing file:
  Path:     ./text.txt
  Size:     34 bytes (1 KiB)
  Modified: 2017-08-21 03:29:06
with the file from archive:
  Path:     text.txt
  Size:     34 bytes (1 KiB)
  Modified: 2017-08-21 03:29:06
? (Y)es / (N)o / (A)lways / (S)kip all / A(u)to rename all / (Q)uit? y


Would you like to replace the existing file:
  Path:     ./text1.txt
  Size:     34 bytes (1 KiB)
  Modified: 2017-08-21 03:29:20
with the file from archive:
  Path:     text1.txt
  Size:     34 bytes (1 KiB)
  Modified: 2017-08-21 03:29:20
? (Y)es / (N)o / (A)lways / (S)kip all / A(u)to rename all / (Q)uit? y

Everything is Ok

Files: 2
Size:       68
Compressed: 261

For floder encryption use -r option with 7zip command. In this example, we are going to create a encrypted 7zip file called test1.7z with test.txt, test1.txt file & test-dir folder.

# 7z a -p -mx=9 -mhe -t7z -r test1.7z text.txt text1.txt test-dir/

7-Zip [64] 16.02 : Copyright (c) 1999-2016 Igor Pavlov : 2016-05-21
p7zip Version 16.02 (locale=en_US.UTF-8,Utf16=on,HugeFiles=on,64 bits,1 CPU Intel Core Processor (Haswell, no TSX) (306C1),ASM,AES-NI)

Scanning the drive:
1 folder, 3 files, 102 bytes (1 KiB)

Creating archive: test1.7z

Items to compress: 4


Enter password (will not be echoed): ******
Verify password (will not be echoed) : ******

Files read from disk: 3
Archive size: 278 bytes (1 KiB)
Everything is Ok

To unzip encrypted 7zip folder, just run the following command and input the password once.

# 7za x test1.7z

7-Zip (a) [64] 16.02 : Copyright (c) 1999-2016 Igor Pavlov : 2016-05-21
p7zip Version 16.02 (locale=en_US.UTF-8,Utf16=on,HugeFiles=on,64 bits,1 CPU Intel Core Processor (Haswell, no TSX) (306C1),ASM,AES-NI)

Scanning the drive for archives:
1 file, 278 bytes (1 KiB)

Extracting archive: test1.7z

Enter password (will not be echoed):
--
Path = test1.7z
Type = 7z
Physical Size = 278
Headers Size = 230
Method = LZMA2:12 7zAES
Solid = +
Blocks = 1


Would you like to replace the existing file:
  Path:     ./test-dir/text.txt
  Size:     34 bytes (1 KiB)
  Modified: 2017-08-21 03:32:49
with the file from archive:
  Path:     test-dir/text.txt
  Size:     34 bytes (1 KiB)
  Modified: 2017-08-21 03:32:49
? (Y)es / (N)o / (A)lways / (S)kip all / A(u)to rename all / (Q)uit? y


Would you like to replace the existing file:
  Path:     ./text.txt
  Size:     34 bytes (1 KiB)
  Modified: 2017-08-21 03:29:06
with the file from archive:
  Path:     text.txt
  Size:     34 bytes (1 KiB)
  Modified: 2017-08-21 03:29:06
? (Y)es / (N)o / (A)lways / (S)kip all / A(u)to rename all / (Q)uit? y


Would you like to replace the existing file:
  Path:     ./text1.txt
  Size:     34 bytes (1 KiB)
  Modified: 2017-08-21 03:29:20
with the file from archive:
  Path:     text1.txt
  Size:     34 bytes (1 KiB)
  Modified: 2017-08-21 03:29:20
? (Y)es / (N)o / (A)lways / (S)kip all / A(u)to rename all / (Q)uit? y

Everything is Ok

Folders: 1
Files: 3
Size:       102
Compressed: 278

One Comment on “How to use Zip & 7zip to Encrypt/Decrypt and Password Protect Files/Folders in Linux”

Leave a Reply

Your email address will not be published. Required fields are marked *