Encrypt and decrypt PDF files in C++

Manage PDF encryption and passwords through the Document API.

A user password is used to open a protected document. An owner password controls access to restricted operations. The sample password constants below illustrate API usage; supply your own password values for your documents.

Encrypt a PDF with restricted permissions

  1. Open the input PDF as a Document.
  2. Forbid all operations with DocumentPrivilege::get_ForbidAll().
  3. Enable screen reader access on the DocumentPrivilege object.
  4. Call Encrypt() with user and owner passwords, the privileges, and CryptoAlgorithm set to AESx128.
  5. Save the result with Document::Save().
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Security/CryptoAlgorithm.h>
#include <Aspose.PDF.Cpp/Facades/DocumentPrivilege.h>
#include <Aspose.PDF.Cpp/Facades/PdfFileInfo.h>

constexpr auto USER_PASSWORD = u"userpassword";
constexpr auto OWNER_PASSWORD = u"ownerpassword";

using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Facades;

void EncryptPassword(System::String path_infile, System::String path_outfile)
{
    const auto document = MakeObject<Document>(path_infile);

    auto documentPrivilege = DocumentPrivilege::get_ForbidAll();
    documentPrivilege->set_AllowScreenReaders(true);

    document->Encrypt(
        USER_PASSWORD,
        OWNER_PASSWORD,
        documentPrivilege,
        CryptoAlgorithm::AESx128,
        false);
    document->Save(path_outfile);
}

Encrypt a PDF with all permissions allowed

  1. Open the input PDF as a Document.
  2. Allow all operations with DocumentPrivilege::get_AllowAll().
  3. Call Encrypt() with user and owner passwords, the privileges, and CryptoAlgorithm set to RC4x128.
  4. Save the result with Document::Save().
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Security/CryptoAlgorithm.h>
#include <Aspose.PDF.Cpp/Facades/DocumentPrivilege.h>
#include <Aspose.PDF.Cpp/Facades/PdfFileInfo.h>

constexpr auto USER_PASSWORD = u"userpassword";
constexpr auto OWNER_PASSWORD = u"ownerpassword";

using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Facades;

void EncryptPdfFile(String path_infile, String path_outfile) {
    const auto document = MakeObject<Document>(path_infile);
    document->Encrypt(
        USER_PASSWORD,
        OWNER_PASSWORD,
        DocumentPrivilege::get_AllowAll(),
        CryptoAlgorithm::RC4x128,
        false);
    document->Save(path_outfile);
}

Decrypt a PDF

  1. Open the input PDF as a Document using its user password.
  2. Remove encryption with Decrypt().
  3. Save the unencrypted document with Document::Save().
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Security/CryptoAlgorithm.h>
#include <Aspose.PDF.Cpp/Facades/DocumentPrivilege.h>
#include <Aspose.PDF.Cpp/Facades/PdfFileInfo.h>

constexpr auto USER_PASSWORD = u"userpassword";
constexpr auto OWNER_PASSWORD = u"ownerpassword";

using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Facades;

void DecryptPdfFile(String path_infile, String path_outfile) {
    const auto document = MakeObject<Document>(path_infile, USER_PASSWORD);
    document->Decrypt();
    document->Save(path_outfile);
}

Change user and owner passwords

  1. Open the input PDF as a Document using the current owner password.
  2. Call ChangePasswords() with the current owner password, the new user password, and the new owner password.
  3. Save the result with Document::Save().
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Security/CryptoAlgorithm.h>
#include <Aspose.PDF.Cpp/Facades/DocumentPrivilege.h>
#include <Aspose.PDF.Cpp/Facades/PdfFileInfo.h>

constexpr auto USER_PASSWORD = u"userpassword";
constexpr auto OWNER_PASSWORD = u"ownerpassword";

using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Facades;

void ChangePassword(String path_infile, String path_outfile) {
    const auto document = MakeObject<Document>(path_infile, OWNER_PASSWORD);
    document->ChangePasswords(OWNER_PASSWORD, u"newuser", u"newowner");
    document->Save(path_outfile);

}

Check encryption and test candidate passwords

  1. Check whether the file is encrypted with PdfFileInfo::get_IsEncrypted().
  2. Create a list of candidate passwords.
  3. For each candidate, open a Document inside the try block.
  4. Read the page count with PageCollection::get_Count().
  5. Report the candidate as correct if the document contains at least one page.
  6. Catch InvalidPasswordException and report an incorrect candidate.
#include <iostream>

#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
#include <Aspose.PDF.Cpp/Security/CryptoAlgorithm.h>
#include <Aspose.PDF.Cpp/Facades/DocumentPrivilege.h>
#include <Aspose.PDF.Cpp/Facades/PdfFileInfo.h>
#include <Aspose.PDF.Cpp/Exceptions/InvalidPasswordException.h>

constexpr auto USER_PASSWORD = u"userpassword";
constexpr auto OWNER_PASSWORD = u"ownerpassword";

using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Facades;

void DetermineCorrectPasswordFromList(String path_infile) {
    const auto info = MakeObject<PdfFileInfo>(path_infile);

    std::cout << "File is password protected: " << info->get_IsEncrypted() << std::endl;

    auto passwords = MakeArray<String>({ u"test", u"test1", u"test2", u"test3", USER_PASSWORD });
    for (auto& password : passwords) {

        try {
            const auto document = MakeObject<Document>(path_infile, password);
            int pageCount = document->get_Pages()->get_Count();
            if (pageCount > 0) {
                std::cout << "Password '" << password << "' is correct. Pages: " << pageCount << std::endl;
            }
        }

        catch (InvalidPasswordException& ex) {
            std::cout << "Wrong password: " << password << std::endl;
        }
    }
}