Mã hóa là một chủ đề khó khăn trong bất kỳ ngôn ngữ nào. Tốt hơn là chọn một thư viện với bảo trì tích cực. Có 2 thư viện mã hóa Python tốt nhất như sau.best Python encryption libraries as following.
Mật mã
Pycryptodome includes both high level recipes and low level interfaces to common cryptographic algorithms such as symmetric ciphers, message digests, and key derivation functions.
Cryptography bao gồm cả công thức nấu ăn cấp cao và giao diện cấp thấp với các thuật toán mật mã phổ biến như mật mã đối xứng, tiêu hóa tin nhắn và các hàm dẫn xuất chính.
from cryptography.fernet import Fernet key = Fernet.generate_key[] f = Fernet[key] encrypted_text = f.encrypt[b"This is a secret text message that needs to be hidden."] decrypted_text = f.decrypt[encrypted_text ]
Mã hóa và giải mã văn bản với Cryptography Công thức mã hóa đối xứng cấp cao:
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes import os key = os.urandom[32] iv = os.urandom[16] cipher = Cipher[algorithms.AES[key], modes.CBC[iv]] encryptor = cipher.encryptor[] ct = encryptor.update[b"This is a secret text"] + encryptor.finalize[] decryptor = cipher.decryptor[] plain_text = decryptor.update[ct] + decryptor.finalize[]
Pycryptodome
Cryptography bao gồm cả công thức nấu ăn cấp cao và giao diện cấp thấp với các thuật toán mật mã phổ biến như mật mã đối xứng, tiêu hóa tin nhắn và các hàm dẫn xuất chính. is a self-contained Python package of low-level cryptographic primitives. It is a fork of PyCrypto, which brings a lot of enhancements with respect to the last official version of PyCrypto [2.6.1].
Mã hóa và giải mã văn bản với Cryptography Công thức mã hóa đối xứng cấp cao:
Mã hóa và giải mã bằng cách sử dụng AES
from Crypto.Random import get_random_bytes key = get_random_bytes[16] cipher = AES.new[key, AES.MODE_EAX] ciphertext, tag = cipher.encrypt_and_digest[data] file_out = open["encrypted.bin", "wb"] [ file_out.write[x] for x in [cipher.nonce, tag, ciphertext] ] file_out.close[]
Pycryptodome là một gói Python khép kín của các nguyên thủy mật mã cấp thấp. Đây là một ngã ba của Pycrypto, mang lại nhiều cải tiến liên quan đến phiên bản chính thức cuối cùng của Pycrypto [2.6.1].
from Crypto.PublicKey import RSA secret_code = "Unguessable" key = RSA.generate[2048] encrypted_key = key.export_key[passphrase=secret_code, pkcs=8, protection="scryptAndAES128-CBC"] file_out = open["rsa_key.bin", "wb"] file_out.write[encrypted_key] file_out.close[] print[key.publickey[].export_key[]]
© Bản quyền 2001-2022, Quỹ phần mềm Python. Trang này được cấp phép theo giấy phép nền tảng phần mềm Python Phiên bản 2. Ví dụ, công thức nấu ăn và mã khác trong tài liệu được cấp phép bổ sung theo giấy phép BSD của mệnh đề 0. Xem lịch sử và giấy phép để biết thêm thông tin.
This page is licensed under the Python Software Foundation License Version 2.
Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
See History and License for more information.
Quỹ phần mềm Python là một tập đoàn phi lợi nhuận. Xin hãy đóng góp.
Cập nhật lần cuối vào ngày 26 tháng 11 năm 2022. Tìm thấy một lỗi? Được tạo bằng Sphinx 4.5.0.
Created using Sphinx 4.5.0.
Trong hướng dẫn này, bạn sẽ tìm hiểu cách mã hóa và giải mã dữ liệu, ví dụ: Một chuỗi văn bản sử dụng thư viện mật mã trong Python.
Mã hóa là quá trình mã hóa thông tin theo cách mà chỉ các bên được ủy quyền mới có thể truy cập vào nó. Nó cho phép chúng tôi bảo vệ dữ liệu một cách an toàn mà chúng tôi không muốn bất cứ ai nhìn thấy hoặc truy cập.
- Khái niệm cơ bản về mật mã
Trong ví dụ này, chúng tôi sẽ sử dụng mã hóa đối xứng, có nghĩa là cùng một khóa mà chúng tôi sử dụng để mã hóa dữ liệu, cũng có thể sử dụng để giải mã.
Thư viện mật mã mà chúng tôi sử dụng ở đây được xây dựng trên thuật toán AES.
Đầu tiên, chúng ta cần cài đặt thư viện mật mã:
pip3 install cryptography
Từ thư viện mật mã, chúng ta cần nhập
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes import os key = os.urandom[32] iv = os.urandom[16] cipher = Cipher[algorithms.AES[key], modes.CBC[iv]] encryptor = cipher.encryptor[] ct = encryptor.update[b"This is a secret text"] + encryptor.finalize[] decryptor = cipher.decryptor[] plain_text = decryptor.update[ct] + decryptor.finalize[]4 và bắt đầu tạo khóa - khóa này được yêu cầu để mã hóa/giải mã đối xứng.
Tạo khóa
Để tạo khóa, chúng tôi gọi phương thức
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes import os key = os.urandom[32] iv = os.urandom[16] cipher = Cipher[algorithms.AES[key], modes.CBC[iv]] encryptor = cipher.encryptor[] ct = encryptor.update[b"This is a secret text"] + encryptor.finalize[] decryptor = cipher.decryptor[] plain_text = decryptor.update[ct] + decryptor.finalize[]5:
from cryptography.fernet import Fernet
def generate_key[]:
"""
Generates a key and save it into a file
"""
key = Fernet.generate_key[]
with open["secret.key", "wb"] as key_file:
key_file.write[key]
Chúng ta chỉ cần thực hiện phương thức trên một lần để tạo khóa.
Tải chìa khóa
Khi chúng tôi đã tạo một khóa, chúng tôi cần tải khóa trong phương thức của mình để mã hóa dữ liệu:
def load_key[]:
"""
Loads the key named `secret.key` from the current directory.
"""
return open["secret.key", "rb"].read[]
Mã hóa một tin nhắn
Bây giờ, chúng tôi đã sẵn sàng để mã hóa một tin nhắn. Đây là một quá trình ba bước:
- 1 - Mã hóa thông báo
- 2 - Khởi tạo lớp Fernet
- 3 - Chuyển thông báo được mã hóa sang phương thức
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes import os key = os.urandom[32] iv = os.urandom[16] cipher = Cipher[algorithms.AES[key], modes.CBC[iv]] encryptor = cipher.encryptor[] ct = encryptor.update[b"This is a secret text"] + encryptor.finalize[] decryptor = cipher.decryptor[] plain_text = decryptor.update[ct] + decryptor.finalize[]
6
mã hóa thông báo:
message = "message I want to encrypt".encode[]
Khởi tạo lớp Fernet:
f = Fernet[key]
Mã hóa tin nhắn:
encrypted_message = f.encrypt[message]
Ví dụ đầy đủ mã
Dưới đây là một ví dụ hoạt động đầy đủ về việc mã hóa một thông báo trong Python:
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes import os key = os.urandom[32] iv = os.urandom[16] cipher = Cipher[algorithms.AES[key], modes.CBC[iv]] encryptor = cipher.encryptor[] ct = encryptor.update[b"This is a secret text"] + encryptor.finalize[] decryptor = cipher.decryptor[] plain_text = decryptor.update[ct] + decryptor.finalize[]0
Output:
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes import os key = os.urandom[32] iv = os.urandom[16] cipher = Cipher[algorithms.AES[key], modes.CBC[iv]] encryptor = cipher.encryptor[] ct = encryptor.update[b"This is a secret text"] + encryptor.finalize[] decryptor = cipher.decryptor[] plain_text = decryptor.update[ct] + decryptor.finalize[]1
Giải mã dữ liệu trong Python
Để giải mã tin nhắn, chúng tôi chỉ gọi phương thức
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes import os key = os.urandom[32] iv = os.urandom[16] cipher = Cipher[algorithms.AES[key], modes.CBC[iv]] encryptor = cipher.encryptor[] ct = encryptor.update[b"This is a secret text"] + encryptor.finalize[] decryptor = cipher.decryptor[] plain_text = decryptor.update[ct] + decryptor.finalize[]7 từ thư viện
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes import os key = os.urandom[32] iv = os.urandom[16] cipher = Cipher[algorithms.AES[key], modes.CBC[iv]] encryptor = cipher.encryptor[] ct = encryptor.update[b"This is a secret text"] + encryptor.finalize[] decryptor = cipher.decryptor[] plain_text = decryptor.update[ct] + decryptor.finalize[]4. Hãy nhớ rằng, chúng tôi cũng cần phải tải chìa khóa, bởi vì cần thiết để giải mã tin nhắn.
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes import os key = os.urandom[32] iv = os.urandom[16] cipher = Cipher[algorithms.AES[key], modes.CBC[iv]] encryptor = cipher.encryptor[] ct = encryptor.update[b"This is a secret text"] + encryptor.finalize[] decryptor = cipher.decryptor[] plain_text = decryptor.update[ct] + decryptor.finalize[]2
Output:
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes import os key = os.urandom[32] iv = os.urandom[16] cipher = Cipher[algorithms.AES[key], modes.CBC[iv]] encryptor = cipher.encryptor[] ct = encryptor.update[b"This is a secret text"] + encryptor.finalize[] decryptor = cipher.decryptor[] plain_text = decryptor.update[ct] + decryptor.finalize[]3