摘要:在当今的数字时代,PDF 文档已成为我们个人和职业生活中必不可少的一部分。从机密业务报告到敏感的个人记录,PDF 在存储和共享重要信息中无处不在。但是,随着数据泄露和未经授权访问的风险不断增加,确保这些 PDF 文件的安全性至关重要。
在当今的数字时代,PDF 文档已成为我们个人和职业生活中必不可少的一部分。从机密业务报告到敏感的个人记录,PDF 在存储和共享重要信息中无处不在。但是,随着数据泄露和未经授权访问的风险不断增加,确保这些 PDF 文件的安全性至关重要。
要在 Python 中加密和解密 PDF 文件,我们可以使用 Spire.PDF for Python,它是一个多功能库,旨在在 Python 应用程序中创建、读取、操作和转换 PDF 文件。
您可以使用以下 pip 命令从 PyPI 安装 Spire.PDF for Python:
pip install Spire.Pdf如果您已经安装了 Spire.PDF for Python 并希望升级到最新版本,请使用以下 pip 命令:
pip install --upgrade Spire.Pdf有关安装的更多详细信息,请查看此官方文档:如何为 Python 安装 Spire.PDF。
要加密 PDF,您需要设置打开和查看文件所需的用户密码(打开密码)。
什么是用户密码(open password)?
这是用户打开和查看 PDF 文档时必须输入的密码。它限制对 PDF 内容的访问,确保只有经过授权的个人才能访问该文件。用户密码除了授予打开 PDF 的权限外,不提供任何其他权限或限制。除了设置用户或打开密码外,您还需要设置将用于保护 PDF 内容的加密级别或算法。Spire.PDF for Python 支持以下加密级别或算法:
40 位 RC4128 位 RC4128 位 AES256 位 AES使用 Python 加密 PDF 文件的主要步骤如下:
创建一个 PdfDocument 实例并使用 PdfDocument.LoadFromFile 方法加载一个 PDF 文件。创建 PdfSecurityPolicy 并设置打开文档所需的用户密码。通过 PdfsecurityPolicy 类中的 EncryptionAlgorithm 属性设置用于保护 PDF 的加密算法。应用安全策略以使用 PdfDocument.Encrypt(securityPolicy:PdfsecurityPolicy) 方法加密 PDF 文档。将加密的 PDF 文档保存到新文件。以下代码示例演示如何使用 Python 加密 PDF 文件:
from spire.pdf.common import *from spire.pdf import *# Create a PdfDocument objectpdf = PdfDocument# Load an existing PDF File that needs to be encryptedpdf.LoadFromFile("Sample.pdf")# Create a security policy and set the user password required to open the documentsecurityPolicy = PdfPasswordSecurityPolicy("userpassword", str)# Specify the encryption algorithm to use for securing the PDFsecurityPolicy.EncryptionAlgorithm = PdfEncryptionAlgorithm.AES_256# Apply the security policy to encrypt the PDF documentpdf.Encrypt(securityPolicy)# Save the encrypted PDF document to a new filepdf.SaveToFile("Encrypt.pdf")pdf.Close使用 Python 加密或密码保护 PDF
要设置 PDF 文档的安全权限,除了用户密码外,您还需要设置所有者密码(权限密码)。
什么是所有者密码(权限密码)?
它用于控制可对 PDF 文档执行的权限和操作。它允许文档所有者限制某些操作,例如打印、复制、修改或提取 PDF 中的内容。所有者密码通常比用户密码更强大,因为它授予所有者对 PDF 安全性设置的完全控制权。使用 Python 在 PDF 中设置安全性权限的主要步骤如下:
创建一个 PdfDocument 实例并使用 PdfDocument.LoadFromFile 方法加载一个 PDF 文件。创建 PdfSecurityPolicy 并设置用户密码以打开文档,并设置所有者密码以限制权限。通过 PdfSecurityPolicy 类中的 EncryptionAlgorithm 属性设置用于保护 PDF 的加密算法。限制所有权限,然后仅允许通过 PdfSecurityPolicy 类中的 DocumentPrivilege 属性获得某些权限。应用安全策略以使用 PdfDocument.Encrypt(securityPolicy:PdfSecurityPolicy) 方法加密 PDF 文档。将加密的 PDF 文档保存到新文件。以下代码示例显示如何使用 Python 在 PDF 中设置安全权限:
from spire.pdf.common import *from spire.pdf import *# Create a PdfDocument objectpdf = PdfDocument# Load an existing PDF file that needs to be encryptedpdf.LoadFromFile("Sample.pdf")# Create a security policy and set the user password to open the document and the owner password to restrict permissionssecurityPolicy = PdfPasswordSecurityPolicy("userpassword", "ownerpassword")# Set the encryption algorithm securityPolicy.EncryptionAlgorithm = PdfEncryptionAlgorithm.AES_256# Restrict all permissionssecurityPolicy.DocumentPrivilege = PdfDocumentPrivilege.ForbidAll# Only allow printing of the documentsecurityPolicy.DocumentPrivilege.AllowPrint = True# Encrypt all document content except metadata (optional, it allows search engines to access the document’s metadata)# securityPolicy.EncryptMetadata = False# Apply the security policy to encrypt the PDF documentpdf.Encrypt(securityPolicy)# Save the encrypted PDF document to a new filepdf.SaveToFile("EncryptWithPermissions.pdf")pdf.Close使用 Python 在 PDF 中设置安全权限
加密的 PDF 文件可以使用其用户口令或所有者口令打开。打开后,您可以使用 PdfDocument.Decrypt 方法对其进行解密。
使用 Python 解密加密的 PDF 文件的主要步骤如下:
创建一个 PdfDocument 实例,并使用 PdfDocument.LoadFromFile 方法使用其用户密码或所有者密码加载加密的 PDF 文件。调用 PdfDocument.Decrypt 方法解密 PDF 文件。将解密的 PDF 文件存储到新文件。以下代码示例演示如何使用 Python 解密加密的 PDF 文件:
from spire.pdf.common import *from spire.pdf import *# Create a PdfDocument objectpdf = PdfDocument# Load an encrypted PDF document with either its user password or owner password pdf.LoadFromFile("EncryptWithPermissions.pdf", "userpassword")# Decrypt the PDF documentpdf.Decrypt("ownerpassword")# Save the resulting document to a new filepdf.SaveToFile("Decrypt.pdf")pdf.Close来源:自由坦荡的湖泊AI