Skip to content

暗号化とセキュリティ

PDF Oxideは、業界標準のアルゴリズムを使用してパスワードと権限でPDFを暗号化できます。ユーザーパスワード(文書を開くために必要)、オーナーパスワード(フルアクセスに必要)、および印刷・コピー・変更を制御するきめ細かい権限を設定できます。

バインディング対応状況。 暗号化PDFを開く機能はすべてのバインディングで利用できます(Pythonでは password=、Rustでは PdfDocument.open_with_password、WASMでは authenticate()、Go/C# の PdfDocument では Authenticate、C# では OpenWithPassword)。暗号化出力の生成(save_encrypted / saveEncryptedToBytes)は PythonRustWASM、および GoDocumentEditor.SaveEncrypted)で使用できます。C# の DocumentEditor は現時点では SaveEncrypted を公開していません。C# ワークフローから暗号化出力を生成するには、Rust CLI(pdf-oxide encrypt)またはGo/Pythonステップを使用してください。

アルゴリズムのサポート

アルゴリズム 読み取り 書き込み 備考
RC4 (40/128ビット) 対応 対応 レガシー。互換性のためにのみ使用
AES-128 (V=4, R=4) 対応 対応 PDF 1.6以降のデフォルト
AES-256 (V=5, R=6) 対応 対応 PDF 2.0。非圧縮オブジェクト文字列の復号、プッシュボタンウィジェットの /MK /CA キャプション、正しいAlgorithm 2.B終端処理を含む

AES-256はエンドツーエンドで完全対応しています:開く、認証する、フォーム値を読む、暗号化出力として保存する、といったすべての処理が可能です。ObjStm / XRef ストリームはISO 32000-2 §7.6.3に従い暗号化されません。遅延 authenticate() 呼び出し後はオブジェクトキャッシュが正しく無効化されるため、認証前に読んだコンテンツは正しいキーで再解析されます。

クイックスタート:暗号化して保存

Python

from pdf_oxide import PdfDocument

doc = PdfDocument("input.pdf")
doc.set_title("Confidential Report")

# Encrypt with user and owner passwords
doc.save_encrypted("protected.pdf", "user123", "owner456")

WASM

import { WasmPdfDocument } from "pdf-oxide-wasm";

const doc = new WasmPdfDocument(bytes);
doc.setTitle("Confidential Report");

// Encrypt with user and owner passwords (all permissions enabled)
const output = doc.saveEncryptedToBytes(
  "user123", "owner456", true, true, true, true
);
doc.free();

Rust

use pdf_oxide::api::Pdf;

let mut doc = Pdf::open("input.pdf")?;

// Simple encryption with user and owner passwords
doc.save_encrypted("protected.pdf", "user123", "owner456")?;

Go

package main

import (
    "log"
    pdfoxide "github.com/yfedoseev/pdf_oxide/go"
)

func main() {
    editor, err := pdfoxide.OpenEditor("input.pdf")
    if err != nil { log.Fatal(err) }
    defer editor.Close()

    _ = editor.SetTitle("Confidential Report")

    // Encrypt with user and owner passwords (AES-256)
    if err := editor.SaveEncrypted("protected.pdf", "user123", "owner456"); err != nil {
        log.Fatal(err)
    }
}

C++

#include <pdf_oxide/pdf_oxide.hpp>

auto editor = pdf_oxide::DocumentEditor::open("input.pdf");

// Encrypt with user and owner passwords (AES-256)
editor.save_encrypted("protected.pdf", "user123", "owner456");

Swift

import PdfOxide

let editor = try DocumentEditor.openEditor("input.pdf")

// Encrypt with user and owner passwords (AES-256)
try editor.saveEncrypted("protected.pdf", userPassword: "user123", ownerPassword: "owner456")

Dart

import 'package:pdf_oxide/pdf_oxide.dart';

final editor = DocumentEditor.open('input.pdf');

// Encrypt with user and owner passwords (AES-256)
editor.saveEncrypted('protected.pdf', 'user123', 'owner456');
editor.close();

R

library(pdfoxide)

editor <- pdf_editor_open("input.pdf")

# Encrypt with user and owner passwords (AES-256)
pdf_editor_save_encrypted(editor, "protected.pdf", "user123", "owner456")

Julia

using PdfOxide

editor = open_editor("input.pdf")

# Encrypt with user and owner passwords (AES-256)
save_encrypted(editor, "protected.pdf", "user123", "owner456")

Zig

const pdf_oxide = @import("pdf_oxide");

var editor = try pdf_oxide.DocumentEditor.openEditor("input.pdf");
defer editor.deinit();

// Encrypt with user and owner passwords (AES-256)
try editor.saveEncrypted("protected.pdf", "user123", "owner456");

Objective-C

#import "POXPdfOxide.h"
NSError *err = nil;

POXDocumentEditor *editor = [POXDocumentEditor openEditor:@"input.pdf" error:&err];

// Encrypt with user and owner passwords (AES-256)
[editor saveEncryptedToPath:@"protected.pdf"
               userPassword:@"user123"
              ownerPassword:@"owner456"
                      error:&err];

Elixir

{:ok, editor} = PdfOxide.open_editor("input.pdf")

# Encrypt with user and owner passwords (AES-256)
:ok = PdfOxide.editor_save_encrypted(editor, "protected.pdf", "user123", "owner456")

カスタム権限による暗号化

Python

save_encrypted メソッドはキーワード引数として権限フラグを受け取ります。

from pdf_oxide import PdfDocument

doc = PdfDocument("input.pdf")

# View-only: no printing, copying, or modifying
doc.save_encrypted(
    "readonly.pdf",
    "viewpass",
    "adminpass",
    allow_print=False,
    allow_copy=False,
    allow_modify=False,
    allow_annotate=False,
)

# Allow only printing
doc.save_encrypted(
    "print-only.pdf",
    "",            # No open password required
    "adminpass",
    allow_print=True,
    allow_copy=False,
    allow_modify=False,
    allow_annotate=False,
)

Python save_encrypted のパラメーター

パラメーター デフォルト 説明
path str 必須 出力ファイルパス
user_password str 必須 開くためのパスワード(空文字列 = パスワードなし)
owner_password str None フルアクセス用パスワード(デフォルトはユーザーパスワード)
allow_print bool True 印刷を許可
allow_copy bool True テキスト・画像のコピーを許可
allow_modify bool True 文書の変更を許可
allow_annotate bool True 注釈の追加を許可

WASM

import { WasmPdfDocument } from "pdf-oxide-wasm";

const doc = new WasmPdfDocument(bytes);

// View-only: no printing, copying, or modifying
const readonly = doc.saveEncryptedToBytes(
  "viewpass", "adminpass", false, false, false, false
);

// Allow only printing (empty user password = no open password)
const printOnly = doc.saveEncryptedToBytes(
  "", "adminpass", true, false, false, false
);
doc.free();

Rust

暗号化設定を完全に制御するには、EncryptionConfigSaveOptions を使用します。

use pdf_oxide::api::Pdf;
use pdf_oxide::editor::{
    EncryptionConfig, EncryptionAlgorithm, Permissions, SaveOptions,
};

let mut doc = Pdf::open("input.pdf")?;

// Build permissions
let mut perms = Permissions::read_only();
perms.print = true;  // Allow printing only

// Build encryption config
let config = EncryptionConfig::new("user123", "owner456")
    .with_algorithm(EncryptionAlgorithm::Aes256)
    .with_permissions(perms);

// Save with encryption
doc.save_with_encryption("protected.pdf", config)?;

EncryptionConfig

EncryptionConfig 構造体はすべての暗号化パラメーターを制御します。

use pdf_oxide::editor::{EncryptionConfig, EncryptionAlgorithm, Permissions};

let config = EncryptionConfig {
    user_password: "user123".to_string(),
    owner_password: "owner456".to_string(),
    algorithm: EncryptionAlgorithm::Aes256,
    permissions: Permissions::all(),
};

ビルダーパターンでも記述できます:

let config = EncryptionConfig::new("user123", "owner456")
    .with_algorithm(EncryptionAlgorithm::Aes128)
    .with_permissions(Permissions::read_only());

EncryptionConfig のフィールド

フィールド 説明
user_password String 文書を開くために必要なパスワード
owner_password String フルアクセスおよびセキュリティ変更用のパスワード
algorithm EncryptionAlgorithm 使用する暗号化アルゴリズム
permissions Permissions アクセス制御フラグ

暗号化アルゴリズム

アルゴリズム 説明
EncryptionAlgorithm::Aes256 AES-256(最強、推奨)
EncryptionAlgorithm::Aes128 AES-128
EncryptionAlgorithm::Rc4_128 RC4 128ビット(レガシー互換)
EncryptionAlgorithm::Rc4_40 RC4 40ビット(レガシー、脆弱)

Pythonの save_encrypted() または Pdf APIを使用する場合、AES-256がデフォルトです。

権限(Permissions)

Permissions 構造体は、ユーザーパスワードで開いた場合に許可される操作を制御します。

use pdf_oxide::editor::Permissions;

// Allow everything
let all = Permissions::all();

// Restrict everything
let readonly = Permissions::read_only();

Permissions のフィールド

フィールド デフォルト (all) デフォルト (read_only) 説明
print bool true false 印刷を許可
print_high_quality bool true false 高品質印刷を許可
modify bool true false コンテンツの変更を許可
copy bool true false テキスト・画像のコピーを許可
annotate bool true false 注釈の追加を許可
fill_forms bool true false フォームフィールドへの入力を許可
accessibility bool true true アクセシビリティ抽出を許可
assemble bool true false ページの組み立て操作を許可

カスタム権限の設定

let mut perms = Permissions::read_only();
perms.print = true;          // Allow printing
perms.fill_forms = true;     // Allow filling forms
perms.accessibility = true;  // Always allow for compliance

SaveOptions

SaveOptions を使用すると、文書の書き込み方法を完全に制御できます。

use pdf_oxide::editor::{SaveOptions, EncryptionConfig};

// Full rewrite (default)
let opts = SaveOptions::full_rewrite();

// Incremental update (faster, preserves structure)
let opts = SaveOptions::incremental();

// With encryption
let config = EncryptionConfig::new("user", "owner");
let opts = SaveOptions::with_encryption(config);

暗号化PDFを開く

Python

文書を開く際にパスワードを渡します。

from pdf_oxide import PdfDocument

doc = PdfDocument("protected.pdf", password="user123")
text = doc.extract_text(0)
print(text)

Rust

use pdf_oxide::PdfDocument;

let doc = PdfDocument::open_with_password("protected.pdf", "user123")?;
let text = doc.extract_text(0)?;
println!("{}", text);

Go

doc, _ := pdfoxide.Open("protected.pdf")
defer doc.Close()
if _, err := doc.Authenticate("user123"); err != nil { log.Fatal(err) }
text, _ := doc.ExtractText(0)
fmt.Println(text)

C#

using var doc = PdfDocument.OpenWithPassword("protected.pdf", "user123");
Console.WriteLine(doc.ExtractText(0));

C++

#include <pdf_oxide/pdf_oxide.hpp>
#include <iostream>

auto doc = pdf_oxide::Document::open_with_password("protected.pdf", "user123");
std::cout << doc.extract_text(0) << std::endl;

Swift

import PdfOxide

let doc = try Document.openWithPassword("protected.pdf", password: "user123")
print(try doc.extractText(0))

Dart

import 'package:pdf_oxide/pdf_oxide.dart';

final doc = PdfDocument.openWithPassword('protected.pdf', 'user123');
print(doc.extractText(0));
doc.close();

R

library(pdfoxide)

doc <- pdf_open_with_password("protected.pdf", "user123")
cat(pdf_extract_text(doc, 0))

Julia

using PdfOxide

doc = open_with_password("protected.pdf", "user123")
println(extract_text(doc, 0))

Zig

const std = @import("std");
const pdf_oxide = @import("pdf_oxide");
const a = std.heap.page_allocator;

var doc = try pdf_oxide.Document.openWithPassword("protected.pdf", "user123");
defer doc.deinit();
const text = try doc.extractText(a, 0);
std.debug.print("{s}\n", .{text});

Objective-C

#import "POXPdfOxide.h"
NSError *err = nil;

POXDocument *doc = [POXDocument openWithPassword:@"protected.pdf" password:@"user123" error:&err];
NSLog(@"%@", [doc extractText:0 error:&err]);

Elixir

{:ok, doc} = PdfOxide.open_with_password("protected.pdf", "user123")
{:ok, text} = PdfOxide.extract_text(doc, 0)
IO.puts(text)

完全な暗号化ワークフロー

Python

from pdf_oxide import PdfDocument

# Open and modify
doc = PdfDocument("report.pdf")
doc.set_title("Confidential Report")
doc.set_author("Finance Team")

# Save with view-only restrictions
doc.save_encrypted(
    "report-protected.pdf",
    "",            # No password to open
    "admin2025",   # Owner password for full access
    allow_print=True,
    allow_copy=False,
    allow_modify=False,
)

WASM

import { WasmPdfDocument } from "pdf-oxide-wasm";

const doc = new WasmPdfDocument(bytes);
doc.setTitle("Confidential Report");
doc.setAuthor("Finance Team");

// Save with view-only restrictions (no open password, print allowed)
const output = doc.saveEncryptedToBytes(
  "", "admin2025", true, false, false, false
);
doc.free();

Rust

use pdf_oxide::api::Pdf;
use pdf_oxide::editor::{
    DocumentEditor, EditableDocument,
    EncryptionConfig, EncryptionAlgorithm, Permissions, SaveOptions,
};

// Open and modify
let mut doc = Pdf::open("report.pdf")?;
{
    let editor = doc.editor().unwrap();
    editor.set_title("Confidential Report");
    editor.set_author("Finance Team");
}

// Configure encryption
let permissions = Permissions {
    print: true,
    print_high_quality: true,
    modify: false,
    copy: false,
    annotate: false,
    fill_forms: true,
    accessibility: true,
    assemble: false,
};

let config = EncryptionConfig::new("", "admin2025")
    .with_algorithm(EncryptionAlgorithm::Aes256)
    .with_permissions(permissions);

doc.save_with_encryption("report-protected.pdf", config)?;

以下のフルサーフェスバインディングはAES-256暗号化とフル権限で出力を保存します。Python/WASM/Rustで利用できる個別フラグによる権限制御は公開されていません。保存前にエディターの set_producer/Info メタデータを設定してください。

C++

#include <pdf_oxide/pdf_oxide.hpp>

auto editor = pdf_oxide::DocumentEditor::open("report.pdf");
editor.set_producer("Finance Team");

// Save with AES-256 encryption (no open password, owner password for full access)
editor.save_encrypted("report-protected.pdf", "", "admin2025");

Swift

import PdfOxide

let editor = try DocumentEditor.openEditor("report.pdf")
try editor.setProducer("Finance Team")

// Save with AES-256 encryption (no open password, owner password for full access)
try editor.saveEncrypted("report-protected.pdf", userPassword: "", ownerPassword: "admin2025")

Dart

import 'package:pdf_oxide/pdf_oxide.dart';

final editor = DocumentEditor.open('report.pdf');
editor.setProducer('Finance Team');

// Save with AES-256 encryption (no open password, owner password for full access)
editor.saveEncrypted('report-protected.pdf', '', 'admin2025');
editor.close();

R

library(pdfoxide)

editor <- pdf_editor_open("report.pdf")
pdf_editor_set_producer(editor, "Finance Team")

# Save with AES-256 encryption (no open password, owner password for full access)
pdf_editor_save_encrypted(editor, "report-protected.pdf", "", "admin2025")

Julia

using PdfOxide

editor = open_editor("report.pdf")
set_producer(editor, "Finance Team")

# Save with AES-256 encryption (no open password, owner password for full access)
save_encrypted(editor, "report-protected.pdf", "", "admin2025")

Zig

const pdf_oxide = @import("pdf_oxide");

var editor = try pdf_oxide.DocumentEditor.openEditor("report.pdf");
defer editor.deinit();
try editor.setProducer("Finance Team");

// Save with AES-256 encryption (no open password, owner password for full access)
try editor.saveEncrypted("report-protected.pdf", "", "admin2025");

Objective-C

#import "POXPdfOxide.h"
NSError *err = nil;

POXDocumentEditor *editor = [POXDocumentEditor openEditor:@"report.pdf" error:&err];
[editor setProducer:@"Finance Team" error:&err];

// Save with AES-256 encryption (no open password, owner password for full access)
[editor saveEncryptedToPath:@"report-protected.pdf"
               userPassword:@""
              ownerPassword:@"admin2025"
                      error:&err];

Elixir

{:ok, editor} = PdfOxide.open_editor("report.pdf")
:ok = PdfOxide.set_producer(editor, "Finance Team")

# Save with AES-256 encryption (no open password, owner password for full access)
:ok = PdfOxide.editor_save_encrypted(editor, "report-protected.pdf", "", "admin2025")

異なる設定で再暗号化する

Rust

use pdf_oxide::editor::{DocumentEditor, EditableDocument, EncryptionConfig, SaveOptions};

// Open with current password
let mut editor = DocumentEditor::open("old-protected.pdf")?;

// Save with new encryption
let config = EncryptionConfig::new("newuser", "newowner");
let options = SaveOptions::with_encryption(config);
editor.save_with_options("re-encrypted.pdf", options)?;

完全APIリファレンス

Pdf のメソッド

メソッド 戻り値 説明
save_encrypted(path, user_pw, owner_pw) Result<()> AES-256とフル権限で保存
save_with_encryption(path, config) Result<()> カスタム暗号化設定で保存

DocumentEditor / EditableDocument のメソッド

メソッド 戻り値 説明
save(path) Result<()> フル書き換えで保存(暗号化なし)
save_with_options(path, options) Result<()> カスタムオプションで保存

設定型

説明
EncryptionConfig ユーザー/オーナーパスワード、アルゴリズム、権限
EncryptionAlgorithm Aes256Aes128Rc4_128Rc4_40
Permissions きめ細かいアクセス制御フラグ
SaveOptions フル書き換え、増分更新、または暗号化保存

関連ページ