C#如何快速檢測PDF是否加密并驗(yàn)證正確密碼
引言
在批量文檔處理系統(tǒng)(如 OCR 文字識別、內(nèi)容提取、格式轉(zhuǎn)換)中,加密 PDF 無法直接操作。檢測加密狀態(tài)可提前篩選文件,避免流程因密碼驗(yàn)證失敗而中斷。
本文使用 Free Spire.PDF for .NET(一個(gè)免費(fèi)的.NET PDF處理庫),演示如何通過C# 檢測PDF是否加密,并驗(yàn)證密碼的正確性。
Free Spire.PDF for .NET簡介
核心功能:
- 免費(fèi)提供基礎(chǔ)的PDF創(chuàng)建、編輯和加密檢測功能
- 支持.NET Framework 和 .NET Core
- 無需Adobe依賴
安裝方式:
可以通過 NuGet 包管理器來完成安裝。打開 Visual Studio,在項(xiàng)目中右鍵點(diǎn)擊 "管理 NuGet 程序包",搜索 "Free Spire.PDF",然后選擇合適的版本進(jìn)行安裝。
關(guān)鍵提示: 加密檢測 ≠ 破解工具。Spire.PDF僅對合法持有的文件進(jìn)行操作。
C# 檢測PDF文檔是否加密
要檢測一個(gè)PDF文件的加密狀態(tài),可以直接使用 PdfDocument 類的 IsPasswordProtected(string fileName) 方法。該屬性返回一個(gè)布爾值,如果為true,表示 PDF 已加密;如果為false,表示 PDF 未加密。示例C#代碼如下:
using Spire.Pdf;
namespace CheckIfPdfIsProtected
{
class Program
{
static void Main(string[] args)
{
// 指定PDF文檔路徑
string pdfPath = "E:\\PythonPDF\\加密PDF.pdf";
// 檢測PDF文檔是否加密
bool isProtected = PdfDocument.IsPasswordProtected(pdfPath);
// 輸出結(jié)果
if (isProtected)
{
Console.WriteLine("該P(yáng)DF已加密。");
}
else
{
Console.WriteLine("該P(yáng)DF未加密。");
}
}
}
}輸出結(jié)果:

C# 測試PDF正確密碼
如果檢測到 PDF 已加密,接下來需要測試正確的密碼。Free Spire.PDF for .NET沒有直接的驗(yàn)證密碼的接口,但是可以通過嘗試用密碼打開PDF文件并捕獲異常來驗(yàn)證。
示例C#代碼如下:
using Spire.Pdf;
namespace DetermineTheCorrectPasswordOfPdf
{
class Program
{
static void Main(string[] args)
{
// 指定PDF文件和測試密碼
string pdfPath = "加密PDF.pdf";
string[] testPasswords = { "admin@2023", "abc", "12345" };
// 遍歷測試密碼
foreach (var pwd in testPasswords)
{
if (ValidatePdfPassword(pdfPath, pwd))
{
Console.WriteLine($"成功!正確密碼為:{pwd}");
break;
}
}
// 驗(yàn)證PDF密碼的方法
static bool ValidatePdfPassword(string filePath, string password)
{
// 用測試密碼逐一打開PDF進(jìn)行驗(yàn)證
PdfDocument pdf = new PdfDocument();
try
{
pdf.LoadFromFile(filePath, password);
// 無異常 = 密碼正確
return true;
}
catch (Exception)
{
return false; // 密碼錯(cuò)誤
}
finally
{
pdf.Close();
}
}
}
}
}輸出結(jié)果:

批量檢測+密碼測試 (完整企業(yè)級解決方案)
對于需要批量處理文檔應(yīng)用場景,如:
企業(yè)文檔管理:批量檢測加密 PDF 并嘗試常用密碼(如部門默認(rèn)密碼),解決密碼遺忘導(dǎo)致的文檔訪問問題。
數(shù)據(jù)恢復(fù):針對恢復(fù)出的歷史加密文檔,通過預(yù)設(shè)密碼庫(如公司舊密碼)嘗試解密,提升數(shù)據(jù)可用性。
安全審計(jì):批量檢測 PDF 密碼強(qiáng)度,發(fā)現(xiàn)可被簡單密碼破解的文件時(shí)提示弱密碼風(fēng)險(xiǎn),推動密碼加固。
示例C#代碼如下:
using Spire.Pdf;
public class PdfSecurityChecker
{
public static void Main()
{
// 掃描指定文件夾中的每一個(gè)PDF文檔
string folderPath = @"F:\PDFs\";
foreach (string file in Directory.GetFiles(folderPath, "*.pdf"))
{
// 驗(yàn)證PDF文檔是否加密
bool isProtected = PdfDocument.IsPasswordProtected(file);
Console.WriteLine($"{Path.GetFileName(file)} 是否加密: {isProtected}");
// 如果是加密的PDF文檔,逐一驗(yàn)證正確密碼
if (isProtected)
{
TestPasswords(file, new[] { "default", "company_secret", "temp_pwd", "user123", "abc"});
}
}
}
// 驗(yàn)證正確的密碼
private static void TestPasswords(string filePath, string[] passwords)
{
foreach (string pwd in passwords)
{
if (ValidatePdfPassword(filePath, pwd))
{
Console.WriteLine($" 驗(yàn)證成功!密碼: {pwd}");
return;
}
}
Console.WriteLine(" 所有密碼測試失??!");
}
static bool ValidatePdfPassword(string filePath, string password)
{
// 用測試密碼逐一打開PDF進(jìn)行驗(yàn)證
PdfDocument pdf = new PdfDocument();
try
{
pdf.LoadFromFile(filePath, password);
// 無異常 = 密碼正確
return true;
}
catch (Exception)
{
return false; // 密碼錯(cuò)誤
}
finally
{
pdf.Close();
}
}
}總結(jié)
本文詳細(xì)介紹了使用Free Spire.PDF庫通過C#檢測PDF加密狀態(tài)和測試密碼的實(shí)用技術(shù)。關(guān)鍵點(diǎn)包括:
通過 IsPasswordProtected 方法檢測加密狀態(tài)
使用 LoadFromFile 方法用密碼逐一加載PDF文檔來測試密碼有效性
實(shí)現(xiàn)對多個(gè)PDF文檔的批量處理
到此這篇關(guān)于C#如何快速檢測PDF是否加密并驗(yàn)證正確密碼的文章就介紹到這了,更多相關(guān)C#檢測PDF是否加密內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于C#實(shí)現(xiàn)自定義計(jì)算的Excel數(shù)據(jù)透視表
數(shù)據(jù)透視表(Pivot?Table)是一種數(shù)據(jù)分析工具,通常用于對大量數(shù)據(jù)進(jìn)行匯總、分析和展示,本文主要介紹了C#實(shí)現(xiàn)自定義計(jì)算的Excel數(shù)據(jù)透視表的相關(guān)知識,感興趣的可以了解下2023-12-12
c#判斷代碼是否執(zhí)行超時(shí)的幾種方式總結(jié)
WPF+WriteableBitmap實(shí)現(xiàn)高性能曲線圖的繪制

