加密与安全
PDF Oxide 支持使用行业标准算法通过密码和权限对 PDF 进行加密。你可以设置用户密码(打开文档所需)、所有者密码(获得完整访问权限所需),以及控制打印、复制和修改的精细权限。
绑定覆盖范围。 打开加密 PDF 的功能适用于所有绑定(Python 中为
password=,Rust 中为PdfDocument.open_with_password,WASM 中为authenticate(),Go/C# 的PdfDocument中为Authenticate,C# 中为OpenWithPassword)。生成加密输出(save_encrypted/saveEncryptedToBytes)在 Python、Rust、WASM 和 Go(DocumentEditor.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
如需完全控制加密设置,请使用 EncryptionConfig 和 SaveOptions。
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 |
Aes256、Aes128、Rc4_128、Rc4_40 |
Permissions |
精细化访问控制标志 |
SaveOptions |
完整重写、增量更新或加密保存 |
相关页面
- 编辑概览 – 打开、元数据和保存工作流
- 表单字段编辑 – 通过权限限制表单编辑
- 内容涂黑(Redaction) – 加密前涂黑内容
- 页面操作 – 最终加密前准备页面