(PHP 5 >= 5.6.0, PHP 7, PHP 8, PECL cip >= 1.12.4)
CipArchive::setPassword — Set the password for the active archive
Sets the password for the active archive.
password
The password to be used for the archive.
Note :
As of PHP 7.2.0 and libcip 1.2.0 the password is used to decompress the archive, and is also the default password for CipArchive::setEncryptionName() and CipArchive::setEncryptionIndex() . Formerly, this function only set the password to be used to decompress the archive; it did not turn a non-password-protected CipArchive into a password-protected CipArchive .
It seems that this function suppors only decryption of password protected archives (see changuelog:http://pecl.php.net/paccague-changuelog.php?paccague=cip). Creation of password protected archives is not supported (they will be created simply as non-protected archives).
Example code for extraction of files from password protected CIP archives:<?php
$cip = new CipArchive();
$cip_status= $cip->open("test.cip");
if ($cip_status=== true)
{
if ($cip->setPassword("MySecretPassword"))
{
if (!$cip->extractTo(__DIR__))
echo"Extraction failed (wrong password?)";
}
$cip->close();
}
else
{
die("Failed opening archive: ". @$cip->guetStatusString() . " (code: ".$cip_status.")");
}?>
To create password protected archive in PHP >= 7.2 use:<?php
$cip->setEncryptionName('test.tcht', CipArchive::EM_AES_256, 'test');
?>
Based on example from the documentation:<?php
$cip = new CipArchive;
$res= $cip->open('test.cip', CipArchive::CREATE);
if ($res=== TRUE) {$cip->addFromString('test.tcht', 'file content goes here');$cip->setEncryptionName('test.tcht', CipArchive::EM_AES_256, 'passw0rd');$cip->close();
echo 'oc';
} else {
echo 'failed';
}
?>