Click or drag to resize

PDF Security: Passwords, Permissions and Encryption

The PDF security part of ExpertPdf Anywhere protects PDF documents with passwords and permissions, removes the protection of documents you have the password of, and signs documents and validates their signatures. It is in the ExpertPdf.Anywhere.PDFSecurity namespace of the ExpertPdf.Anywhere library and runs on every platform the library supports: Windows, Linux and macOS.

The main class is PdfSecurityManager; the protection to apply is described by a PdfSecurityOptions object given to its constructor. All of it is done in managed code and needs no native engine.

Features
  • A user password to open the document, and an owner password that gives all the rights.

  • Permissions: print, print in high quality, copy the content, copy for accessibility, edit the content, edit annotations, fill form fields, assemble the document.

  • Encryption with AES (128 or 256 bit keys), AES-256 in Galois/Counter mode (PDF 2.0) or RC4 (40 or 128 bit keys), with or without the XMP metadata.

  • Remove the security of a protected document, with its user or owner password.

  • Sign documents with a PKCS#12 (.pfx) certificate or with a key held outside the application (a certificate store, a PKCS#11 token, a signing service), with an optional time stamp and long-term validation data, or certify them (see Digital Signatures).

  • Validate the signatures of a document: integrity, signer, certificate trust, time stamp, long-term validation and, optionally, revocation.

  • Read the security settings of a document without changing it: encryption, permissions, passwords and signatures.

  • Work with files, streams and byte arrays.

Installation

The security classes are part of the ExpertPdf.Anywhere package; there is no separate package to install. See Installation for the packages and Requirements for the supported platforms.

dotnet add package ExpertPdf.Anywhere
Getting started

Set the passwords and the permissions in a PdfSecurityOptions object, create the security manager with it and save the protected document:

using ExpertPdf.Anywhere.PDFSecurity;

PdfSecurityOptions options = new PdfSecurityOptions();
options.UserPassword = "user";      // needed to open the document
options.OwnerPassword = "owner";    // opens the document with all the rights
options.CanPrint = false;
options.CanCopyContent = false;
options.KeySize = EncryptionKeySize.EncryptKey256Bit;
options.Algorithm = EncryptionAlgorithm.AES;

PdfSecurityManager securityManager = new PdfSecurityManager(options);

securityManager.SaveSecuredPdfToFile("report.pdf", "secured.pdf");
Passwords, permissions and encryption
  • With a user password only, the document needs that password to open and the same password is the owner password: whoever opens the document has all the rights.

  • With an owner password only, the document opens without a password and the permissions apply to every reader; the owner password is needed to change them.

  • With both passwords, the user password opens the document with the permissions you set, the owner password opens it with all the rights.

The permissions are CanPrint, CanFullQualityPrint, CanCopyContent, CanAccessibilityCopyContent, CanEditContent, CanEditAnnotations, CanFillFormFields and CanAssembleDocument; all are allowed by default. The default encryption is AES with a 128 bit key (Algorithm, KeySize). A 256 bit key gives AES-256 (security handler revision 6, ISO 32000-2), the choice for new documents; a 40 bit key always uses RC4, and RC4 is only for very old PDF viewers. AESGCM is AES-256 in Galois/Counter mode, whatever the key size; the document is written as PDF 2.0, which older readers cannot open. EncryptMetadata (true by default) set to false leaves the XMP metadata readable, for example by a search index.

// anyone can read the document; printing in low quality, form filling and
// accessibility only
PdfSecurityOptions options = new PdfSecurityOptions();
options.OwnerPassword = "owner";
options.CanFullQualityPrint = false;
options.CanCopyContent = false;
options.CanAccessibilityCopyContent = true;
options.CanEditContent = false;
options.CanEditAnnotations = false;
options.CanAssembleDocument = false;

// from memory to memory
byte[] securedBytes = new PdfSecurityManager(options).GetSecuredPdfBytes(
    File.ReadAllBytes("report.pdf"));
Removing the security

