在线精品99_中国九九盗摄偷拍偷看_91免费版在线观看_91.app_91高清视频在线_99热最新网站

python文件加密与解密的方法是什么

167次阅读
没有评论

共计 2947 个字符,预计需要花费 8 分钟才能阅读完成。

Python 文件加密与解密的方法可以使用以下几种方式:

  1. 对称加密:使用相同的密钥进行加密和解密。常见的对称加密算法有 AES、DES 等。可以使用 cryptography 库来实现对称加密,具体方法如下:
from cryptography.fernet import Fernet

# 生成密钥
key = Fernet.generate_key()

# 创建加密器
cipher = Fernet(key)

# 加密文件
with open('plain.txt', 'rb') as file:
    plaintext = file.read()
encrypted_text = cipher.encrypt(plaintext)

# 将加密文件写入新文件
with open('encrypted.txt', 'wb') as file:
    file.write(encrypted_text)

# 解密文件
with open('encrypted.txt', 'rb') as file:
    encrypted_text = file.read()
decrypted_text = cipher.decrypt(encrypted_text)

# 将解密文件写入新文件
with open('decrypted.txt', 'wb') as file:
    file.write(decrypted_text)
  1. 非对称加密:使用公钥进行加密,私钥进行解密。常见的非对称加密算法有 RSA 等。可以使用 cryptography 库来实现非对称加密,具体方法如下:
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import rsa, padding
from cryptography.hazmat.backends import default_backend

# 生成 RSA 密钥对
private_key = rsa.generate_private_key(public_exponent=65537,
    key_size=2048,
    backend=default_backend())
public_key = private_key.public_key()

# 保存私钥到文件
with open('private_key.pem', 'wb') as file:
    file.write(
        private_key.private_bytes(
            encoding=serialization.Encoding.PEM,
            format=serialization.PrivateFormat.PKCS8,
            encryption_algorithm=serialization.NoEncryption())
    )

# 保存公钥到文件
with open('public_key.pem', 'wb') as file:
    file.write(
        public_key.public_bytes(
            encoding=serialization.Encoding.PEM,
            format=serialization.PublicFormat.SubjectPublicKeyInfo
        )
    )

# 加密文件
with open('plain.txt', 'rb') as file:
    plaintext = file.read()
encrypted_text = public_key.encrypt(
    plaintext,
    padding.OAEP(mgf=padding.MGF1(algorithm=hashes.SHA256()),
        algorithm=hashes.SHA256(),
        label=None
    )
)

# 将加密文件写入新文件
with open('encrypted.txt', 'wb') as file:
    file.write(encrypted_text)

# 解密文件
with open('encrypted.txt', 'rb') as file:
    encrypted_text = file.read()
decrypted_text = private_key.decrypt(
    encrypted_text,
    padding.OAEP(mgf=padding.MGF1(algorithm=hashes.SHA256()),
        algorithm=hashes.SHA256(),
        label=None
    )
)

# 将解密文件写入新文件
with open('decrypted.txt', 'wb') as file:
    file.write(decrypted_text)
  1. 文件压缩加密:先对文件进行压缩,然后再进行加密。可以使用 zipfile 库进行文件压缩,再结合对称或非对称加密方法进行加密。具体方法如下:
import zipfile
import shutil
from cryptography.fernet import Fernet

# 压缩文件
shutil.make_archive('archive', 'zip', '.', 'plain.txt')

# 加密压缩文件
with open('archive.zip', 'rb') as file:
    plaintext = file.read()
    
# 使用对称加密
key = Fernet.generate_key()
cipher = Fernet(key)
encrypted_text = cipher.encrypt(plaintext)

# 将加密文件写入新文件
with open('encrypted.zip', 'wb') as file:
    file.write(encrypted_text)

# 解密文件
with open('encrypted.zip', 'rb') as file:
    encrypted_text = file.read()

# 使用对称解密
decrypted_text = cipher.decrypt(encrypted_text)

# 将解密文件写入新文件
with open('decrypted.zip', 'wb') as file:
    file.write(decrypted_text)

# 解压缩文件
with zipfile.ZipFile('decrypted.zip', 'r') as zip_ref:
    zip_ref.extractall('.')

注意:在加密和解密文件时,需要确保密钥的安全

丸趣 TV 网 – 提供最优质的资源集合!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-12-16发表,共计2947字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)
主站蜘蛛池模板: 久久婷婷国产麻豆91天堂 | 毛片无码免费无码播放 | 鲁一鲁一鲁一鲁一曰综合网 | 精品日韩在线观看 | 亚洲国产精品日韩av不卡在线 | 一级视频在线观看 | 人妻少妇精品无码专区动漫 | 久久久精品国产免费观看同学 | 欧美精品一区二区蜜臀亚洲 | 欧美成人精品欧美一级乱黄 | 国产免费又色又爽又黄软件 | 国产精品高清视亚洲精品 | 亚洲熟妇自偷自拍另欧美 | 亚洲区欧美区 | 黑人巨大精品欧美一区二区免费 | 欧洲第一页 | 国产真人无遮挡作爱免费视频 | 羞羞一区二区三区四区片 | 18禁无遮挡羞羞污污污污免费 | 免费中国一级啪啪片 | 偷拍自拍在线观看 | 日本在线播放一区 | 久久婷婷五月综合色欧美 | 中文字幕一区二区三区四区五区 | 人人妻一区二区三区 | 无码av中文一区二区三区桃花岛 | 午夜啪视频| katsumi精品作品在线播放 | 色婷婷亚洲一区二区三区 | 我想看毛片 | 日韩手机在线视频 | 日本黄色免费看 | 国产精品99久久不卡 | 亚洲免费在线看 | 亚洲男人的天堂网站 | 97涩涩涩 | 亚洲在线观看网站 | 自拍偷在线精品自拍偷无码专区 | 国产麻豆剧传媒精品网站 | 国产精品三级av及在线观看 | 韩国精品一区二区三区无码视频 |