使用對稱金鑰進行安全加密
Python 中安全加密的建議方法是使用加密庫中的 Fernet 配方。它採用 AES CBC 加密和 HMAC 進行完整性驗證,有效保護資料免遭篡改和未經授權的解密。
Fernet 加解密
<code class="python">from cryptography.fernet import Fernet # Generate a secret key for encryption key = Fernet.generate_key() # Encode a message (plaintext) encoded_message = Fernet(key).encrypt(b"John Doe") # Decode the encrypted message (ciphertext) decoded_message = Fernet(key).decrypt(encoded_message) print(decoded_message.decode()) # Output: John Doe</code>
密碼衍生的Fernet 金鑰
雖然建議使用隨機產生的金鑰以確保安全,但如有必要,您也可以從密碼派生金鑰:
<code class="python">from cryptography.fernet import Fernet from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC from cryptography.hazmat.backends import default_backend from cryptography.hazmat.primitives import hashes def derive_key(password): kdf = PBKDF2HMAC( algorithm=hashes.SHA256(), length=32, salt=secrets.token_bytes(16), iterations=100_000, backend=default_backend() ) return b64e(kdf.derive(password.encode())) # Generate a password using a key derivation function key = derive_key(password) # Encrypt and decrypt using the password-derived Fernet key encoded_message = Fernet(key).encrypt(b"John Doe") decoded_message = Fernet(key).decrypt(encoded_message) print(decoded_message.decode()) # Output: John Doe</code>
隱藏資料
對於非敏感數據,請考慮使用base64編碼而不是加密:
<code class="python">from base64 import urlsafe_b64encode as b64e # Encode data encoded_data = b64e(b"Hello world!") # Decode data decoded_data = b64d(encoded_data) print(decoded_data) # Output: b'Hello world!'</code>
對資料進行簽名
使用HMAC 對資料進行簽署以確保完整性:
<code class="python">import hmac import hashlib # Sign data using a secret key key = secrets.token_bytes(32) signature = hmac.new(key, b"Data to sign", hashlib.sha256).digest() # Verify the signature def verify(data, signature, key): expected = hmac.new(key, data, hashlib.sha256).digest() return hmac.compare_digest(expected, signature) # Verify the signature using the same key print(verify(b"Data to sign", signature, key)) # Output: True</code>
其他:不安全方案的正確實作
AES CFB:
<code class="python">import secrets from base64 import urlsafe_b64encode as b64e, urlsafe_b64decode as b64d from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes from cryptography.hazmat.backends import default_backend backend = default_backend() def aes_cfb_encrypt(message, key): algorithm = algorithms.AES(key) iv = secrets.token_bytes(algorithm.block_size // 8) cipher = Cipher(algorithm, modes.CFB(iv), backend=backend) encryptor = cipher.encryptor() return b64e(iv + encryptor.update(message) + encryptor.finalize()) def aes_cfb_decrypt(ciphertext, key): iv_ciphertext = b64d(ciphertext) algorithm = algorithms.AES(key) size = algorithm.block_size // 8 iv, encrypted = iv_ciphertext[:size], iv_ciphertext[size:] cipher = Cipher(algorithm, modes.CFB(iv), backend=backend) decryptor = cipher.decryptor() return decryptor.update(encrypted) + decryptor.finalize()</code>
AES ECB:
<code class="python">from base64 import urlsafe_b64encode as b64e, urlsafe_b64decode as b64d from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes from cryptography.hazmat.primitives import padding from cryptography.hazmat.backends import default_backend backend = default_backend() def aes_ecb_encrypt(message, key): cipher = Cipher(algorithms.AES(key), modes.ECB(), backend=backend) encryptor = cipher.encryptor() padder = padding.PKCS7(cipher.algorithm.block_size).padder() padded_message = padder.update(message.encode()) + padder.finalize() return b64e(encryptor.update(padded_message) + encryptor.finalize()) def aes_ecb_decrypt(ciphertext, key): cipher = Cipher(algorithms.AES(key), modes.ECB(), backend=backend) decryptor = cipher.decryptor() unpadder = padding.PKCS7(cipher.algorithm.block_size).unpadder() padded_message = decryptor.update(b64d(ciphertext)) + decryptor.finalize() return unpadder.update(padded_message) + unpadder.finalize()</code>
注意: AES ECB 不是建議用於安全加密。
以上是如何確保Python中的安全加密?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

每天學習Python兩個小時是否足夠?這取決於你的目標和學習方法。 1)制定清晰的學習計劃,2)選擇合適的學習資源和方法,3)動手實踐和復習鞏固,可以在這段時間內逐步掌握Python的基本知識和高級功能。

Python在Web開發中的關鍵應用包括使用Django和Flask框架、API開發、數據分析與可視化、機器學習與AI、以及性能優化。 1.Django和Flask框架:Django適合快速開發複雜應用,Flask適用於小型或高度自定義項目。 2.API開發:使用Flask或DjangoRESTFramework構建RESTfulAPI。 3.數據分析與可視化:利用Python處理數據並通過Web界面展示。 4.機器學習與AI:Python用於構建智能Web應用。 5.性能優化:通過異步編程、緩存和代碼優

Python在開發效率上優於C ,但C 在執行性能上更高。 1.Python的簡潔語法和豐富庫提高開發效率。 2.C 的編譯型特性和硬件控制提升執行性能。選擇時需根據項目需求權衡開發速度與執行效率。

Python在現實世界中的應用包括數據分析、Web開發、人工智能和自動化。 1)在數據分析中,Python使用Pandas和Matplotlib處理和可視化數據。 2)Web開發中,Django和Flask框架簡化了Web應用的創建。 3)人工智能領域,TensorFlow和PyTorch用於構建和訓練模型。 4)自動化方面,Python腳本可用於復製文件等任務。

Python在數據科學、Web開發和自動化腳本領域廣泛應用。 1)在數據科學中,Python通過NumPy、Pandas等庫簡化數據處理和分析。 2)在Web開發中,Django和Flask框架使開發者能快速構建應用。 3)在自動化腳本中,Python的簡潔性和標準庫使其成為理想選擇。

Python的靈活性體現在多範式支持和動態類型系統,易用性則源於語法簡潔和豐富的標準庫。 1.靈活性:支持面向對象、函數式和過程式編程,動態類型系統提高開發效率。 2.易用性:語法接近自然語言,標準庫涵蓋廣泛功能,簡化開發過程。

Python因其簡潔與強大而備受青睞,適用於從初學者到高級開發者的各種需求。其多功能性體現在:1)易學易用,語法簡單;2)豐富的庫和框架,如NumPy、Pandas等;3)跨平台支持,可在多種操作系統上運行;4)適合腳本和自動化任務,提升工作效率。

可以,在每天花費兩個小時的時間內學會Python。 1.制定合理的學習計劃,2.選擇合適的學習資源,3.通過實踐鞏固所學知識,這些步驟能幫助你在短時間內掌握Python。


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

Dreamweaver Mac版
視覺化網頁開發工具

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。