Các mô-đun được mô tả trong chương này thực hiện các thuật toán khác nhau có tính chất mật mã. Chúng có sẵn theo quyết định của việc cài đặt. Trên các hệ thống Unix, mô-đun
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]
3 cũng có thể có sẵn. Dưới đây là tổng quan
4 — Băm an toàn và tóm tắt thông báofrom 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]
- thuật toán băm
- Thông báo chiều dài thay đổi của SHAKE
- băm tập tin
- dẫn xuất chính
- BLACK2
- Tạo đối tượng băm
- hằng số
- ví dụ
- băm đơn giản
- Sử dụng các kích thước tiêu hóa khác nhau
- băm có khóa
- băm ngẫu nhiên
- Cá nhân hóa
- chế độ cây
- Tín dụng
5 — Băm khóa để xác thực thư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]
6 — Tạo số ngẫu nhiên an toàn để quản lý bí mậtfrom 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]
- Số ngẫu nhiên
- Tạo mã thông báo
- Mã thông báo nên sử dụng bao nhiêu byte?
- Cac chưc năng khac
- Công thức nấu ăn và thực hành tốt nhất
Trong hướng dẫn này, bạn sẽ học cách mã hóa và giải mã dữ liệu, e. g. 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 thông tin đó. Nó cho phép chúng tôi bảo vệ an toàn dữ liệu mà chúng tôi không muốn bất kỳ ai xem hoặc truy cập
Có liên quan
- 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ể được 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 dựa trên thuật toán AES
Mã hóa dữ liệu trong Python
Đầ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 tôi cần nhập
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]
0 và bắt đầu tạo khóa - khóa này cần thiết để mã hóa/giải mã đối xứngTạo khóa
Để tạo khóa, chúng tôi gọi phương thức
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]
1from 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
Ghi chú. Bạn cần giữ khóa này ở nơi an toàn. Nếu mất khóa, bạn sẽ không thể giải mã dữ liệu đã được mã hóa bằng khóa này.
Nạp chìa khóa
Khi chúng tôi đã tạo 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 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 tin nhắn
- 2 - khởi tạo lớp Fernet
- 3 - chuyển tin nhắn đã mã hóa sang phương thức
2from 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]
mã hóa tin nhắn
message = "message I want to encrypt".encode[]
khởi tạo lớp Fernet
f = Fernet[key]
mã hóa tin nhắn
________số 8Ví dụ về mã đầy đủ
Dưới đây là một ví dụ hoạt động đầy đủ về mã hóa tin nhắn trong python
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]
def load_key[]:
"""
Load the previously generated key
"""
return open["secret.key", "rb"].read[]
def encrypt_message[message]:
"""
Encrypts a message
"""
key = load_key[]
encoded_message = message.encode[]
f = Fernet[key]
encrypted_message = f.encrypt[encoded_message]
print[encrypted_message]
if __name__ == "__main__":
encrypt_message["encrypt this message"]
đầu ra
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]
0Giải mã dữ liệu bằng Python
Để giải mã tin nhắn, chúng ta chỉ cần gọi phương thức
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]
3 từ thư viện 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]
0. Hãy nhớ rằng, chúng tôi cũng cần tải khóa vì khóa cần thiết để giải mã tin nhắn