Bảo vệ bằng mật khẩu cho bản trình chiếu trong C++

Tổng quan

Mật khẩu mở đầu mã hoá một bản trình chiếu. Cần mật khẩu đúng để tải và xem nội dung bản trình chiếu, do đó bảo vệ này mang lại tính bảo mật.

Mật khẩu mở đầu khác với mật khẩu bảo vệ ghi. Bảo vệ ghi hạn chế việc chỉnh sửa nhưng không mã hoá nội dung hoặc ngăn bản trình chiếu được tải. Để quản lý mật khẩu cho việc sửa đổi bản trình chiếu, xem Write-Protect Presentations.

Các quy trình dưới đây áp dụng cho cả bản trình chiếu PPT và PPTX. Các ví dụ sử dụng cả hai định dạng khi hành vi dựa trên tệp và luồng của chúng quan trọng.

Mã hoá bản trình chiếu bằng mật khẩu mở đầu

Sử dụng IProtectionManager::Encrypt để chỉ định một mật khẩu mở đầu. Sau đó sử dụng IPresentation::Save để lưu bản trình chiếu đã được mã hoá.

Ví dụ sau mã hoá một bản PPTX:

#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);

Giữ các thuộc tính tài liệu công khai

Theo mặc định, Aspose.Slides bao gồm các thuộc tính tài liệu trong quá trình mã hoá bản trình chiếu. IProtectionManager::set_EncryptDocumentProperties kiểm soát hành vi này một cách độc lập với việc mã hoá nội dung slide. Truyền false cho phương thức này trước khi gọi IProtectionManager::Encrypt khi một hệ thống lập chỉ mục, phân loại, tìm kiếm hoặc quản lý tài liệu cần đọc siêu dữ liệu mà không cần mật khẩu mở đầu.

Ví dụ sau tạo một bản PPTX đã được mã hoá trong khi để lại các thuộc tính tài liệu tích hợp công khai:

#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();

Việc truyền false cho set_EncryptDocumentProperties không làm cho các slide, master, layout, shape, media hoặc nội dung bản trình chiếu khác trở thành công khai. Nó chỉ ảnh hưởng tới các thuộc tính tài liệu. Để đọc các thuộc tính này mà không tải nội dung đã mã hoá, xem Manage Presentation Properties.

Tải một bản trình chiếu đã mã hoá

Đặt LoadOptions::set_Password thành mật khẩu mở đầu và truyền các tùy chọn này cho Presentation khi tải tệp. Việc tải sẽ thất bại khi cần mật khẩu mở đầu nhưng mật khẩu đã cung cấp thiếu hoặc không đúng.

#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);

// Làm việc với bản trình chiếu đã giải mã.

Xóa mã hoá khỏi một bản trình chiếu

Tải bản trình chiếu với mật khẩu mở đầu, gọi IProtectionManager::RemoveEncryption, và lưu kết quả. Bản trình chiếu đã lưu sau đó có thể được tải mà không cần mật khẩu.

#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);

Xác thực mật khẩu mở đầu trước khi tải

Sử dụng IPresentationFactory::GetPresentationInfo để lấy IPresentationInfo mà không tạo một thể hiện đầy đủ của bản trình chiếu. Kiểm tra IPresentationInfo::get_IsPasswordProtected trước khi yêu cầu hoặc xác thực mật khẩu. Khi có bảo vệ, xác thực giá trị đã cung cấp bằng IPresentationInfo::CheckPassword.

Quy trình Đường dẫn Tệp

Ví dụ sau xác thực mật khẩu mở đầu cho một tệp PPTX, truyền giá trị đã xác thực vào LoadOptions::set_Password, rồi tải đầy đủ bản trình chiếu:

#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.");
}

Quy trình Luồng

Phiên bản quá tải luồng của IPresentationFactory::GetPresentationInfo cung cấp cùng quy trình. Đặt lại vị trí của luồng có thể tìm kiếm trước khi tải đầy đủ bản trình chiếu từ luồng đó.

Ví dụ sau sử dụng một tệp PPT:

#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.");
}

Giá trị trả về của CheckPassword

IPresentationInfo::CheckPassword trả về true chỉ khi bản trình chiếu có mật khẩu mở đầu và mật khẩu đã cung cấp đúng. Nó trả về false trong mỗi trường hợp sau:

  • Mật khẩu không đúng.
  • Bản trình chiếu không có mật khẩu mở đầu.
  • Mật khẩu đã cung cấp là null hoặc rỗng.

Hành vi này giống nhau cho các bản trình chiếu PPT và PPTX.

Kiểm tra liệu một bản trình chiếu đã tải có được mã hoá hay không

Sau khi tải một bản trình chiếu bằng mật khẩu đúng, kiểm tra IProtectionManager::get_IsEncrypted để xác nhận rằng bản trình chiếu nguồn đã được mã hoá. Để phát hiện bảo vệ mật khẩu mở đầu trước khi tải, sử dụng IPresentationInfo::get_IsPasswordProtected như đã trình bày ở trên.

#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.");

Khuyến nghị Bảo mật

Bảo vệ bản trình chiếu bằng mật khẩu trực tuyến

  1. Mở ứng dụng Aspose.Slides Lock.
  2. Chọn hoặc tải lên bản trình chiếu.
  3. Nhập mật khẩu để bảo vệ chế độ xem.
  4. Tùy chọn nhập một mật khẩu riêng cho bảo vệ chỉnh sửa.
  5. Áp dụng bảo vệ và tải xuống tệp kết quả.

Câu hỏi thường gặp

Sự khác nhau giữa mật khẩu mở đầu và mật khẩu bảo vệ ghi là gì?

Mật khẩu mở đầu mã hoá bản trình chiếu và cần thiết để tải nội dung của nó. Mật khẩu bảo vệ ghi hạn chế việc chỉnh sửa mà không mã hoá nội dung.

Tôi có thể xác thực mật khẩu mở đầu mà không tải toàn bộ các slide không?

Có. Lấy thông tin bản trình chiếu, kiểm tra xem có bảo vệ mật khẩu mở đầu không, và xác thực mật khẩu trước khi tạo một thể hiện đầy đủ của bản trình chiếu.

Một ứng dụng có thể đọc siêu dữ liệu mà không có mật khẩu mở đầu không?

Có, nhưng chỉ khi bản trình chiếu được mã hoá bằng set_EncryptDocumentProperties(false). Ứng dụng sau đó phải sử dụng chế độ chỉ tải thuộc tính tài liệu như mô tả trong Manage Presentation Properties.

Các quy trình kiểm tra mật khẩu có hỗ trợ cả PPT và PPTX không?

Có. Phát hiện và xác thực mật khẩu dựa trên đường dẫn tệp và luồng hoạt động giống nhau cho các bản trình chiếu PPT và PPTX.