
在python中,常用的数据加密和解密方法有以下几种:
示例代码:
立即学习“Python免费学习笔记(深入)”;
import hashlib # 加密数据 data = "Hello World" hashed_data = hashlib.sha256(data.encode()).hexdigest() print(hashed_data) # 解密数据 # 由于哈希算法是单向的,无法逆向解密,只能通过对比哈希值来验证数据的一致性
示例代码:
立即学习“Python免费学习笔记(深入)”;
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)
示例代码:
立即学习“Python免费学习笔记(深入)”;
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)
以上就是python数据加密和解密的方法是什么的详细内容,更多请关注php中文网其它相关文章!
python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号