Table of contents
Namespace: Com\Tecnick\Pdf\Encrypt
Extends: Compute
File: Encrypt.php line 38
Encrypts data for PDF documents.
Tags
- since: 2008-01-02
- category: Library
- author: Nicola Asuni info@tecnick.com
- copyright: 2011-2026 Nicola Asuni - Tecnick.com LTD
- license: https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
- link: https://github.com/tecnickcom/tc-lib-pdf-encrypt
- phpstan-import-type: TEncryptData from Output
Constants
DEFAULTPERMS
File: Data.php line 63
public array<string|int, string> DEFAULTPERMS = ['print', 'modify', 'copy', 'annot-forms', 'fill-forms', 'extract', 'assemble', 'print-high']
Methods
__construct()
File: Encrypt.php line 69
public __construct([bool $enabled = false ][, string $file_id = '' ][, int $mode = 0 ][, array<string|int, string> $permissions = self::DEFAULTPERMS ][, string $user_pass = '' ][, string $owner_pass = '' ][, array<int, array{'c': string, 'p'?: string[]}>|null $pubkeys = null ][, bool $encryptMetadata = true ][, bool $encryptEmbeddedFiles = true ]) : mixed
Parameters
- $enabled : bool = false: False if the encryption is disabled.
- $file_id : string = ‘’: File ID as an even-length hexadecimal string; a random one is generated when empty. It is the first element of the trailer /ID array, from which revisions 2 to 4 derive the key.
- $mode : int = 0: Encryption strength: 0 = RC4-40 (deprecated); 1 = RC4-128 (deprecated); 2 = AES-128; 3 = AES-256 R5; 4 = AES-256 R6 (PDF 2.0 / ISO 32000-2).
- $permissions : array<string|int, string> = self::DEFAULTPERMS: The set of permissions to block: ‘owner’ (inverted logic: when set, permits change of encryption), ‘print’, ‘modify’, ‘copy’, ‘annot-forms’, ‘fill-forms’, ’extract’, ‘assemble’, ‘print-high’.
- $user_pass : string = ‘’: User password.
- $owner_pass : string = ‘’: Owner password. A random value is used when empty.
- $pubkeys : array<int, array{‘c’: string, ‘p’?: string[]}>|null = null: Recipients, each with a public-key certificate (‘c’) and the permissions to block for it (‘p’), for example: [[‘c’ => ‘file://cert.pem’, ‘p’ => [‘print’]]].
- $encryptMetadata : bool = true: When false, adds /EncryptMetadata false to the encryption dictionary and leaves metadata streams unencrypted. Requires mode 2, 3 or 4: for modes 0 and 1 the value is forced back to true with an E_USER_WARNING.
- $encryptEmbeddedFiles : bool = true: Selects the /EFF crypt filter for V 4 and V 5. True points /EFF at the same filter as the other streams; false writes /EFF /Identity, and the caller must then write embedded file streams without calling encryptString() on them.
Tags
- throws: Exception
__debugInfo()
File: Output.php line 131
public __debugInfo() : array<string, mixed>
Return values
array<string, mixed>
convertHexStringToString()
File: Encrypt.php line 336
public convertHexStringToString(string $bstr) : string
Parameters
- $bstr : string: Byte-string to convert.
Tags
- throws: Exception When the input is not an even-length hexadecimal string.
Return values
string
convertStringToHexString()
File: Encrypt.php line 359
public convertStringToHexString(string $str) : string
Parameters
- $str : string: String to convert.
Return values
string
encodeNameObject()
File: Encrypt.php line 372
public encodeNameObject(string $name) : string
Every character outside [0-9a-zA-Z_=-], including the NUMBER SIGN itself, is written as a two-digit #XX escape (ISO 32000-1 section 7.3.5).
Parameters
- $name : string: Name object to encode.
Return values
string
encrypt()
File: Compute.php line 51
public encrypt(int|string $type[, string $data = '' ][, string $key = '' ][, int $objnum = 0 ][, int $gennum = 0 ]) : string
Parameters
- $type : int|string: Encrypt type.
- $data : string = ‘’: Data string to encrypt.
- $key : string = ‘’: Encryption key.
- $objnum : int = 0: Object number.
- $gennum : int = 0: Object generation number.
Tags
- throws: Exception
Return values
string
encryptString()
File: Encrypt.php line 398
public encryptString(string $str[, int $objnum = 0 ][, int $gennum = 0 ]) : string
Parameters
- $str : string: String to encrypt.
- $objnum : int = 0: Object ID.
- $gennum : int = 0: Object generation number.
Tags
- throws: Exception
Return values
string
escapeDataString()
File: Encrypt.php line 412
public escapeDataString(string $str[, int $objnum = 0 ][, int $gennum = 0 ]) : string
Parameters
- $str : string: Data string to escape.
- $objnum : int = 0: Object ID.
- $gennum : int = 0: Object generation number.
Tags
- throws: Exception
Return values
string
escapeString()
File: Output.php line 146
public escapeString(string $str) : string
Parameters
- $str : string: String to escape.
Return values
string
getEncPermissionsString()
File: Compute.php line 239
public getEncPermissionsString(int $protection) : string
Parameters
- $protection : int: 32bit encryption permission value (P value).
Return values
string
getEncryptionData()
File: Encrypt.php line 313
public getEncryptionData() : TEncryptData
Return values
TEncryptData
getFileId()
File: Encrypt.php line 324
public getFileId() : string
The document must carry this value as the first element of the trailer /ID array: revisions 2 to 4 derive the encryption key from it.
Return values
string
getFormattedDate()
File: Encrypt.php line 430
public getFormattedDate([int $time = null ][, int $objnum = 0 ][, int $gennum = 0 ]) : string
The instant is rendered in UTC, not in the ambient date.timezone.
Parameters
- $time : int = null: UTC time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).
- $objnum : int = 0: Object ID.
- $gennum : int = 0: Object generation number.
Tags
- throws: Exception
Return values
string —
escaped date string.
getObjectKey()
File: Compute.php line 211
public getObjectKey(int $objnum[, int $gennum = 0 ]) : string
This is used for all strings and streams without crypt filter specifier.
Parameters
- $objnum : int: Object number.
- $gennum : int = 0: Object generation number.
Tags
- throws: Exception When either number is out of range.
Return values
string
getPdfEncryptionObj()
File: Output.php line 186
public getPdfEncryptionObj(int &$pon) : string
Parameters
- $pon : int: Current PDF object number
Tags
- throws: Exception When encryption is not enabled.
Return values
string
getPubKeyPermissionsString()
File: Compute.php line 253
public getPubKeyPermissionsString(int $protection) : string
The byte order is the opposite of the /P key material of getEncPermissionsString().
Parameters
- $protection : int: 32bit encryption permission value (P value).
Return values
string
getUserPermissionCode()
File: Compute.php line 285
public getUserPermissionCode(array<string|int, string> $permissions, int $mode) : int
The returned value is a signed 32-bit integer as required by Table 22: reserved bits 7, 8 and 13 to 32 are set, bits 1 and 2 are clear, and every granted permission bit is set.
Parameters
- $permissions : array<string|int, string>: The set of permissions (specify the ones you want to block).
- $mode : int: Encryption strength: 0 = RC4-40 (deprecated); 1 = RC4-128 (deprecated); 2 = AES-128; 3 = AES-256 R5; 4 = AES-256 R6 (PDF 2.0 / ISO 32000-2). Revision 2 defines fewer permission bits than the others.
Tags
- throws: Exception When a permission name is not recognised.
Return values
int