使用 C++ 保護 PowerPoint 文件

Microsoft PowerPoint 是一款功能強大且功能豐富的軟件,可讓您創建精美的幻燈片以用於會議和討論。您可能會發現自己的演示文稿包含您想要保護的機密信息。在這種情況下,使用密碼保護演示文稿文件可能會有所幫助。另一方面,如果您想要將演示文稿標記為最終的並且不希望其內容被修改,您可以對演示文稿文件進行數字簽名。只要簽名有效,您就可以確信演示文稿文件未被更改。鑑於此,本文將教您如何使用 C++ 使用密碼或數字簽名保護 PowerPoint 演示文稿。

用於保護 PowerPoint 文件的 C++ API

Aspose.Slides for C++ 是一個功能豐富的 C++ 庫,允許您創建、讀取和修改 PowerPoint 文件。此外,API 支持使用密碼和數字簽名保護 PowerPoint 文件。您可以通過 NuGet 安裝 API 或直接從 下載 部分下載。

PM> Install-Package Aspose.Slides.Cpp

使用密碼保護 PowerPoint 文件

以下是使用密碼保護 PowerPoint 演示文稿的步驟。

以下是使用 C++ 使用密碼保護 PowerPoint 演示文稿的示例代碼。

// 文件路徑
const String sourceFilePath = u"SourceDirectory\\SamplePresentation.pptx";
const String outputFilePath = u"OutputDirectory\\protected-presentation.pptx";

// 加載演示文稿文件
SharedPtr<Presentation> presentation = MakeObject<Presentation>(sourceFilePath);

// 使用密碼保護演示文稿
presentation->get_ProtectionManager()->Encrypt(u"password");

// 保存演示文稿
presentation->Save(outputFilePath, SaveFormat::Pptx);

使用數字簽名保護 PowerPoint 文件

以下是使用數字簽名保護 PowerPoint 演示文稿的步驟。

以下是使用 C++ 保護帶有數字簽名的 PowerPoint 文件的示例代碼。

// 文件路徑
const String sourceFilePath = u"SourceDirectory\\SamplePresentation.pptx";
const String signatureFilePath = u"SourceDirectory\\testsignature1.pfx";
const String outputFilePath = u"OutputDirectory\\digital-signature-presentation.pptx";

// 加載演示文稿文件
SharedPtr<Presentation> presentation = MakeObject<Presentation>(sourceFilePath);

// 使用 PFX 文件和密碼創建 DigitalSignature 對象
SharedPtr<DigitalSignature> signature = MakeObject<DigitalSignature>(signatureFilePath, u"testpass1");

// 添加評論
signature->set_Comments(u"Test Comments");

// 向演示文稿添加數字簽名
presentation->get_DigitalSignatures()->Add(signature);

// 保存演示文稿
presentation->Save(outputFilePath, SaveFormat::Pptx);

使用 C++ 驗證數字簽名的 PowerPoint 文件

Aspose.Slides for C++ API 還為您提供了驗證數字簽名的 PowerPoint 文件的能力。以下是驗證 PowerPoint 文件數字簽名的步驟。

以下是使用 C++ 驗證 PowerPoint 文件數字簽名的示例代碼。

// 文件路徑
const String sourceFilePath = u"SourceDirectory\\digital-signature-presentation.pptx";

// 加載演示文稿文件
SharedPtr<Presentation> presentation = MakeObject<Presentation>(sourceFilePath);

// 檢查演示文稿是否有數字簽名
if (presentation->get_DigitalSignatures()->get_Count() > 0)
{
	bool allSignaturesAreValid = true;

	Console::WriteLine(u"Signatures used to sign the presentation: ");

	// 驗證數字簽名
	for (int i = 0; i < presentation->get_DigitalSignatures()->get_Count(); i++)
	{
		SharedPtr<IDigitalSignature> signature = presentation->get_DigitalSignatures()->idx_get(i);

		Console::WriteLine(System::Convert::ToString(signature->get_SignTime()) + u" -- " + (signature->get_IsValid() ? u"VALID" : u"INVALID"));
		if (signature->get_IsValid() == false)
		{
			allSignaturesAreValid = false;
		}
	}

	if (allSignaturesAreValid)
	{
		Console::WriteLine(u"Presentation is genuine, all signatures are valid.");
	}
	else
	{
		Console::WriteLine(u"Presentation has been modified since signing.");
	}
}

獲得免費許可證

您可以通過申請 免費的臨時許可證 來試用沒有評估限制的 API。

結論

在本文中,您了解瞭如何使用 C++ 使用密碼和數字簽名來保護 PowerPoint 演示文稿。此外,您還了解瞭如何通過驗證其數字簽名來驗證演示文件的完整性。 Aspose.Slides for C++ API 具有更多功能,使處理演示文稿文件變得輕而易舉。您可以使用 官方文檔 詳細探索 API。如有任何疑問,請隨時通過 免費支持論壇 與我們聯繫。

也可以看看