PoDoFo  1.0.0-dev
Public Member Functions | Static Public Member Functions | Protected Types | Protected Member Functions | Friends | List of all members
PoDoFo::PdfEncrypt Class Referenceabstract

A class that is used to encrypt a PDF file and set document permissions on the PDF file. More...

#include <PdfEncrypt.h>

Inherited by PoDoFo::PdfEncryptMD5Base.

Public Member Functions

void EnsureEncryptionInitialized (const PdfString &documentId, PdfEncryptContext &context)
 Ensure encryption key and /O, /U, /OE, /UE values are initialized. More...
 
void Authenticate (const std::string_view &password, const PdfString &documentId, PdfEncryptContext &context) const
 Tries to authenticate a user using either the user or owner password. More...
 
virtual void CreateEncryptionDictionary (PdfDictionary &dictionary) const =0
 Fill all keys into a encryption dictionary. More...
 
virtual std::unique_ptr< InputStreamCreateEncryptionInputStream (InputStream &inputStream, size_t inputLen, PdfEncryptContext &context, const PdfReference &objref) const =0
 Create an InputStream that decrypts all data read from it using the current settings of the PdfEncrypt object. More...
 
virtual std::unique_ptr< OutputStreamCreateEncryptionOutputStream (OutputStream &outputStream, PdfEncryptContext &context, const PdfReference &objref) const =0
 Create an OutputStream that encrypts all data written to it using the current settings of the PdfEncrypt object. More...
 
PdfEncryptionAlgorithm GetEncryptAlgorithm () const
 Get the encryption algorithm of this object. More...
 
bool IsOwnerPasswordSet () const
 Checks if an owner password is set. More...
 
bool IsPrintAllowed () const
 Checks if printing this document is allowed. More...
 
bool IsEditAllowed () const
 Checks if modifying this document (besides annotations, form fields or changing pages) is allowed. More...
 
bool IsCopyAllowed () const
 Checks if text and graphics extraction is allowed. More...
 
bool IsEditNotesAllowed () const
 Checks if it is allowed to add or modify annotations or form fields Every PDF consuming applications has to adhere this value! More...
 
bool IsFillAndSignAllowed () const
 Checks if it is allowed to fill in existing form or signature fields Every PDF consuming applications has to adhere this value! More...
 
bool IsAccessibilityAllowed () const
 Checks if it is allowed to extract text and graphics to support users with disabilities Every PDF consuming applications has to adhere this value! More...
 
bool IsDocAssemblyAllowed () const
 Checks if it is allowed to insert, create, rotate, delete pages or add bookmarks Every PDF consuming applications has to adhere this value! More...
 
bool IsHighPrintAllowed () const
 Checks if it is allowed to print a high quality version of this document Every PDF consuming applications has to adhere this value! More...
 
void EncryptTo (charbuff &out, const bufferview &view, PdfEncryptContext &context, const PdfReference &objref) const
 Encrypt a character span.
 
void DecryptTo (charbuff &out, const bufferview &view, PdfEncryptContext &context, const PdfReference &objref) const
 Decrypt a character span.
 
virtual size_t CalculateStreamLength (size_t length) const =0
 Calculate stream size.
 
virtual size_t CalculateStreamOffset () const =0
 Calculate stream offset.
 
unsigned GetKeyLengthBytes () const
 Get the encryption key length in bytes. More...
 
bufferview GetUValue () const
 Get the U object value (user)
 
bufferview GetOValue () const
 Get the O object value (owner)
 
PdfKeyLength GetKeyLength () const
 Get the length of the encryption key in bits.
 
PdfPermissions GetPValue () const
 Get the P object value (protection)
 
unsigned GetRevision () const
 Get the revision number of the encryption method.
 
bool IsMetadataEncrypted () const
 
bool IsParsed () const
 

Static Public Member Functions

static std::unique_ptr< PdfEncryptCreate (const std::string_view &userPassword, const std::string_view &ownerPassword, PdfPermissions protection=PdfPermissions::Default, PdfEncryptionAlgorithm algorithm=PdfEncryptionAlgorithm::AESV2, PdfKeyLength keyLength=PdfKeyLength::Unknown)
 Create a PdfEncrypt object which can be used to encrypt a PDF file. More...
 
static std::unique_ptr< PdfEncryptCreateFromObject (const PdfObject &obj)
 Initialize a PdfEncrypt object from an encryption dictionary in a PDF file. More...
 
static PdfEncryptionAlgorithm GetEnabledEncryptionAlgorithms ()
 Retrieve the list of encryption algorithms that are used when loading a PDF document. More...
 
static bool IsEncryptionEnabled (PdfEncryptionAlgorithm algorithm)
 Test if a certain encryption algorithm is enabled for loading PDF documents. More...
 

Protected Types

enum class  PdfRC4Revision { R2 = 2 , R3 = 3 }
 

Protected Member Functions

const unsigned char * GetUValueRaw () const
 
const unsigned char * GetOValueRaw () const
 
const std::string & GetUserPassword () const
 
const std::string & GetOwnerPassword () const
 
int64_t GetPValueForSerialization () const
 
