搜尋
首頁後端開發php教程如何在 AES 加密字串中新增和刪除 PKCS7 填充?

How to Add and Remove PKCS7 Padding from AES-Encrypted Strings?

如何從 AES 加密字串新增/刪除 PKCS7 填充?

在密碼學中,PKCS7 填充用於透過向字串添加額外位元組來確保敏感資料的機密性。訊息結束。此填充允許加密資料是所使用的加密演算法的區塊大小的倍數,通常是 128 位元模式下的 AES(高級加密標準)。

了解 PKCS7 填充

PKCS7 填充遵循特定演算法:

  1. 計算填充訊息所需的位元組數為區塊大小的倍數。
  2. 將該位元組數附加到訊息末尾。
  3. 將每個填充位元組設定為填充位元組數的值。

新增PKCS7 填充

要新增PKCS7 填充,您可以按照以下操作步驟:

import base64
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad

# Sample data to encrypt
data = "Hello, World!"

# Define the AES key (128 bits = 16 bytes)
key = 'YOUR_AES_KEY_16_BYTES_LONG'

# Initialization vector (IV) for ECB mode is not used and ignored
iv = '0' * 16

# Create an AES cipher in ECB mode
cipher = AES.new(key, AES.MODE_ECB)

# Pad the data before encryption
padded_data = pad(data, 16)  # 16 is the block size for AES-128

# Encrypt the padded data
ciphertext = cipher.encrypt(padded_data)

# Encode the ciphertext in base64 for transmission
ciphertext_base64 = base64.b64encode(ciphertext).decode('utf-8')

# Print the encrypted and base64-encoded ciphertext
print(ciphertext_base64)

刪除PKCS7 填充

刪除PKCS7 填充
import base64
from Crypto.Cipher import AES
from Crypto.Util.Padding import unpad

# Sample base64-encoded ciphertext to decrypt
ciphertext_base64 = 'ENCRYPTED_AND_BASE64_ENCODED_DATA'

# Decode the base64 ciphertext
ciphertext = base64.b64decode(ciphertext_base64)

# Define the AES key (128 bits = 16 bytes)
key = 'YOUR_AES_KEY_16_BYTES_LONG'

# Initialization vector (IV) for ECB mode is not used and ignored
iv = '0' * 16

# Create an AES cipher in ECB mode
cipher = AES.new(key, AES.MODE_ECB)

# Decrypt the ciphertext
decrypted = cipher.decrypt(ciphertext)

# Remove the padding from the decrypted data
data = unpad(decrypted, 16)

# Print the decrypted and unpadded data
print(data.decode('utf-8'))
要刪除PKCS7填充,您可以按照以下步驟操作:

以上是如何在 AES 加密字串中新增和刪除 PKCS7 填充?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
unset()和session_destroy()有什麼區別?unset()和session_destroy()有什麼區別?May 04, 2025 am 12:19 AM

Thedifferencebetweenunset()andsession_destroy()isthatunset()clearsspecificsessionvariableswhilekeepingthesessionactive,whereassession_destroy()terminatestheentiresession.1)Useunset()toremovespecificsessionvariableswithoutaffectingthesession'soveralls

在負載平衡的情況下,什麼是粘性會話(會話親和力)?在負載平衡的情況下,什麼是粘性會話(會話親和力)?May 04, 2025 am 12:16 AM

stickysessensureuserRequestSarerOutedTothesMeServerForsessionDataConsisterency.1)sessionIdentificeAssificationAssigeaSsignAssignSignSuserServerServerSustersusiseCookiesorUrlModifications.2)一致的ententRoutingDirectSsssssubsequeSssubsequeSubsequestrequestSameSameserver.3)loadBellankingDisteributesNebutesneNewuserEreNevuseRe.3)

PHP中有哪些不同的會話保存處理程序?PHP中有哪些不同的會話保存處理程序?May 04, 2025 am 12:14 AM

phpoffersvarioussessionsionsavehandlers:1)文件:默認,簡單的ButMayBottLeneckonHigh-trafficsites.2)Memcached:高性能,Idealforsforspeed-Criticalapplications.3)REDIS:redis:similartomemememememcached,withddeddeddedpassistence.4)withddeddedpassistence.4)databases:gelifforcontrati forforcontrati,有用

PHP中的會話是什麼?為什麼使用它們?PHP中的會話是什麼?為什麼使用它們?May 04, 2025 am 12:12 AM

PHP中的session是用於在服務器端保存用戶數據以在多個請求之間保持狀態的機制。具體來說,1)session通過session_start()函數啟動,並通過$_SESSION超級全局數組存儲和讀取數據;2)session數據默認存儲在服務器的臨時文件中,但可通過數據庫或內存存儲優化;3)使用session可以實現用戶登錄狀態跟踪和購物車管理等功能;4)需要注意session的安全傳輸和性能優化,以確保應用的安全性和效率。

說明PHP會話的生命週期。說明PHP會話的生命週期。May 04, 2025 am 12:04 AM

PHPsessionsstartwithsession_start(),whichgeneratesauniqueIDandcreatesaserverfile;theypersistacrossrequestsandcanbemanuallyendedwithsession_destroy().1)Sessionsbeginwhensession_start()iscalled,creatingauniqueIDandserverfile.2)Theycontinueasdataisloade

絕對會話超時有什麼區別?絕對會話超時有什麼區別?May 03, 2025 am 12:21 AM

絕對會話超時從會話創建時開始計時,閒置會話超時則從用戶無操作時開始計時。絕對會話超時適用於需要嚴格控制會話生命週期的場景,如金融應用;閒置會話超時適合希望用戶長時間保持會話活躍的應用,如社交媒體。

如果會話在服務器上不起作用,您將採取什麼步驟?如果會話在服務器上不起作用,您將採取什麼步驟?May 03, 2025 am 12:19 AM

服務器會話失效可以通過以下步驟解決:1.檢查服務器配置,確保會話設置正確。 2.驗證客戶端cookies,確認瀏覽器支持並正確發送。 3.檢查會話存儲服務,如Redis,確保其正常運行。 4.審查應用代碼,確保會話邏輯正確。通過這些步驟,可以有效診斷和修復會話問題,提升用戶體驗。

session_start()函數的意義是什麼?session_start()函數的意義是什麼?May 03, 2025 am 12:18 AM

session_start()iscucialinphpformanagingusersessions.1)ItInitiateSanewsessionifnoneexists,2)resumesanexistingsessions,and3)setsasesessionCookieforContinuityActinuityAccontinuityAcconActInityAcconActInityAcconAccRequests,EnablingApplicationsApplicationsLikeUseAppericationLikeUseAthenticationalticationaltication and PersersonalizedContentent。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

SecLists

SecLists

SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版