跳過到頁腳內容
PYTHON 幫助

cryptography python(開發人員工作原理)

加密技術 是在數位時代中保護資料和通訊的必要工具。 這個套件及其豐富的函式庫,使得實現加密技術變得容易。 Python 中最受歡迎的加密技術庫之一是 cryptography 套件,它提供高階和低階介面的加密配方。 Later in the article, we will also look into a versatile PDF generation library called IronPDF from Iron Software.

主要特點

  1. 高階配方:加密技術包含一個高階的加密配方層,用於常見的加密任務,例如對稱加密、對稱密碼、訊息摘要和密鑰導出函數。 高階的對稱加密配方幫助快速且簡單地實現複雜的演算法。
  2. 低階介面:它還提供對加密算法的低階介面,允許更細粒度的控制和自定義。
  3. 對稱和非對稱加密:該庫支持常見的加密算法,包括對稱加密(例如 AES)和非對稱加密(例如 RSA)算法。
  4. 加密基元:加密標準庫包含加密配方和基元,提供給 Python 開發者,包括用於散列、密鑰導出和訊息驗證碼(MACs)的基元。
  5. 開發者支援:開發者可以提交問題報告,且該庫還提供開發討論的郵件列表。

安裝

要安裝加密技術套件,你可以使用 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:Python 的PDF 庫

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 和加密技術的 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 內容設置:初始化 content 變數,包含要包含在 PDF 文件中的 HTML 標記。

  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 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。