macOS下实现文件加密的示例代码与解决方案

作者:佚名 上传时间:2023-11-28 运行软件:macOS 软件版本:Python 3.x, cryptography库版本 3.x 版权申诉

本示例演示在macOS上使用Python实现文件加密和解密的功能。通过使用cryptography库,可以轻松实现对敏感文件的保护,确保数据的安全性。

from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
import os

def encrypt_file(password, input_file, output_file):
    # 通过PBKDF2HMAC获取密钥
    kdf = PBKDF2HMAC(
        algorithm=hashes.SHA256(),
        iterations=100000,
        salt=os.urandom(16),
        length=32,
        backend=default_backend()
    )
    key = kdf.derive(password.encode())

    # 生成随机的IV(Initialization Vector)
    iv = os.urandom(16)

    # 使用AES-GCM加密文件
    cipher = Cipher(algorithms.AES(key), modes.GCM(iv), backend=default_backend())
    encryptor = cipher.encryptor()

    with open(input_file, 'rb') as file_in:
        with open(output_file, 'wb') as file_out:
            file_out.write(iv)
            for chunk in iter(lambda: file_in.read(4096), b''):
                encrypted_chunk = encryptor.update(chunk)
                file_out.write(encrypted_chunk)

def decrypt_file(password, input_file, output_file):
    with open(input_file, 'rb') as file_in:
        # 从文件中读取IV
        iv = file_in.read(16)

        # 通过PBKDF2HMAC获取密钥
        kdf = PBKDF2HMAC(
            algorithm=hashes.SHA256(),
            iterations=100000,
            salt=os.urandom(16),
            length=32,
            backend=default_backend()
        )
        key = kdf.derive(password.encode())

        # 使用AES-GCM解密文件
        cipher = Cipher(algorithms.AES(key), modes.GCM(iv), backend=default_backend())
        decryptor = cipher.decryptor()

        with open(output_file, 'wb') as file_out:
            for chunk in iter(lambda: file_in.read(4096), b''):
                decrypted_chunk = decryptor.update(chunk)
                file_out.write(decrypted_chunk)

# 使用示例
encrypt_file("my_secret_password", "sensitive_data.txt", "encrypted_data.bin")
decrypt_file("my_secret_password", "encrypted_data.bin", "decrypted_data.txt")

免责申明:文章和图片全部来源于公开网络,如有侵权,请通知删除 server@dude6.com

用户评论
相关推荐
macOS
本示例演示如何在macOS环境下使用Python编写一个简单的文件加密与解密工具。通过使用AES加密算法,用户可以轻松地保护敏感文件的安全性。该示例还包括错误处理机制,以确保程序在出现异常情况下能够
Python 3.x, pycryptodome库
PyCharm
2023-11-28 23:13
macOS
本示例演示在macOS上使用Python实现文件加密和解密的功能。通过使用cryptography库,可以轻松实现对敏感文件的保护,确保数据的安全性。from cryptography.hazmat
Python 3.x, cryptography库版本 3.x
macOS
2023-11-28 03:59
macOS简易
本示例演示在macOS环境下使用Python实现文件加密和解密的简单方法,通过密码学技术确保文件安全性。使用AES加密算法进行加密,通过密码将文件加密保存,再通过密码解密还原文件内容。from Cr
Python 3.x, pycryptodome库
Visual Studio Code
2023-11-12 21:20
macOS
本文介绍在macOS上使用Python实现文件加密的解决方案,通过AES算法对文件进行加密和解密。这个方案可以用于保护用户的敏感文件,确保数据安全性。from Crypto.Cipher impor
Python 3.x, pycryptodome库
PyCharm
2023-11-22 11:57
FreeBSD系统
在FreeBSD环境中,通过GEOM框架和GELI工具,可以实现对文件系统的加密。这提供了一种保护敏感数据的有效方法,确保数据在磁盘上存储时得到加密保护。# 在终端中执行以下命令安装所需工具sud
FreeBSD 12.2-RELEASE
FreeBSD核心团队
2023-12-04 00:00
macOS技术
本示例展示了在macOS上使用Python语言实现文件加密与解密的方法,采用AES算法确保数据安全。通过使用Crypto库,实现了简单而强大的文件加密与解密功能,代码注释详细解释了每个步骤。from
Python 3.8.5,Crypto 1.4.1
Visual Studio Code
2023-11-24 00:52
macOS及技术
本示例演示如何在macOS环境下使用Python编写一个简单的文件加密与解密工具。通过AES加密算法,用户可以轻松地保护敏感文件的安全性。from Crypto.Cipher import AES
Python 3.x, pycryptodome库
Python
2023-11-18 00:48
z/OS环境
本示例演示了如何在z/OS操作系统环境下使用IBM Z硬件加速功能进行文件的加密和解密。通过调用IBM CPACF(Central Processor Assist for Cryptographic
z/OS V2.5
IBM Assembler
2023-11-26 15:49
macOS环境技术
本示例展示了在macOS操作系统下使用Python语言实现文件加密与解密的功能。通过AES加密算法,确保文件的安全性。同时提供了密码输入机制以及错误处理,以提高用户体验。from Crypto.Ci
Python 3.8.5, pycryptodome 3.9.9
Visual Studio Code
2023-12-04 08:09
PythonAES
本示例演示了使用Python中的cryptography库进行文件加密的方法,采用高级加密标准(AES)算法。AES是一种对称加密算法,广泛用于保护敏感信息的安全传输。示例代码中通过密码学安全的方式实
cryptography 3.4.8
Python 3.9
2023-11-16 22:48