/** * Enables encryption for the document. * * @param string $userPassword * @param string|null $ownerPassword * @param bool $use128bit */ public function enableEncryption($userPassword, $ownerPassword = null, $use128bit = true) { if (null === $ownerPassword) { $ownerPassword = $userPassword; } if ($use128bit) { $algorithm = 2; $revision = 3; $keyLength = 128 / 8; } else { $algorithm = 1; $revision = 2; $keyLength = 40 / 8; } $permissions = -1; $ownerEntry = EncryptionUtils::computeOwnerEntry($ownerPassword, $userPassword, $revision, $keyLength); if (2 === $revision) { list($userEntry, $key) = EncryptionUtils::computeUserEntryRev2($userPassword, $ownerEntry, $revision, $this->firstId->getValue()); } else { list($userEntry, $key) = EncryptionUtils::computeUserEntryRev3OrGreater($userPassword, $revision, $keyLength, $ownerEntry, $permissions, $this->firstId->getValue()); } $encrypt = new DictionaryObject(); $encrypt['Filter'] = new NameObject('Standard'); $encrypt['V'] = new NumericObject($algorithm); if (2 === $algorithm) { $encrypt['Length'] = new NumericObject($keyLength * 8); } $encrypt['R'] = new NumericObject($revision); $encrypt['O'] = new HexadecimalStringObject($ownerEntry); $encrypt['U'] = new HexadecimalStringObject($userEntry); $encrypt['P'] = new NumericObject($permissions); $this->encrypt = $this->objects->addObject($encrypt); $this->encryptionKey = $key; }