当前位置:   article > 正文

Python文件操作和I/O流:读写文件、处理数据的实用技巧_python io 文件流操作

python io 文件流操作

密码学作为计算机科学中的重要分支,负责研究安全通信和数据保护的方法。在现代信息社会中,随着数据传输和存储需求的不断增加,密码学变得愈发重要。本文将深入探讨Python中常用的密码学库及其应用,包括数据加密、数字签名和其他安全协议的实现和应用。

数据加密

1.1 对称加密算法

对称加密算法使用相同的密钥进行加密和解密,是最快速、最简单的加密形式之一。Python中常用的对称加密算法有AES(Advanced Encryption Standard)。

from Crypto.Cipher import 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(b'Hello, AES!')  
  
# 解密  
cipher = AES.new(key, AES.MODE_EAX, nonce=cipher.nonce)  
plaintext = cipher.decrypt_and_verify(ciphertext, tag)  

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
1.2 非对称加密算法

非对称加密算法使用一对密钥:公钥和私钥。公钥用于加密,私钥用于解密。Python中常用的非对称加密算法有RSA。

from Crypto.PublicKey import RSA  
from Crypto.Cipher import PKCS1_OAEP  
  
# 生成密钥对  
key = RSA.generate(2048)  
  
# 加密  
cipher = PKCS1_OAEP.new(key.publickey())  
ciphertext = cipher.encrypt(b'Hello, RSA!')  
  
# 解密  
cipher = PKCS1_OAEP.new(key)  
plaintext = cipher.decrypt(ciphertext)  

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

数字签名

数字签名用于确保数据的完整性和来源可信。常用的数字签名算法包括RSA和DSA(Digital Signature Algorithm)。

from Crypto.Signature import DSS  
from Crypto.Hash import SHA256  
  
# 生成密钥对  
key = RSA.generate(2048)  
  
# 签名  
h = SHA256.new(b'Hello, Digital Signature!')  
signature = DSS.new(key, 'fips-186-3').sign(h)  
  
# 验证  
verifier = DSS.new(key.publickey(), 'fips-186-3')  
try:  
    verifier.verify(h, signature)  
    print("Signature is valid.")  
except ValueError:  
    print("Signature is invalid.")  

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

安全协议

3.1 TLS/SSL

TLS(Transport Layer Security)和其前身SSL(Secure Sockets Layer)是用于保护网络通信的协议。Python中的ssl模块提供了TLS/SSL的支持。

import ssl  
import socket  
  
# 创建安全连接  
context = ssl.create_default_context()  
with socket.create_connection(('www.example.com', 443)) as sock:  
    with context.wrap_socket(sock, server_hostname='www.example.com') as ssock:  
        ssock.sendall(b'GET / HTTP/1.1\r\nHost: www.example.com\r\n\r\n')  
        response = ssock.recv(4096)  
        print(response.decode())  

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
3.2 OAuth

OAuth是一种授权框架,用于允许第三方应用访问用户在另一个服务提供商上存储的私有资源,而无需共享用户的凭据。Python中的oauthlib库提供了OAuth的实现。

from flask import Flask  
from authlib.integrations.flask_client import OAuth  
  
app = Flask(__name__)  
oauth = OAuth(app)  
  
# 添加OAuth提供商  
oauth.register('example',  
    authorize_url='https://example.com/oauth/authorize',  
    authorize_params=None,  
    authorize_params_hook=None,  
    fetch_token=None,  
    fetch_token_params=None,  
    fetch_token_params_hook=None,  
    refresh_token=None,  
    refresh_token_params=None,  
    refresh_token_params_hook=None,  
    client_kwargs=None,  
    client_id=None,  
    client_secret=None,  
    client=WebApplicationClient,  
    client_kwargs_hook=None  
)  
  
# 使用OAuth进行认证  
@app.route('/login')  
def login():  
    redirect_uri = url_for('auth', _external=True)  
    return oauth.example.authorize_redirect(redirect_uri)  
  
@app.route('/auth')  
def auth():  
    token = oauth.example.authorize_access_token()  
    user = oauth.example.parse_id_token(token)  
    # 处理用户信息  
    return 'Authenticated as ' + user['sub']  

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
3.3 Hash算法在密码存储中的应用

存储密码时,通常不会直接存储明文密码,而是存储密码的哈希值。Python的hashlib库提供了多种哈希算法,可以用于密码的安全存储。

import hashlib  
  
def hash_password(password, salt):  
    hashed_password = hashlib.pbkdf2_hmac('sha256', password.encode('utf-8'), salt, 100000)  
    return hashed_password  
  
def verify_password(stored_password, input_password, salt):  
    input_hashed_password = hash_password(input_password, salt)  
    return input_hashed_password == stored_password  

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

以上就是“Python文件操作和I/O流:读写文件、处理数据的实用技巧”的全部内容,希望对你有所帮助。

关于Python技术储备

学好 Python 不论是就业还是做副业赚钱都不错,但要学会 Python 还是要有一个学习规划。最后大家分享一份全套的 Python 学习资料,给那些想学习 Python 的小伙伴们一点帮助!

一、Python所有方向的学习路线

Python所有方向的技术点做的整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。

在这里插入图片描述

二、Python必备开发工具

img

三、Python视频合集

观看零基础学习视频,看视频学习是最快捷也是最有效果的方式,跟着视频中老师的思路,从基础到深入,还是很容易入门的。

img

四、实战案例

光学理论是没用的,要学会跟着一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。

img

五、Python练习题

检查学习结果。

img

六、面试资料

我们学习Python必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有阿里大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。

img

最后祝大家天天进步!!

上面这份完整版的Python全套学习资料已经上传至CSDN官方,朋友如果需要可以直接微信扫描下方CSDN官方认证二维码免费领取【保证100%免费】。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/927186
推荐阅读
相关标签
  

闽ICP备14008679号