Java で PDF ファイルを暗号化および復号化

Aspose.PDF for Java は、PDF のセキュリティ操作を PdfFileSecurity ファサードを通じて提供します。

ユーザー パスワードとオーナー パスワードで PDF の暗号化

  1. PdfFileSecurity インスタンスを作成し、ソース PDF ドキュメントにバインドしてください。
  2. 例に必要な DocumentPrivilege および KeySize プロパティを設定してください。
  3. 更新された PDF ドキュメントを PdfFileSecurity を通じて保存してください。
public static void encryptPdfWithUserOwnerPassword(Path inputFile, Path outputFile) {
    PdfFileSecurity fileSecurity = new PdfFileSecurity();
    fileSecurity.bindPdf(inputFile.toString());
    DocumentPrivilege privilege = DocumentPrivilege.getForbidAll();
    privilege.setAllowPrint(true);
    fileSecurity.encryptFile("user_password", "owner_password", privilege, KeySize.x128);
    fileSecurity.save(outputFile.toString());
    fileSecurity.close();
}

特定のアルゴリズムで PDF の暗号化

encryptPdfWithEncryptionAlgorithm は KeySize.x256 と Algorithm.AES を組み合わせて、より強力な暗号化設定を適用します。

保護された PDF の復号化

  1. PdfFileSecurity インスタンスを作成し、ソース PDF ドキュメントにバインドしてください。
  2. 保護された文書をオーナーパスワードで復号化してください。
  3. 更新された PDF ドキュメントを PdfFileSecurity を通じて保存してください。
public static void decryptPdfWithOwnerPassword(Path inputFile, Path outputFile) {
    PdfFileSecurity fileSecurity = new PdfFileSecurity();
    fileSecurity.bindPdf(inputFile.toString());
    fileSecurity.decryptFile("owner_password");
    fileSecurity.save(outputFile.toString());
    fileSecurity.close();
}

例のセットには tryDecryptPdfWithoutException も含まれており、復号に失敗した場合に例外をスローする代わりに false を返します。

パスワードの変更とセキュリティのリセット

PdfFileSecurityExamples クラスでは、以下の操作を示しています。

  • changeUserAndOwnerPassword:ユーザー パスワードとオーナー パスワードの両方を置き換えます。
  • changePasswordAndResetSecurity:パスワードを変更し、権限を再適用する操作を 1 ステップで実行します。
  • tryChangePasswordWithoutException:例外をスローしないパスワード変更フロー用です。

ドキュメントの権限の設定

印刷やコピーなどの操作を制限するには、次のように行います。

  1. PdfFileSecurity インスタンスを作成し、ソース PDF ドキュメントにバインドしてください。
  2. DocumentPrivilege 権限または暗号化オプションとして必要な設定を行ってください。
  3. サンプルで必要なプロパティを設定してください。
  4. 更新された PDF ドキュメントを PdfFileSecurity を通じて保存してください。
public static void setPdfPrivilegesWithPasswords(Path inputFile, Path outputFile) {
    PdfFileSecurity fileSecurity = new PdfFileSecurity();
    fileSecurity.bindPdf(inputFile.toString());
    DocumentPrivilege privilege = DocumentPrivilege.getForbidAll();
    privilege.setAllowPrint(true);
    privilege.setAllowCopy(false);
    fileSecurity.setPrivilege("user_password", "owner_password", privilege);
    fileSecurity.save(outputFile.toString());
    fileSecurity.close();
}