在 Android 上對簡報進行密碼保護

概述

開啟密碼會加密簡報。必須提供正確的密碼才能載入並檢視簡報內容,因而此保護提供機密性。

開啟密碼與寫入保護密碼不同。寫入保護限制修改,但不加密內容,也不阻止載入簡報。若要管理修改簡報的密碼,請參閱Write-Protect Presentations

以下工作流程同時適用於 PPT 與 PPTX 簡報。範例會在需關注檔案模式與串流模式行為的情況下同時使用兩種格式。

使用開啟密碼加密簡報

使用IProtectionManager.encrypt指派開啟密碼。然後使用IPresentation.save將加密的簡報儲存下來。

以下範例加密 PPTX 簡報:

import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;

Presentation presentation = new Presentation("pres.pptx");
try {
    presentation.getProtectionManager().encrypt("open_password");
    presentation.save("encrypted-pres.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

保留文件屬性為公開

預設情況下,Aspose.Slides 會在簡報加密時包含文件屬性。IProtectionManager.setEncryptDocumentProperties 方法可獨立於投影片內容加密而控制此行為。若索引、分類、搜尋或文件管理系統必須在未提供開啟密碼的情況下讀取中繼資料,請在呼叫IProtectionManager.encrypt之前傳入false

以下範例在建立加密的 PPTX 簡報時,保持內建文件屬性為公開:

import com.aspose.slides.IDocumentProperties;
import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;

Presentation presentation = new Presentation();
try {
    IDocumentProperties properties = presentation.getDocumentProperties();
    properties.setAuthor("Contoso Knowledge Management");
    properties.setTitle("Quarterly Product Roadmap");
    properties.setKeywords("roadmap, planning, internal");

    presentation.getSlides().get_Item(0).setName("Encrypted presentation content");
    presentation.getProtectionManager().setEncryptDocumentProperties(false);
    presentation.getProtectionManager().encrypt("open_password");
    presentation.save("public-properties-encrypted.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

false 傳遞給IProtectionManager.setEncryptDocumentProperties不會使投影片、母片、版面配置、圖形、媒體或其他簡報內容公開。它僅影響文件屬性。若要在不載入加密內容的情況下讀取這些屬性,請參閱Manage Presentation Properties

載入加密的簡報

ILoadOptions.setPassword設定為開啟密碼,並在載入檔案時將該選項傳遞給Presentation。若需要開啟密碼但提供的密碼遺失或不正確,載入將失敗。

import com.aspose.slides.LoadOptions;
import com.aspose.slides.Presentation;

LoadOptions loadOptions = new LoadOptions();
loadOptions.setPassword("open_password");

Presentation presentation = new Presentation("encrypted-pres.pptx", loadOptions);
try {
    // 處理已解密的簡報。
} finally {
    presentation.dispose();
}

從簡報中移除加密

使用開啟密碼載入簡報,呼叫IProtectionManager.removeEncryption,並儲存結果。儲存後的簡報即可在不需要密碼的情況下載入。

import com.aspose.slides.LoadOptions;
import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;

LoadOptions loadOptions = new LoadOptions();
loadOptions.setPassword("open_password");

Presentation presentation = new Presentation("encrypted-pres.pptx", loadOptions);
try {
    presentation.getProtectionManager().removeEncryption();
    presentation.save("encryption-removed.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

在載入前驗證開啟密碼

使用IPresentationFactory.getPresentationInfo可取得IPresentationInfo,而不必建立完整的簡報實例。在請求或驗證密碼之前,先檢查IPresentationInfo.isPasswordProtected。若存在保護,請使用IPresentationInfo.checkPassword驗證提供的值。

檔案路徑工作流程

以下範例驗證 PPTX 檔案的開啟密碼,將驗證後的值傳遞給ILoadOptions.setPassword,然後載入完整的簡報:

import com.aspose.slides.IPresentationInfo;
import com.aspose.slides.LoadOptions;
import com.aspose.slides.Presentation;
import com.aspose.slides.PresentationFactory;

String filePath = "protected-presentation.pptx";
String password = "open_password";
IPresentationInfo presentationInfo = PresentationFactory.getInstance().getPresentationInfo(filePath);

if (!presentationInfo.isPasswordProtected()) {
    System.out.println("The presentation does not have an opening password.");
} else if (!presentationInfo.checkPassword(password)) {
    System.out.println("The opening password is incorrect.");
} else {
    LoadOptions loadOptions = new LoadOptions();
    loadOptions.setPassword(password);

    Presentation presentation = new Presentation(filePath, loadOptions);
    try {
        System.out.println("The presentation was validated and loaded successfully.");
    } finally {
        presentation.dispose();
    }
}

串流工作流程

IPresentationFactory.getPresentationInfo的串流重載提供相同的工作流程。載入完整簡報之前,先將可尋址串流的位置重設。

以下範例使用 PPT 檔案:

import com.aspose.slides.IPresentationInfo;
import com.aspose.slides.LoadOptions;
import com.aspose.slides.Presentation;
import com.aspose.slides.PresentationFactory;
import java.io.FileInputStream;

String password = "open_password";

FileInputStream presentationStream = new FileInputStream("protected-presentation.ppt");
try {
    IPresentationInfo presentationInfo = PresentationFactory.getInstance().getPresentationInfo(presentationStream);

    if (!presentationInfo.isPasswordProtected()) {
        System.out.println("The presentation does not have an opening password.");
    } else if (!presentationInfo.checkPassword(password)) {
        System.out.println("The opening password is incorrect.");
    } else {
        presentationStream.getChannel().position(0);

        LoadOptions loadOptions = new LoadOptions();
        loadOptions.setPassword(password);

        Presentation presentation = new Presentation(presentationStream, loadOptions);
        try {
            System.out.println("The presentation was validated and loaded successfully.");
        } finally {
            presentation.dispose();
        }
    }
} finally {
    presentationStream.close();
}

checkPassword 回傳值

IPresentationInfo.checkPassword僅在簡報具備開啟密碼且提供的密碼正確時回傳true。在以下情況皆回傳false

  • 密碼不正確。
  • 簡報沒有開啟密碼。
  • 提供的密碼為null或空字串。

此行為於 PPT 與 PPTX 簡報皆相同。

檢查已載入的簡報是否已加密

使用正確密碼載入簡報後,檢查IProtectionManager.isEncrypted以確認來源簡報已被加密。若要在載入前偵測開啟密碼保護,請使用上面示範的IPresentationInfo.isPasswordProtected

import com.aspose.slides.LoadOptions;
import com.aspose.slides.Presentation;

LoadOptions loadOptions = new LoadOptions();
loadOptions.setPassword("open_password");

Presentation presentation = new Presentation("encrypted-pres.pptx", loadOptions);
try {
    boolean isEncrypted = presentation.getProtectionManager().isEncrypted();
    System.out.println("The presentation is encrypted: " + isEncrypted);
} finally {
    presentation.dispose();
}

安全性建議

在線保護簡報密碼

  1. 開啟Aspose.Slides Lock應用程式。
  2. 選取或上傳簡報。
  3. 輸入檢視保護的密碼。
  4. (可選)輸入用於編輯保護的其他密碼。
  5. 套用保護並下載產生的檔案。

常見問題

開啟密碼與寫入保護密碼有何差異?

開啟密碼會加密簡報,且必須提供才能載入其內容。寫入保護密碼僅限制修改,且不會加密內容。

是否可以在不載入全部投影片的情況下驗證開啟密碼?

可以。先取得簡報資訊,檢查是否存在開啟密碼保護,然後在建立完整簡報實例之前驗證密碼。

應用程式是否能在沒有開啟密碼的情況下讀取中繼資料?

可以,但僅限於簡報在加密時已停用文件屬性加密的情況。此時應用程式必須使用 Manage Presentation Properties 中描述的僅載入文件屬性的模式。

密碼檢查工作流程是否同時支援 PPT 與 PPTX?

是。檔案路徑與串流模式的密碼偵測與驗證在 PPT 與 PPTX 簡報中皆表現相同。