您现在的位置是:网站首页> 编程资料编程资料
python密码学各种加密模块教程_python_
2023-05-26
411人已围观
简介 python密码学各种加密模块教程_python_
在本章中,您将详细了解Python中各种加密模块.
加密模块
它包含所有配方和基元,并在Python中提供高级编码接口.您可以使用以下命令安装加密模块 :
pip install cryptography

代码
您可以使用以下代码实现加密模块 :
from cryptography.fernet import Fernet key = Fernet.generate_key() cipher_suite = Fernet(key) cipher_text = cipher_suite.encrypt("This example is used to demonstrate cryptography module") plain_text = cipher_suite.decrypt(cipher_text)输出
上面给出的代码产生以下输出 :

此处给出的代码用于验证密码并创建其哈希值.
它还包括用于验证密码以进行身份验证的逻辑.
import uuid import hashlib def hash_password(password): # uuid is used to generate a random number of the specified password salt = uuid.uuid4().hex return hashlib.sha256(salt.encode() + password.encode()).hexdigest() + ':' + salt def check_password(hashed_password, user_password): password, salt = hashed_password.split(':') return password == hashlib.sha256(salt.encode() + user_password.encode()).hexdigest() new_pass = input('Please enter a password: ') hashed_password = hash_password(new_pass) print('The string to store in the db is: ' + hashed_password) old_pass = input('Now please enter the password again to check: ') if check_password(hashed_password, old_pass): print('You entered the right password') else: print('Passwords do not match')输出
场景1 : 如果您输入了正确的密码,您可以找到以下输出 :

情景2 : 如果我们输入错误的密码,您可以找到以下输出 :

说明
Hashlib 包用于在数据库中存储密码.在此程序中,使用 salt ,在实现哈希函数之前,将随机序列添加到密码字符串中.
以上就是python密码学各种加密模块教程的详细内容,更多关于Python密码学加密模块的资料请关注其它相关文章!
您可能感兴趣的文章:
相关内容
- python密码学简单替代密码解密及测试教程_python_
- Python实现Harbor私有镜像仓库垃圾自动化清理详情_python_
- 利用Python绘制好看的玫瑰花图_python_
- Python密码学仿射密码及攻击单字母密码教程_python_
- Python协程实践分享_python_
- Python密码学XOR算法编码流程及乘法密码教程_python_
- Python日志模块logging的使用方法总结_python_
- python密码学Base64编码和解码教程_python_
- Python自动化办公之定时发送邮件的实现_python_
- Python初识二叉树续之实战binarytree_python_
