跳至页脚内容
PYTHON 帮助

加密 Python(开发人员如何使用)

Cryptography 是数字时代数据和通信安全的关键。 这个包,其广泛的库,使加密技术的实施变得简单。 Python中最受欢迎的加密库之一是cryptography包,它提供了同时具备高层和低层接口的加密解决方案。 Later in the article, we will also look into a versatile PDF generation library called IronPDF from Iron Software.

关键特性

  1. 高级方案:Cryptography包含一个高级加密方案层,用于常见的加密任务,如对称加密、对称密码、消息摘要和密钥导出功能。 高级对称加密方案帮助快速且简单地实施复杂的算法。
  2. 低层接口:它还提供加密算法的低层接口,允许更细粒度的控制和自定义。
  3. 对称和非对称加密:该库支持常见的加密算法,包括对称加密(如AES)和非对称加密(如RSA)算法。
  4. 加密原语:加密标准库向Python开发者提供加密方案和原语,其中包括散列、密钥导出和消息认证码(MACs)等原语。
  5. 开发者支持:开发者可以提交问题报告,同时也提供用于开发讨论的邮件列表。

安装

要安装cryptography包,可以使用pip:

pip install cryptography
pip install cryptography
SHELL

基本用法

这里是一个使用Fernet模块进行对称加密的简单示例:

from cryptography.fernet import Fernet

# Generate a key
key = Fernet.generate_key()
cipher_suite = Fernet(key)

# Encrypt a message
message = b"IronPDF is awesome"  # This must be a really secret message
cipher_text = cipher_suite.encrypt(message)
print(cipher_text)

# Decrypt the message
plain_text = cipher_suite.decrypt(cipher_text)
print(plain_text)
from cryptography.fernet import Fernet

# Generate a key
key = Fernet.generate_key()
cipher_suite = Fernet(key)

# Encrypt a message
message = b"IronPDF is awesome"  # This must be a really secret message
cipher_text = cipher_suite.encrypt(message)
print(cipher_text)

# Decrypt the message
plain_text = cipher_suite.decrypt(cipher_text)
print(plain_text)
PYTHON

在这个例子中,我们生成一个密钥,加密一条消息,然后使用Fernet模块解密它。

输出

cryptography Python (开发人员如何操作): 图 1

用例

  1. 数据加密:在将敏感数据存储到数据库或通过网络传输之前对其加密。
  2. 安全通信:确保各方之间交换的信息是机密且防篡改的。
  3. 身份验证:使用加密签名验证数据的完整性和真实性。

IronPDF 简介

cryptography Python (开发人员如何操作): 图 2 - IronPDF: The Python PDF Library

IronPDF 是一个强大的Python库,设计用于使用HTML、CSS、图像和JavaScript来创建、编辑和签署PDF,支持现代网络标准。 它提供商业级性能,具有低内存占用。 关键特性包括:

HTML到PDF转换: IronPDF可以将HTML文件、HTML字符串和URL转换为PDF。 例如,使用Chrome PDF渲染器将网页渲染为PDF。

跨平台支持: IronPDF设计用于Python 3+,也可以运行在Windows、Mac、Linux或云平台上。
IronPDF is also available in .NET, Java, Python, and Node.js.

编辑和签署: 使用IronPDF设置属性、通过密码和权限添加安全性,并将数字签名应用到您的PDF中。

页面模板和设置: 您可以使用IronPDF自定义PDF的页眉、页脚、页码和可调整的边距。 它还支持自定义纸张大小和响应式布局。

标准合规: IronPDF符合PDF标准,包括PDF/A和PDF/UA,支持UTF-8字符编码,并管理如图像、CSS和字体的资源。

安装

 pip install ironpdf

使用IronPDF和Cryptography生成PDF文档。

前提条件

  1. 确保安装了Visual Studio Code
  2. 安装了Python 3版本

首先,让我们创建一个Python文件以添加我们的脚本。

打开Visual Studio Code并创建一个文件,cryptographyDemo.py

安装必要的库:

pip install cryptography
pip install ironpdf
pip install cryptography
pip install ironpdf
SHELL

然后添加以下代码来演示IronPDF和cryptography Python包的使用:

from cryptography.fernet import Fernet
from ironpdf import ChromePdfRenderer, License

# Apply your license key
License.LicenseKey = "your key"

# Create a PDF from an HTML string using Python
content = "<h1>Awesome IronPDF with Cryptography</h1>"

