0

i used ZipArchive::EM_TRAD_PKWARE, with password, but zip file is not encrypted. I think that pkware not working. Have solutions ?

Thx

$zip->setEncryptionName('test.txt', ZipArchive::EM_TRAD_PKWARE, 'PASSWORD'); 

1 Answers1

1

I got it working by the following example and rules:

Two rules to make it work

I got inconsistent results when not following these 2 rules:

  1. Make sure the file to be encrypted contains data
  2. Make sure addFile is called before setEncryptionName

Working example

test.txt

Create the following test.txt file with the following content:

test

test.php

Add the following PHP code to test.php:

$zip = new ZipArchive();
$zip->open("test.zip", ZipArchive::CREATE);
$zip->addFile("test.txt");
$zip->setEncryptionName('test.txt', ZipArchive::EM_TRAD_PKWARE, 'PASSWORD'); 
$zip->close();

Run test.php

php test.php

A zip file is now created which prompts for a password on unzip.

Douma
  • 2,682
  • 14
  • 23
  • Thx for answer. I have this strucutre, and i create your example, but when i open a zip file, password not required. I can open the test.txt – Lukáš Mesároš Nov 23 '21 at 19:14
  • Strange, which OS are you using and which php version? – Douma Nov 23 '21 at 19:17
  • Tested and working. See properties with 7-zip get these result. Method: ZipCrypto Store, Characteristics: Encrypt Descriptor, Packed Size: 17, CRC: 3610A686. Password asked. Tested on Windows 10 PHP 8.0.8 – vee Nov 23 '21 at 19:20
  • @Douma Linux Ubuntu, and PHP Version 8.0.11, Zip version 1.19.4, Libzip 1.5.2. – Lukáš Mesároš Nov 23 '21 at 19:28
  • @vee I have test.txt, packed size: 2, encrypted: -, CRC: 0F149D8B, Method: Store, – Lukáš Mesároš Nov 23 '21 at 19:32
  • @LukášMesároš This is strange. It must be one of PHP library or external library. I'm using PHP zip 1.19.3, Libzip 1.7.1, BZIP2 compression: yes, XZ compression: yes, AES-128 encryption: yes, AES-192 encryption: yes, AES-256 encryption: yes, otherwise: no – vee Nov 23 '21 at 19:37
  • @vee When i used AES, method in properties file was "AES256". But PKWARE dont work – Lukáš Mesároš Nov 23 '21 at 19:39
  • I'm running libzip 1.8.0. Maybe you can try to update to a newer version. – Douma Nov 24 '21 at 07:38
  • Yes, i must update to a newer version of libzip. Thanks guys – Lukáš Mesároš Nov 24 '21 at 08:05