117.info
人生若只如初见

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

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('.')

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

未经允许不得转载 » 本文链接:https://www.117.info/ask/fec09AzsLBQZQDVY.html

推荐文章

  • linux的python如何进行进程管理

    在Linux中,可以使用Python的内置库os和subprocess进行进程管理 使用os.system()执行外部命令: import os # 执行外部命令,例如:ls
    os.system("ls") 使用...

  • linux的python怎样进行系统调用

    在Linux中,Python可以通过os和sys库进行系统调用 使用os库: os库提供了许多与操作系统交互的函数。例如,要使用os.system()执行系统命令,可以这样做:
    i...

  • python中set怎样进行集合排序

    在Python中,可以使用sorted()函数对集合(set)进行排序。sorted()函数返回一个排序后的列表(list),而不是集合,因为集合本身是无序的。以下是一个示例:

  • python中set如何进行集合转换

    在Python中,可以使用集合(set)的内置方法进行集合转换。以下是一些常用的集合转换方法: 将列表转换为集合: my_list = [1, 2, 3, 4, 5]
    my_set = set(m...

  • Java用scanner输入卡住了怎么办

    当Java中的Scanner输入卡住时,通常是因为等待输入的操作没有完成。这可能是因为输入缓冲区中没有足够的数据可供读取,或者在读取输入之前没有调用Scanner的相应...

  • docker修改端口号的步骤是什么

    要修改Docker容器的端口号,需要以下步骤: 停止并删除现有的容器:使用docker stop 命令停止正在运行的容器,然后使用docker rm 命令删除容器。如果不删除容器,...

  • linux怎么进入docker容器

    要进入Docker容器,可以使用docker exec命令。以下是进入Docker容器的步骤: 确保已经安装了Docker并且Docker守护进程正在运行。
    打开终端或命令行窗口。

  • java中package的用法是什么

    在Java中,package是用来组织代码的一种方式。它可以定义一个命名空间,用于将相关的类或接口组织在一起,防止命名冲突。
    package的用法如下: 定义package...