溫馨提示×

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

小億
141
2023-11-04 21:39:45
欄目: 編程語言

Python文件加密與解密的方法可以使用以下幾種方式:

  1. 對稱加密:使用相同的密鑰進行加密和解密。常見的對稱加密算法有AES、DES等。可以使用cryptography庫來實現(xiàn)對稱加密,具體方法如下:
from cryptography.fernet import Fernet

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

# 創(chuàng)建加密器
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等??梢允褂?code>cryptography庫來實現(xiàn)非對稱加密,具體方法如下:
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. 文件壓縮加密:先對文件進行壓縮,然后再進行加密??梢允褂?code>zipfile庫進行文件壓縮,再結(jié)合對稱或非對稱加密方法進行加密。具體方法如下:
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('.')

注意:在加密和解密文件時,需要確保密鑰的安全

0