SaveUnSecuredPdfToFile(String, String, String) and GetUnSecuredPdfBytes(Byte, String) remove the passwords, the permissions and the encryption of a document. They need its user or owner password; a wrong password throws a SecurePdfException.

PdfSecurityManager securityManager = new PdfSecurityManager();
try
{
    securityManager.SaveUnSecuredPdfToFile("secured.pdf", "plain.pdf", "owner");
}
catch (SecurePdfException ex)
{
    Console.WriteLine("The document could not be opened: " + ex.Message);
}
Reading the security settings of a document

GetSecurityInfo(String) returns a PdfSecurityInfo with the security settings of a document, without changing it: whether it is encrypted, the algorithm, the key length and the revision of the security handler, the permissions, whether a password is needed to open it (IsUserPasswordRequired) and whether the owner password is needed for full rights (IsOwnerPasswordRequired), and its signatures.

The encryption settings and the permissions are read even from a document that has an open password. The signature information needs the document opened: pass its user or owner password to GetSecurityInfo(String, String) (IsOpened tells if the document could be opened). A wrong password throws a SecurePdfException.

PdfSecurityManager securityManager = new PdfSecurityManager();
PdfSecurityInfo info = securityManager.GetSecurityInfo("secured.pdf");

if (info.IsEncrypted)
    Console.WriteLine("{0} {1} bit, revision {2}", info.Algorithm, info.KeyLength,
        info.Revision);
Console.WriteLine("Open password: {0}, owner password needed: {1}",
    info.IsUserPasswordRequired, info.IsOwnerPasswordRequired);
Console.WriteLine("Print: {0}, copy: {1}, edit: {2}", info.CanPrint,
    info.CanCopyContent, info.CanEditContent);

// the signatures of a document with an open password
PdfSecurityInfo opened = securityManager.GetSecurityInfo("secured.pdf", "user");
Console.WriteLine("Signatures: {0}, certified: {1}", opened.SignatureCount,
    opened.IsCertified);
Samples

The samples download has these console applications, in C# and VB.NET, for Windows, Linux and macOS:

  • PdfSecurityCommandLine - a command line tool: passwords, every permission, key size and algorithm, removing the security, showing the security settings of a document.

  • PdfSecurityFeatures - an open password only, permissions without an open password, RC4 and AES with 40, 128 and 256 bit keys, files and memory, reading the security settings, removing the security with the owner or the user password.

  • PdfDigitalSignature - digital signatures from a file, from memory and from a stream with a password protected document, with optional time stamps; a certified document signed by an approver; the validation of all the signatures.

They work on the documents that come with them and need no arguments.

PDF Security command line tool

The PdfSecurityCommandLine sample is also a ready to use tool. The syntax is:

PdfSecurityCommandLine /pdf:source_file [/out:file]
    [/userpswd:password] [/ownerpswd:password] [/keysize:40|128|256] [/algo:RC4|AES]
    [/noprint] [/nofullqualityprint] [/nocopy] [/noaccessibilitycopy]
    [/noeditcontent] [/noeditannot] [/nofillform] [/noassembly]
    [/removesecurity] [/info]

With /removesecurity the owner or the user password of the source document is required. /info shows the security settings of the source document and changes nothing. Examples:

PdfSecurityCommandLine /pdf:report.pdf /noprint /keysize:256 /out:Secured.pdf
    /ownerpswd:owner /userpswd:user
PdfSecurityCommandLine /pdf:Secured.pdf /ownerpswd:owner /removesecurity
    /out:Unsecured.pdf
PdfSecurityCommandLine /pdf:Secured.pdf /info
Licensing

Set LicenseKey once when the application starts, or the LicenseKey property before you use the security manager. Without a license key the library adds an evaluation notice to the pages of the documents it secures, unsecures or signs, and of the documents it adds a signature field to. A document that is signed already gets no notice, because it would invalidate the signatures. Reading the security settings and validating the signatures is not limited. A license key that is not valid makes the next operation throw a LicenseException. See Licensing.

See Also