Skip to content

暗号化とセキュリティ

PDF Oxide は業界標準のアルゴリズムを使用して、パスワードと権限による PDF の暗号化をサポートします。 You can set a user password (required to open the document), an owner password (required for full access), and fine-grained permissions controlling printing, copying, and modification.

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

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")?;

カスタム権限で暗号化

Python

The save_encrypted method accepts permission flags as keyword arguments.

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 Parameters

Parameter Type Default 説明
path str required Output file path
user_password str required 開くためのパスワード(空 = パスワードなし)
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 Fields

Field Type 説明
user_password String ドキュメントを開くためのパスワード
owner_password String フルアクセスとセキュリティ変更のためのパスワード
algorithm EncryptionAlgorithm 使用する暗号化アルゴリズム
permissions Permissions アクセス制御フラグ

暗号化アルゴリズム

Algorithm 説明
EncryptionAlgorithm::Aes256 AES-256 (strongest, recommended)
EncryptionAlgorithm::Aes128 AES-128
EncryptionAlgorithm::Rc4_128 RC4 128-bit (legacy compatibility)
EncryptionAlgorithm::Rc4_40 RC4 40-bit (legacy, weak)

AES-256 is the default when using save_encrypted() in Python or the Pdf API.

権限

The Permissions struct controls what operations are allowed when a document is opened with the user password.

use pdf_oxide::editor::Permissions;

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

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

権限 Fields

Field Type Default (all) Default (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 Allow page assembly operations

カスタム権限

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

Use SaveOptions for full control over how the document is written.

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

// 完全書き換え(デフォルト)
let opts = SaveOptions::full_rewrite();

// 増分更新(高速、構造保持)
let opts = SaveOptions::incremental();

// 暗号化付き
let config = EncryptionConfig::new("user", "owner");
let opts = SaveOptions::with_encryption(config);

暗号化された PDF を開く

Python

Pass the password when opening the document.

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

暗号化ワークフロー(完全版)

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",   # オーナーパスワード 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)?;

別の設定で再暗号化

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 Methods

Method Returns 説明
save_encrypted(path, user_pw, owner_pw) Result<()> AES-256と全権限で保存
save_with_encryption(path, config) Result<()> カスタム暗号化設定で保存

DocumentEditor / EditableDocument Methods

Method Returns 説明
save(path) Result<()> 完全書き換えで保存 (no encryption)
save_with_options(path, options) Result<()> カスタムオプションで保存

設定 Types

Type 説明
EncryptionConfig ユーザー/オーナーパスワード、アルゴリズム、権限
EncryptionAlgorithm Aes256, Aes128, Rc4_128, Rc4_40
Permissions きめ細かいアクセス制御フラグ
SaveOptions 完全書き換え、増分、暗号化保存

関連ページ