# Generate a key
content += "<h2>Generate a key</h2>"
key = Fernet.generate_key()
cipher_suite = Fernet(key)
content += "<p>Fernet.generate_key() = " + str(key) + "</p>"

# Encrypt a message
content += "<h2>Encrypt a message</h2>"
message = b"IronPDF is awesome"
cipher_text = cipher_suite.encrypt(message)
content += "<p>cipher_suite.encrypt(message)</p>"
content += "<p>" + str(cipher_text) + "</p>"

# Decrypt the message
content += "<h2>Decrypt the message</h2>"
plain_text = cipher_suite.decrypt(cipher_text)
content += "<p>cipher_suite.decrypt(cipher_text)</p>"
content += "<p>" + plain_text.decode() + "</p>"

# Generate PDF using IronPDF
renderer = ChromePdfRenderer()
pdf = renderer.RenderHtmlAsPdf(content)

# Export to a file or Stream
pdf.SaveAs("Demo-cryptography.pdf")
from cryptography.fernet import Fernet
from ironpdf import ChromePdfRenderer, License

# Apply your license key
License.LicenseKey = "your key"

# Create a PDF from an HTML string using Python
content = "<h1>Awesome IronPDF with Cryptography</h1>"

# Generate a key
content += "<h2>Generate a key</h2>"
key = Fernet.generate_key()
cipher_suite = Fernet(key)
content += "<p>Fernet.generate_key() = " + str(key) + "</p>"

# Encrypt a message
content += "<h2>Encrypt a message</h2>"
message = b"IronPDF is awesome"
cipher_text = cipher_suite.encrypt(message)
content += "<p>cipher_suite.encrypt(message)</p>"
content += "<p>" + str(cipher_text) + "</p>"

# Decrypt the message
content += "<h2>Decrypt the message</h2>"
plain_text = cipher_suite.decrypt(cipher_text)
content += "<p>cipher_suite.decrypt(cipher_text)</p>"
content += "<p>" + plain_text.decode() + "</p>"

# Generate PDF using IronPDF
renderer = ChromePdfRenderer()
pdf = renderer.RenderHtmlAsPdf(content)

# Export to a file or Stream
pdf.SaveAs("Demo-cryptography.pdf")
PYTHON

代码解释

这段代码片段演示如何使用cryptography库的Fernet模块执行消息的加密和解密,然后使用IronPDF生成PDF文档。 以下是代码每个部分的说明:

  1. 导入和许可证密钥设置

    • cryptography.fernet模块导入Fernet类以进行加密和解密功能。
    • 从IronPDF导入ChromePdfRendererLicense用于PDF生成。
    • 设置IronPDF的许可证密钥以启用其功能。
  2. HTML内容设置:使用将被包含在PDF文档中的HTML标记初始化content变量。

  3. 生成一个密钥:使用Fernet.generate_key()生成一个新密钥并使用生成的密钥创建一个Fernet密码套件对象(cipher_suite)。 在HTML内容中包含生成的密钥。

  4. 加密一条信息:定义一个要加密的消息(message) (b"IronPDF is awesome")。 使用cipher_suite.encrypt()方法加密消息,并在HTML内容中包含密文。

  5. 解密一条信息:使用cipher_suite.decrypt()解密加密的cipher_text,并在HTML内容中包含解密的明文。

  6. PDF生成:使用ChromePdfRenderercontent HTML字符串渲染为PDF文档。 将生成的PDF文件保存为“Demo-cryptography.pdf”。

该设置允许创建一个展示cryptography库提供的加密和解密功能以及IronPDF的PDF生成能力的PDF文档。

输出

cryptography Python (开发人员如何操作): 图 3

PDF

cryptography Python (开发人员如何操作): 图 4

IronPDF 许可证

IronPDF提供试用许可证密钥,允许用户在购买前查看其广泛的功能。

在开始使用IronPDF包之前,将许可证密钥置于脚本的开始处:

from ironpdf import License

# Apply your license key
License.LicenseKey = "key"
from ironpdf import License

# Apply your license key
License.LicenseKey = "key"
PYTHON

结论

Python中的cryptography库是实现安全数据加密和解密的强大工具。 其易用性和全面功能对希望增强其应用程序安全性的开发者来说是一个绝佳的选择。

另一方面,IronPDF是一个多功能且功能丰富的PDF生成库,将以标准方式帮助记录结果。 这两个库都可以为开发者带来巨大的好处,提升他们的技能。

Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。