void InitFromValues (PdfEncryptionAlgorithm algorithm, PdfKeyLength keyLength, unsigned char revision, PdfPermissions pValue, const bufferview &uValue, const bufferview &oValue, bool encryptedMetadata)
 Init from parsed values.
 
void InitFromScratch (const std::string_view &userPassword, const std::string_view &ownerPassword, PdfEncryptionAlgorithm algorithm, PdfKeyLength keyLength, unsigned char revision, PdfPermissions pValue, bool encryptedMetadata)
 Init from scratch with user/owner password. More...
 
virtual void Decrypt (const char *inStr, size_t inLen, PdfEncryptContext &context, const PdfReference &objref, char *outStr, size_t &outLen) const =0
 
virtual void Encrypt (const char *inStr, size_t inLen, PdfEncryptContext &context, const PdfReference &objref, char *outStr, size_t outLen) const =0
 
virtual PdfAuthResult Authenticate (const std::string_view &password, const std::string_view &documentId, PODOFO_CRYPT_CTX *ctx, unsigned char encryptionKey[32]) const =0
 
virtual void GenerateEncryptionKey (const std::string_view &documentId, PdfAuthResult authResult, PODOFO_CRYPT_CTX *ctx, unsigned char uValue[48], unsigned char oValue[48], unsigned char encryptionKey[32])=0
 
bool CheckKey (const unsigned char key1[32], const unsigned char key2[32]) const
 

Friends

class PdfEncryptMD5Base
 
class PdfEncryptAESV3
 

Detailed Description

A class that is used to encrypt a PDF file and set document permissions on the PDF file.

As a user of this class, you have only to instantiate a object of this class and pass it to PdfWriter, PdfMemDocument, PdfStreamedDocument or PdfImmediateWriter. You do not have to call any other method of this class. The above classes know how to handle encryption using PdfEncrypt.

Member Function Documentation

◆ Authenticate()

void PoDoFo::PdfEncrypt::Authenticate ( const std::string_view &  password,
const PdfString documentId,
PdfEncryptContext &  context 
) const

Tries to authenticate a user using either the user or owner password.

Parameters
passwordowner or user password
documentIdthe documentId of the PDF file
Returns
true if either the owner or user password matches password

◆ Create()

unique_ptr< PdfEncrypt > PdfEncrypt::Create ( const std::string_view &  userPassword,
const std::string_view &  ownerPassword,
PdfPermissions  protection = PdfPermissions::Default,
PdfEncryptionAlgorithm  algorithm = PdfEncryptionAlgorithm::AESV2,
PdfKeyLength  keyLength = PdfKeyLength::Unknown 
)
static

Create a PdfEncrypt object which can be used to encrypt a PDF file.

Parameters
userPasswordthe user password (if empty the user does not have to enter a password to open the document)
ownerPasswordthe owner password
protectionseveral PdfPermissions values or'ed together to set the users permissions for this document
algorithmthe revision of the encryption algorithm to be used
keyLengththe length of the encryption key ranging from 40 to 128 bits (only used if algorithm == PdfEncryptAlgorithm::RC4V2)
See also
EnsureEncryptionInitialized with the documentID to generate the real encryption key using this information

◆ CreateEncryptionDictionary()

virtual void PoDoFo::PdfEncrypt::CreateEncryptionDictionary ( PdfDictionary dictionary) const
pure virtual

Fill all keys into a encryption dictionary.

This dictionary is usually added to the PDF files trailer under the /Encryption key.

Parameters
dictionaryan empty dictionary which is filled with information about the used encryption algorithm

◆ CreateEncryptionInputStream()

virtual std::unique_ptr<InputStream> PoDoFo::PdfEncrypt::CreateEncryptionInputStream ( InputStream inputStream,
size_t  inputLen,
PdfEncryptContext &  context,
const PdfReference objref 
) const
pure virtual

Create an InputStream that decrypts all data read from it using the current settings of the PdfEncrypt object.

Warning: Currently only RC4 based encryption is supported using output streams!

Parameters
inputStreamthe created InputStream reads all decrypted data to this input stream.
Returns
an InputStream that decrypts all data.

Implemented in PoDoFo::PdfEncryptRC4, and PoDoFo::PdfEncryptAESV2.

◆ CreateEncryptionOutputStream()

virtual std::unique_ptr<OutputStream> PoDoFo::PdfEncrypt::CreateEncryptionOutputStream ( OutputStream outputStream,
PdfEncryptContext &  context,
const PdfReference objref 
) const
pure virtual

Create an OutputStream that encrypts all data written to it using the current settings of the PdfEncrypt object.

Warning: Currently only RC4 based encryption is supported using output streams!

Parameters
outputStreamthe created OutputStream writes all encrypted data to this output stream.
Returns
a OutputStream that encrypts all data.

Implemented in PoDoFo::PdfEncryptRC4, and PoDoFo::PdfEncryptAESV2.

◆ CreateFromObject()

unique_ptr< PdfEncrypt > PdfEncrypt::CreateFromObject ( const PdfObject obj)
static

Initialize a PdfEncrypt object from an encryption dictionary in a PDF file.

This is required for encrypting a PDF file, but handled internally in PdfParser for you.

