Encrypt and decrypt PDF files in C++
Contents
[
Hide
]
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
- Open the input PDF as a Document.
- Forbid all operations with DocumentPrivilege::get_ForbidAll().
- Enable screen reader access on the DocumentPrivilege object.
- Call Encrypt() with user and owner passwords, the privileges, and CryptoAlgorithm set to AESx128.
- 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
- Open the input PDF as a Document.
- Allow all operations with DocumentPrivilege::get_AllowAll().
- Call Encrypt() with user and owner passwords, the privileges, and CryptoAlgorithm set to RC4x128.
- 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
- Open the input PDF as a Document using its user password.
- Remove encryption with Decrypt().
- 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
- Open the input PDF as a Document using the current owner password.
- Call ChangePasswords() with the current owner password, the new user password, and the new owner password.
- 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
- Check whether the file is encrypted with PdfFileInfo::get_IsEncrypted().
- Create a list of candidate passwords.
- For each candidate, open a Document inside the try block.
- Read the page count with PageCollection::get_Count().
- Report the candidate as correct if the document contains at least one page.
- 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;
}
}
}