Local file encrypt

From malue-ai/dazee-small · View interactive version

Local file encrypt is an AI agent skill from the GitHub repository malue-ai/dazee-small. Encrypt and decrypt local files using strong encryption. Protect sensitive documents with password-based AES-256 encryption, all processed locally.

How to use this skill

Skill file

本地文件加密

帮助用户加密和解密本地文件:使用 AES-256 强加密保护敏感文档,全程本地处理。

使用场景

依赖安装

首次使用时自动安装:

pip install cryptography

执行方式

通过 Python 使用 cryptography 库实现 AES-256 加密。

加密文件

import os
from cryptography.fernet import Fernet
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
import base64

def derive_key(password: str, salt: bytes) -> bytes:
    """From password derive encryption key."""
    kdf = PBKDF2HMAC(
        algorithm=hashes.SHA256(),
        length=32,
        salt=salt,
        iterations=480000,
    )
    return base64.urlsafe_b64encode(kdf.derive(password.encode()))

def encrypt_file(input_path: str, output_path: str, password: str):
    """Encrypt a file with password."""
    salt = os.urandom(16)
    key = derive_key(password, salt)
    f = Fernet(key)

    with open(input_path, 'rb') as file:
        data = file.read()

    encrypted = f.encrypt(data)

    with open(output_path, 'wb') as file:
        file.write(salt + encrypted)  # salt prepended

    print(f"Encrypted: {output_path} ({len(data)} -> {len(encrypted)+16} bytes)")

# Usage
encrypt_file("/path/to/secret.pdf", "/path/to/secret.pdf.enc", "user_password")

解密文件

def decrypt_file(input_path: str, output_path: str, password: str):
    """Decrypt a file with password."""
    with open(input_path, 'rb') as file:
        raw = file.read()

    salt = raw[:16]
    encrypted = raw[16:]
    key = derive_key(password, salt)
    f = Fernet(key)

    decrypted = f.decrypt(encrypted)

    with open(output_path, 'wb') as file:
        file.write(decrypted)

    print(f"Decrypted: {output_path} ({len(decrypted)} bytes)")

# Usage
decrypt_file("/path/to/secret.pdf.enc", "/path/to/secret.pdf", "user_password")

批量加密

import os

def encrypt_directory(dir_path: str, password: str):
    """Encrypt all files in a directory."""
    encrypted_dir = dir_path + "_encrypted"
    os.makedirs(encrypted_dir, exist_ok=True)

    count = 0
    for filename in os.listdir(dir_path):
        filepath = os.path.join(dir_path, filename)
        if os.path.isfile(filepath):
            encrypt_file(filepath, os.path.join(encrypted_dir, filename + ".enc"), password)
            count += 1

    print(f"Encrypted {count} files -> {encrypted_dir}")

安全规则

输出规范

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.