In python, the commonly used data encryption and decryption methods are as follows:
Sample code:
import hashlib # 加密数据 data = "Hello World" hashed_data = hashlib.sha256(data.encode()).hexdigest() print(hashed_data) # 解密数据 # 由于哈希算法是单向的,无法逆向解密,只能通过对比哈希值来验证数据的一致性
Sample code:
import base64 # 加密数据 data = "Hello World" encoded_data = base64.b64encode(data.encode()).decode() print(encoded_data) # 解密数据 decoded_data = base64.b64decode(encoded_data).decode() print(decoded_data)
Sample code:
from cryptography.fernet import Fernet # 生成密钥 key = Fernet.generate_key() # 加密数据 cipher_suite = Fernet(key) data = "Hello World" encrypted_data = cipher_suite.encrypt(data.encode()).decode() print(encrypted_data) # 解密数据 decrypted_data = cipher_suite.decrypt(encrypted_data.encode()).decode() print(decrypted_data)
The above is the detailed content of What is the method of data encryption and decryption in python. For more information, please follow other related articles on the PHP Chinese website!