久久精品人人爽,华人av在线,亚洲性视频网站,欧美专区一二三

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

共計(jì) 2947 個(gè)字符,預(yù)計(jì)需要花費(fèi) 8 分鐘才能閱讀完成。

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

  1. 對(duì)稱(chēng)加密:使用相同的密鑰進(jìn)行加密和解密。常見(jiàn)的對(duì)稱(chēng)加密算法有 AES、DES 等。可以使用 cryptography 庫(kù)來(lái)實(shí)現(xiàn)對(duì)稱(chēng)加密,具體方法如下:
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)

# 將加密文件寫(xiě)入新文件
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)

# 將解密文件寫(xiě)入新文件
with open('decrypted.txt', 'wb') as file:
    file.write(decrypted_text)
  1. 非對(duì)稱(chēng)加密:使用公鑰進(jìn)行加密,私鑰進(jìn)行解密。常見(jiàn)的非對(duì)稱(chēng)加密算法有 RSA 等。可以使用 cryptography 庫(kù)來(lái)實(shí)現(xiàn)非對(duì)稱(chēng)加密,具體方法如下:
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import rsa, padding
from cryptography.hazmat.backends import default_backend

# 生成 RSA 密鑰對(duì)
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
    )
)

# 將加密文件寫(xiě)入新文件
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
    )
)

# 將解密文件寫(xiě)入新文件
with open('decrypted.txt', 'wb') as file:
    file.write(decrypted_text)
  1. 文件壓縮加密:先對(duì)文件進(jìn)行壓縮,然后再進(jìn)行加密。可以使用 zipfile 庫(kù)進(jìn)行文件壓縮,再結(jié)合對(duì)稱(chēng)或非對(duì)稱(chēng)加密方法進(jìn)行加密。具體方法如下:
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()
    
# 使用對(duì)稱(chēng)加密
key = Fernet.generate_key()
cipher = Fernet(key)
encrypted_text = cipher.encrypt(plaintext)

# 將加密文件寫(xiě)入新文件
with open('encrypted.zip', 'wb') as file:
    file.write(encrypted_text)

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

# 使用對(duì)稱(chēng)解密
decrypted_text = cipher.decrypt(encrypted_text)

# 將解密文件寫(xiě)入新文件
with open('decrypted.zip', 'wb') as file:
    file.write(decrypted_text)

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

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

丸趣 TV 網(wǎng) – 提供最優(yōu)質(zhì)的資源集合!

正文完
 
丸趣
版權(quán)聲明:本站原創(chuàng)文章,由 丸趣 2023-12-16發(fā)表,共計(jì)2947字。
轉(zhuǎn)載說(shuō)明:除特殊說(shuō)明外本站除技術(shù)相關(guān)以外文章皆由網(wǎng)絡(luò)搜集發(fā)布,轉(zhuǎn)載請(qǐng)注明出處。
評(píng)論(沒(méi)有評(píng)論)
主站蜘蛛池模板: 黑龙江省| 武威市| 清流县| 万年县| 雷山县| 舟曲县| 英德市| 深水埗区| 寿宁县| 涪陵区| 万源市| 徐水县| 关岭| 长垣县| 元谋县| 桂阳县| 马尔康县| 庆阳市| 沙河市| 湘潭县| 五常市| 钟山县| 安吉县| 宽甸| 香格里拉县| 银川市| 噶尔县| 栖霞市| 鄱阳县| 揭东县| 民乐县| 游戏| 中宁县| 石泉县| 安溪县| 长治市| 河池市| 安陆市| 卫辉市| 金沙县| 图木舒克市|