OFD 安全信封
本节介绍 OFD 安全信封(OFDEnvelope)能力,包括读取信封信息、验证信封密码、将 OFD 文件加密为信封文件,以及将信封文件解密为 OFD 文件。
OFDEnvelope 是信封处理的工具类,公开能力以静态方法为主;OFDEnvelopeInfo 用于保存信封元数据和加密参数。信封处理不依赖 OFDDoc 或 OFDPage 作为入口。Decrypt 输出普通 OFD 文件后,按 OFD 概述 中的打开流程,通过 OFD 文件包 加载 OFDDoc 后继续处理。
能力概览
| 能力 | 说明 |
|---|---|
| 信封工具 | 仅静态方法:GetEnvelopeInfo、VerifyPassword、 Encrypt、Decrypt |
| 信封信息 | SetTitle、SetPassword、SetCipher 等;IsEmpty 判断有效性 |
| 读取信息 | 参数为信封文件路径;不解密正文 |
| 验证密码 | password + OFDEnvelopeInfo |
| 加密为信封 | info、输入 OFD 路径、输出信封路径 |
| 解密为 OFD | password、输入信封路径、输出 OFD 路径 |
| 加密算法 | RC4、AES128/192/256、SM4 |
类与方法细节见接口文档。
基本处理流程
信封文件由 SDK 创建和解析,开发者通常不需要直接操作信封内部结构。典型流程如下:
- 构造
OFDEnvelopeInfo,设置标题、密码、加密算法等信息。 - 调用
OFDEnvelope::Encrypt,将输入 OFD 文件加密输出为信封文件。 - 调用
OFDEnvelope::GetEnvelopeInfo读取信封信息。 - 调用
OFDEnvelope::VerifyPassword验证密码。 - 调用
OFDEnvelope::Decrypt,将信封文件解密输出为 OFD 文件。
以下示例演示信封信息读取、密码验证、加密和解密流程。
c++
#include "ofd/fs_ofdenvelope.h"
using namespace foxit;
using namespace foxit::ofd;
bool ProcessOFDEnvelope(const wchar_t* input_ofd_file,
const wchar_t* output_envelope_file,
const wchar_t* output_ofd_file) {
OFDEnvelopeInfo info;
if (info.IsEmpty()) {
return false;
}
info.SetTitle(L"TestEnvelope");
info.SetDocType(L"OFD");
info.SetDescription(L"Envelope created by Foxit PDF SDK");
info.SetCreator(L"Foxit PDF SDK");
info.SetPassword(L"testpwd");
info.SetCipher(OFDEnvelopeInfo::e_EnvelopeEncryptTypeAES128);
if (!OFDEnvelope::Encrypt(info, WString(input_ofd_file), WString(output_envelope_file))) {
return false;
}
OFDEnvelopeInfo envelope_info = OFDEnvelope::GetEnvelopeInfo(WString(output_envelope_file));
if (envelope_info.IsEmpty()) {
return false;
}
if (!OFDEnvelope::VerifyPassword(String("testpwd"), envelope_info)) {
return false;
}
return OFDEnvelope::Decrypt(String("testpwd"),
WString(output_envelope_file),
WString(output_ofd_file));
}
java
import com.foxit.sdk.ofd.OFDEnvelope;
import com.foxit.sdk.ofd.OFDEnvelopeInfo;
public class OFDEnvelopeExample {
public static boolean processOFDEnvelope(String inputOfdFile,
String outputEnvelopeFile, String outputOfdFile) throws Exception {
OFDEnvelopeInfo info = new OFDEnvelopeInfo();
if (info.isEmpty()) {
return false;
}
info.setTitle("TestEnvelope");
info.setDocType("OFD");
info.setDescription("Envelope created by Foxit PDF SDK");
info.setCreator("Foxit PDF SDK");
info.setPassword("testpwd");
info.setCipher(OFDEnvelopeInfo.e_EnvelopeEncryptTypeAES128);
if (!OFDEnvelope.encrypt(info, inputOfdFile, outputEnvelopeFile)) {
return false;
}
OFDEnvelopeInfo envelopeInfo = OFDEnvelope.getEnvelopeInfo(outputEnvelopeFile);
if (envelopeInfo.isEmpty()) {
return false;
}
if (!OFDEnvelope.verifyPassword("testpwd", envelopeInfo)) {
return false;
}
return OFDEnvelope.decrypt("testpwd", outputEnvelopeFile, outputOfdFile);
}
}
csharp
using foxit.ofd;
bool ProcessOFDEnvelope(string inputOfdFile, string outputEnvelopeFile, string outputOfdFile)
{
using (OFDEnvelopeInfo info = new OFDEnvelopeInfo())
{
if (info.IsEmpty())
{
return false;
}
info.SetTitle("TestEnvelope");
info.SetDocType("OFD");
info.SetDescription("Envelope created by Foxit PDF SDK");
info.SetCreator("Foxit PDF SDK");
info.SetPassword("testpwd");
info.SetCipher(OFDEnvelopeInfo.EnvelopeEncryptType.e_EnvelopeEncryptTypeAES128);
if (!OFDEnvelope.Encrypt(info, inputOfdFile, outputEnvelopeFile))
{
return false;
}
using (OFDEnvelopeInfo envelopeInfo = OFDEnvelope.GetEnvelopeInfo(outputEnvelopeFile))
{
if (envelopeInfo.IsEmpty())
{
return false;
}
if (!OFDEnvelope.VerifyPassword("testpwd", envelopeInfo))
{
return false;
}
return OFDEnvelope.Decrypt("testpwd", outputEnvelopeFile, outputOfdFile);
}
}
}
python
from FoxitPDFSDKPython3 import *
def process_ofd_envelope(input_ofd_file, output_envelope_file, output_ofd_file):
info = OFDEnvelopeInfo()
if info.IsEmpty():
return False
info.SetTitle("TestEnvelope")
info.SetDocType("OFD")
info.SetDescription("Envelope created by Foxit PDF SDK")
info.SetCreator("Foxit PDF SDK")
info.SetPassword("testpwd")
info.SetCipher(OFDEnvelopeInfo.e_EnvelopeEncryptTypeAES128)
if not OFDEnvelope.Encrypt(info, input_ofd_file, output_envelope_file):
return False
envelope_info = OFDEnvelope.GetEnvelopeInfo(output_envelope_file)
if envelope_info.IsEmpty():
return False
if not OFDEnvelope.VerifyPassword("testpwd", envelope_info):
return False
return OFDEnvelope.Decrypt("testpwd", output_envelope_file, output_ofd_file)
go
package main
import "foxit.com/fsdk"
func ProcessOFDEnvelope(inputOfdFile, outputEnvelopeFile, outputOfdFile string) bool {
info := fsdk.NewOFDEnvelopeInfo()
defer fsdk.DeleteOFDEnvelopeInfo(info)
if info.IsEmpty() {
return false
}
info.SetTitle("TestEnvelope")
info.SetDocType("OFD")
info.SetDescription("Envelope created by Foxit PDF SDK")
info.SetCreator("Foxit PDF SDK")
info.SetPassword("testpwd")
info.SetCipher(fsdk.OFDEnvelopeInfoE_EnvelopeEncryptTypeAES128)
if !fsdk.OFDEnvelopeEncrypt(info, inputOfdFile, outputEnvelopeFile) {
return false
}
envelopeInfo := fsdk.OFDEnvelopeGetEnvelopeInfo(outputEnvelopeFile)
defer fsdk.DeleteOFDEnvelopeInfo(envelopeInfo)
if envelopeInfo.IsEmpty() {
return false
}
if !fsdk.OFDEnvelopeVerifyPassword("testpwd", envelopeInfo) {
return false
}
return fsdk.OFDEnvelopeDecrypt("testpwd", outputEnvelopeFile, outputOfdFile)
}
js
const FSDK = require("@fuxinsoft/fuxin-pdf-ofd-sdk-node");
function processOFDEnvelope(inputOfdFile, outputEnvelopeFile, outputOfdFile) {
const info = new FSDK.OFDEnvelopeInfo();
if (info.IsEmpty()) {
return false;
}
info.SetTitle("TestEnvelope");
info.SetDocType("OFD");
info.SetDescription("Envelope created by Foxit PDF SDK");
info.SetCreator("Foxit PDF SDK");
info.SetPassword("testpwd");
info.SetCipher(FSDK.OFDEnvelopeInfo.e_EnvelopeEncryptTypeAES128);
if (!FSDK.OFDEnvelope.Encrypt(info, inputOfdFile, outputEnvelopeFile)) {
return false;
}
const envelopeInfo = FSDK.OFDEnvelope.GetEnvelopeInfo(outputEnvelopeFile);
if (envelopeInfo.IsEmpty()) {
return false;
}
if (!FSDK.OFDEnvelope.VerifyPassword("testpwd", envelopeInfo)) {
return false;
}
return FSDK.OFDEnvelope.Decrypt("testpwd", outputEnvelopeFile, outputOfdFile);
}
信封信息字段
OFDEnvelopeInfo 表示信封信息,常用字段如下:
| 字段 | 说明 |
|---|---|
Title | 信封标题或文件名称 |
DocType | 文件类型,例如 OFD |
Description | 信封描述 |
Creator | 创建人 |
CreateTime | 创建时间 |
StartTime / ExpirationTime | 有效期开始与结束时间 |
Password | 信封密码 |
Permission | 用户权限字符串 |
FileCode | 文件代码 |
HashKey | 密码哈希值 |
Filter / SubFilter | 安全处理器标识 |
Cipher | 加密算法,使用 EnvelopeEncryptType 枚举 |
SecurityType / CryptoType | 安全类型与加密类型字符串 |
Cipher 是公开枚举,支持 RC4、AES128、AES192、AES256、SM4。Permission、SecurityType、CryptoType 均以字符串形式返回;SDK 当前未在公开 API 中提供对应枚举定义。使用 Encrypt 创建信封时,如果 Filter、SubFilter、SecurityType 或 CryptoType 为空,SDK 会使用默认值。
| 字段 | 默认值 |
|---|---|
Filter | htfoxit.PwdSec |
SubFilter | htfoxit.sha256.s1 |
SecurityType | Standard |
CryptoType | Standard |
注意事项
GetEnvelopeInfo返回的是OFDEnvelopeInfo,可通过OFDEnvelopeInfo::IsEmpty判断对象是否有效。- 信封文件内部结构由 SDK 创建和解析,开发者通常不需要直接操作内部 XML 或包结构。