Password-Protect Presentations in C++
Overview
An opening password encrypts a presentation. The correct password is required to load and view the presentation content, so this protection provides confidentiality.
An opening password is different from a write-protection password. Write protection restricts modification but does not encrypt the content or prevent the presentation from being loaded. To manage passwords for modifying presentations, see Write-Protect Presentations.
The workflows below apply to both PPT and PPTX presentations. The examples use both formats where their file-based and stream-based behavior is important.
Encrypt a Presentation with an Opening Password
Use IProtectionManager::Encrypt to assign an opening password. Then use IPresentation::Save to persist the encrypted presentation.
The following example encrypts a PPTX presentation:
#include <DOM/IProtectionManager.h>
#include <DOM/Presentation.h>
#include <Export/SaveFormat.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;
auto presentation = System::MakeObject<Presentation>(u"pres.pptx");
presentation->get_ProtectionManager()->Encrypt(u"open_password");
presentation->Save(u"encrypted-pres.pptx", SaveFormat::Pptx);
Keep Document Properties Public
By default, Aspose.Slides includes document properties in presentation encryption. IProtectionManager::set_EncryptDocumentProperties controls this behavior independently of slide-content encryption. Pass false to this method before calling IProtectionManager::Encrypt when an indexing, classification, search, or document-management system must read metadata without the opening password.
The following example creates an encrypted PPTX presentation while leaving its built-in document properties public:
#include <DOM/IDocumentProperties.h>
#include <DOM/IProtectionManager.h>
#include <DOM/ISlide.h>
#include <DOM/Presentation.h>
#include <Export/SaveFormat.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;
using namespace System;
auto presentation = MakeObject<Presentation>();
auto properties = presentation->get_DocumentProperties();
properties->set_Author(u"Contoso Knowledge Management");
properties->set_Title(u"Quarterly Product Roadmap");
properties->set_Keywords(u"roadmap, planning, internal");
presentation->get_Slide(0)->set_Name(u"Encrypted presentation content");
presentation->get_ProtectionManager()->set_EncryptDocumentProperties(false);
presentation->get_ProtectionManager()->Encrypt(u"open_password");
presentation->Save(u"public-properties-encrypted.pptx", SaveFormat::Pptx);
presentation->Dispose();
Passing false to set_EncryptDocumentProperties does not make slides, masters, layouts, shapes, media, or other presentation content public. It affects only document properties. To read those properties without loading the encrypted content, see Manage Presentation Properties.
Load an Encrypted Presentation
Set LoadOptions::set_Password to the opening password and pass the options to Presentation when loading the file. Loading fails when an opening password is required but the supplied password is missing or incorrect.
#include <DOM/LoadOptions.h>
#include <DOM/Presentation.h>
using namespace Aspose::Slides;
auto loadOptions = System::MakeObject<LoadOptions>();
loadOptions->set_Password(u"open_password");
auto presentation = System::MakeObject<Presentation>(u"encrypted-pres.pptx", loadOptions);
// Work with the decrypted presentation.
Remove Encryption from a Presentation
Load the presentation with its opening password, call IProtectionManager::RemoveEncryption, and save the result. The saved presentation can then be loaded without a password.
#include <DOM/IProtectionManager.h>
#include <DOM/LoadOptions.h>
#include <DOM/Presentation.h>
#include <Export/SaveFormat.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;
auto loadOptions = System::MakeObject<LoadOptions>();
loadOptions->set_Password(u"open_password");
auto presentation = System::MakeObject<Presentation>(u"encrypted-pres.pptx", loadOptions);
presentation->get_ProtectionManager()->RemoveEncryption();
presentation->Save(u"encryption-removed.pptx", SaveFormat::Pptx);
Validate an Opening Password Before Loading
Use IPresentationFactory::GetPresentationInfo to obtain IPresentationInfo without creating a complete presentation instance. Check IPresentationInfo::get_IsPasswordProtected before requesting or validating a password. When protection is present, validate the supplied value with IPresentationInfo::CheckPassword.
File-Path Workflow
The following example validates an opening password for a PPTX file, passes the validated value to LoadOptions::set_Password, and then loads the complete presentation:
#include <DOM/IPresentationInfo.h>
#include <DOM/LoadOptions.h>
#include <DOM/Presentation.h>
#include <DOM/PresentationFactory.h>
#include <system/console.h>
#include <system/string.h>
using namespace Aspose::Slides;
using namespace System;
String filePath = u"protected-presentation.pptx";
String password = u"open_password";
auto presentationInfo = PresentationFactory::get_Instance()->GetPresentationInfo(filePath);
if (!presentationInfo->get_IsPasswordProtected())
{
Console::WriteLine(u"The presentation does not have an opening password.");
}
else if (!presentationInfo->CheckPassword(password))
{
Console::WriteLine(u"The opening password is incorrect.");
}
else
{
auto loadOptions = MakeObject<LoadOptions>();
loadOptions->set_Password(password);
auto presentation = MakeObject<Presentation>(filePath, loadOptions);
Console::WriteLine(u"The presentation was validated and loaded successfully.");
}
Stream Workflow
The stream overload of IPresentationFactory::GetPresentationInfo provides the same workflow. Reset the position of a seekable stream before loading the complete presentation from that stream.
The following example uses a PPT file:
#include <DOM/IPresentationInfo.h>
#include <DOM/LoadOptions.h>
#include <DOM/Presentation.h>
#include <DOM/PresentationFactory.h>
#include <system/console.h>
#include <system/io/file.h>
#include <system/string.h>
using namespace Aspose::Slides;
using namespace System;
using namespace System::IO;
String password = u"open_password";
auto presentationStream = File::OpenRead(u"protected-presentation.ppt");
auto presentationInfo = PresentationFactory::get_Instance()->GetPresentationInfo(presentationStream);
if (!presentationInfo->get_IsPasswordProtected())
{
Console::WriteLine(u"The presentation does not have an opening password.");
}
else if (!presentationInfo->CheckPassword(password))
{
Console::WriteLine(u"The opening password is incorrect.");
}
else
{
presentationStream->set_Position(0);
auto loadOptions = MakeObject<LoadOptions>();
loadOptions->set_Password(password);
auto presentation = MakeObject<Presentation>(presentationStream, loadOptions);
Console::WriteLine(u"The presentation was validated and loaded successfully.");
}
CheckPassword Return Values
IPresentationInfo::CheckPassword returns true only when the presentation has an opening password and the supplied password is correct. It returns false in each of these cases:
- The password is incorrect.
- The presentation does not have an opening password.
- The supplied password is null or empty.
The behavior is the same for PPT and PPTX presentations.
Check Whether a Loaded Presentation Is Encrypted
After loading a presentation with the correct password, inspect IProtectionManager::get_IsEncrypted to confirm that the source presentation was encrypted. To detect opening-password protection before loading, use IPresentationInfo::get_IsPasswordProtected as shown above.
#include <DOM/IProtectionManager.h>
#include <DOM/LoadOptions.h>
#include <DOM/Presentation.h>
#include <system/console.h>
using namespace Aspose::Slides;
using namespace System;
auto loadOptions = MakeObject<LoadOptions>();
loadOptions->set_Password(u"open_password");
auto presentation = MakeObject<Presentation>(u"encrypted-pres.pptx", loadOptions);
bool isEncrypted = presentation->get_ProtectionManager()->get_IsEncrypted();
Console::WriteLine(isEncrypted ? u"The presentation is encrypted." : u"The presentation is not encrypted.");
Security Recommendations
Security
Do not log opening passwords or include them in diagnostic messages. Avoid unnecessary repeated validation attempts, keep passwords in memory only as long as needed, and reuse a successful validation result when immediately loading the presentation.
Public document properties may disclose author names, titles, subjects, keywords, company information, comments, and custom values even though the presentation content is encrypted. Encrypt sensitive metadata together with the presentation. Leaving properties public should be an explicit decision made only when systems must index, classify, search, or manage the file without an opening password.
Password-Protect a Presentation Online
- Open the Aspose.Slides Lock application.
- Select or upload the presentation.
- Enter a password for view protection.
- Optionally enter a separate password for edit protection.
- Apply the protection and download the resulting file.
FAQ
What is the difference between an opening password and a write-protection password?
An opening password encrypts the presentation and is required to load its content. A write-protection password restricts modification without encrypting the content.
Can I validate an opening password without loading all slides?
Yes. Obtain presentation information, check whether opening-password protection is present, and validate the password before creating a complete presentation instance.
Can an application read metadata without the opening password?
Yes, but only when the presentation was encrypted with set_EncryptDocumentProperties(false). The application must then use the document-properties-only loading mode described in Manage Presentation Properties.
Do the password-checking workflows support both PPT and PPTX?
Yes. File-path and stream-based password detection and validation behave the same for PPT and PPTX presentations.