Will use only encrypting algorithms that are enabled.

Parameters
obja PDF encryption dictionary
See also
GetEnabledEncryptionAlgorithms

◆ EnsureEncryptionInitialized()

void PdfEncrypt::EnsureEncryptionInitialized ( const PdfString documentId,
PdfEncryptContext &  context 
)

Ensure encryption key and /O, /U, /OE, /UE values are initialized.

Parameters
documentIdthe documentId of the current document

◆ GetEnabledEncryptionAlgorithms()

PdfEncryptionAlgorithm PdfEncrypt::GetEnabledEncryptionAlgorithms ( )
static

Retrieve the list of encryption algorithms that are used when loading a PDF document.

By default all algorithms are enabled.

See also
IsEncryptionEnabled
SetEnabledEncryptionAlgorithms
Returns
an or'ed together list of all enabled encryption algorithms

◆ GetEncryptAlgorithm()

PdfEncryptionAlgorithm PoDoFo::PdfEncrypt::GetEncryptAlgorithm ( ) const
inline

Get the encryption algorithm of this object.

Returns
the PdfEncryptAlgorithm of this object

◆ GetKeyLengthBytes()

unsigned PdfEncrypt::GetKeyLengthBytes ( ) const

Get the encryption key length in bytes.

Remarks
The maximum is 32 bytes

◆ InitFromScratch()

void PdfEncrypt::InitFromScratch ( const std::string_view &  userPassword,
const std::string_view &  ownerPassword,
PdfEncryptionAlgorithm  algorithm,
PdfKeyLength  keyLength,
unsigned char  revision,
PdfPermissions  pValue,
bool  encryptedMetadata 
)
protected

Init from scratch with user/owner password.

Requires full initialization with EnsureEncryptionInitialized

◆ IsAccessibilityAllowed()

bool PdfEncrypt::IsAccessibilityAllowed ( ) const

Checks if it is allowed to extract text and graphics to support users with disabilities Every PDF consuming applications has to adhere this value!

Returns
true if you are allowed to extract text and graphics to support users with disabilities
See also
PdfEncrypt to set own document permissions.

◆ IsCopyAllowed()

bool PdfEncrypt::IsCopyAllowed ( ) const

Checks if text and graphics extraction is allowed.

Every PDF consuming applications has to adhere this value!

Returns
true if you are allowed to extract text and graphics from this document
See also
PdfEncrypt to set own document permissions.

◆ IsDocAssemblyAllowed()

bool PdfEncrypt::IsDocAssemblyAllowed ( ) const

Checks if it is allowed to insert, create, rotate, delete pages or add bookmarks Every PDF consuming applications has to adhere this value!

Returns
true if you are allowed to insert, create, rotate, delete pages or add bookmarks
See also
PdfEncrypt to set own document permissions.

◆ IsEditAllowed()

bool PdfEncrypt::IsEditAllowed ( ) const

Checks if modifying this document (besides annotations, form fields or changing pages) is allowed.

Every PDF consuming applications has to adhere this value!

Returns
true if you are allowed to modify this document
See also
PdfEncrypt to set own document permissions.

◆ IsEditNotesAllowed()

bool PdfEncrypt::IsEditNotesAllowed ( ) const

Checks if it is allowed to add or modify annotations or form fields Every PDF consuming applications has to adhere this value!

Returns
true if you are allowed to add or modify annotations or form fields
See also
PdfEncrypt to set own document permissions.

◆ IsEncryptionEnabled()

bool PdfEncrypt::IsEncryptionEnabled ( PdfEncryptionAlgorithm  algorithm)
static

Test if a certain encryption algorithm is enabled for loading PDF documents.

Returns
true if the encryption algorithm is enabled
See also
GetEnabledEncryptionAlgorithms
SetEnabledEncryptionAlgorithms

◆ IsFillAndSignAllowed()

bool PdfEncrypt::IsFillAndSignAllowed ( ) const

Checks if it is allowed to fill in existing form or signature fields Every PDF consuming applications has to adhere this value!

Returns
true if you are allowed to fill in existing form or signature fields
See also
PdfEncrypt to set own document permissions.

◆ IsHighPrintAllowed()

bool PdfEncrypt::IsHighPrintAllowed ( ) const

Checks if it is allowed to print a high quality version of this document Every PDF consuming applications has to adhere this value!

Returns
true if you are allowed to print a high quality version of this document
See also
PdfEncrypt to set own document permissions.

◆ IsOwnerPasswordSet()

bool PdfEncrypt::IsOwnerPasswordSet ( ) const

Checks if an owner password is set.

An application reading PDF must adhere to permissions for printing, copying, etc., unless the owner password was used to open it.

Returns
true if the owner password is set, in which case IsPrintAllowed etc. should be ignored
See also
PdfEncrypt to set own document permissions.

◆ IsPrintAllowed()

bool PdfEncrypt::IsPrintAllowed ( ) const

Checks if printing this document is allowed.

Every PDF consuming applications has to adhere this value!

Returns
true if you are allowed to print this document
See also
PdfEncrypt to set own document permissions.

The documentation for this class was generated from the following files: