电脑文件夹保护全攻略:6大使用代码加密方法,涵盖Python与BAT脚本及详尽教程

方法1:使用PowerShell加密文件夹

  1. 安装AES加密模块:
    在PowerShell中运行以下命令:
Install-Module -Name AES
  1. 创建加密文件夹的脚本:
    将以下代码保存为EncryptFolder.ps1
param(
    [string]$folderPath,
    [string]$password
)

Import-Module AES

Get-ChildItem $folderPath -Recurse | ForEach-Object {
    
    
    if (!$_.PSIsContainer) {
    
    
        $encryptedFile = $_.FullName + ".aes"
        Protect-AesFile -Path $_.FullName -Destination $encryptedFile -Password $password
        Remove-Item $_.FullName
    }
}
  1. 使用脚本加密文件夹:
    在PowerShell中运行以下命令:
.\EncryptFolder.ps1 -folderPath "C:\path\to\your\folder" -password "your_password"

请替换相应的文件夹路径和密码。

方法2:使用Python加密文件夹

  1. 安装cryptography库:
    在命令行中运行以下命令
pip install cryptography
  1. 创建加密文件夹的脚本:
    将以下代码保存为encrypt_folder.py
import os
import sys
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.primitives import padding
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
from cryptography.hazmat.primitives import hashes, hmac
from cryptography.hazmat.backends import default_backend

def encrypt_file(file_path, password):
    with open(file_path, "rb") as file:
        data = file.read()

    salt = os.urandom(16)
    kdf = PBKDF2HMAC(
        algorithm=hashes.SHA256(),
        length=32,
        salt=salt,
        iterations=100000,
        backend=default_backend()
    )
    key = kdf.derive(password.encode())

    cipher = Cipher(algorithms.AES(key), modes.CBC(salt), backend=default_backend())
    encryptor = cipher.encryptor()

    padder = padding.PKCS7(128).padder()
    padded_data = padder.update(data) + padder.finalize()
    encrypted_data = encryptor.update(padded_data) + encryptor.finalize()

    with open(file_path + ".enc", "wb") as encrypted_file:
        encrypted_file.write(salt + encrypted_data)

    os.remove(file_path)

def encrypt_folder(folder_path, password):
    for root, dirs, files in os.walk(folder_path):
        for file in files:
            file_path = os.path.join(root, file)
            encrypt_file(file_path, password)

if __name__ == "__main__":
    folder_path = sys.argv[1]
    password = sys.argv[2]
    encrypt_folder(folder_path, password)
  1. 使用脚本加密文件夹:
    在命令行中运行以下命令
python encrypt_folder.py "C:\path\to\your\folder" "your_password"

请替换相应的文件夹路径和密码。

方法3:使用Python和PyCryptoDome库加密文件夹

  1. 安装PyCryptoDome库
    在命令行中运行以下命令
pip install pycryptodome
  1. 创建加密文件夹的脚本
    将以下代码保存为encrypt_folder_pycryptodome.py
import os
import sys
from base64 import b64encode, b64decode
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
from Crypto.Random import get_random_bytes
from Crypto.Protocol.KDF import scrypt

def encrypt_file(file_path, password):
    with open(file_path, "rb") as file:
        data = file.read()

    salt = get_random_bytes(16)
    key = scrypt(password.encode(), salt, 32, N=2**14, r=8, p=1)

    cipher = AES.new(key, AES.MODE_GCM)
    encrypted_data, tag = cipher.encrypt_and_digest(data)

    with open(file_path + ".enc", "wb") as encrypted_file:
        encrypted_file.write(salt + cipher.nonce + tag + encrypted_data)

    os.remove(file_path)

def encrypt_folder(folder_path, password):
    for root, dirs, files in os.walk(folder_path):
        for file in files:
            file_path = os.path.join(root, file)
            encrypt_file(file_path, password)

if __name__ == "__main__":
    folder_path = sys.argv[1]
    password = sys.argv[2]
    encrypt_folder(folder_path, password)
  1. 使用脚本加密文件夹
    在命令行中运行以下命令
python encrypt_folder_pycryptodome.py "C:\path\to\your\folder" "your_password"

请替换相应的文件夹路径和密码。

方法4:使用Python和Fernet加密文件夹

  1. 安装cryptography库
    在命令行中运行以下命令
pip install cryptography
  1. 创建加密文件夹的脚本
    将以下代码保存为encrypt_folder_fernet.py
import os
import sys
from cryptography.fernet import Fernet
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.backends import default_backend
from base64 import urlsafe_b64encode

def generate_key(password, salt):
    kdf = PBKDF2HMAC(
        algorithm=hashes.SHA256(),
        length=32,
        salt=salt,
        iterations=100000,
        backend=default_backend()
    )
    key = urlsafe_b64encode(kdf.derive(password.encode()))
    return key

def encrypt_file(file_path, password):
    with open(file_path, "rb") as file:
        data = file.read()

    salt = os.urandom(16)
    key = generate_key(password, salt)
    cipher = Fernet(key)

    encrypted_data = cipher.encrypt(data)
    with open(file_path + ".enc", "wb") as encrypted_file:
        encrypted_file.write(salt + encrypted_data)

    os.remove(file_path)

def encrypt_folder(folder_path, password):
    for root, dirs, files in os.walk(folder_path):
        for file in files:
            file_path = os.path.join(root, file)
            encrypt_file(file_path, password)

if __name__ == "__main__":
    folder_path = sys.argv[1]
    password = sys.argv[2]
    encrypt_folder(folder_path, password)
  1. 使用脚本加密文件夹
    在命令行中运行以下命令
python encrypt_folder_fernet.py "C:\path\to\your\folder" "your_password"

请替换相应的文件夹路径和密码。

方法5:使用PowerShell和AES加密文件夹

  1. 创建加密文件夹的PowerShell脚本
    将以下代码保存为encrypt_folder_aes.ps1
param (
    [string]$folderPath,
    [string]$password
)

function Encrypt-File($file, $password) {
    
    
    $key = (New-Object Security.Cryptography.Rfc2898DeriveBytes $password, 16, 1000).GetBytes(32)
    $aes = New-Object Security.Cryptography.AesCryptoServiceProvider
    $aes.Key = $key
    $aes.IV = New-Object Byte[] 16
    $aes.Mode = [Security.Cryptography.CipherMode]::CBC
    $aes.Padding = [Security.Cryptography.PaddingMode]::PKCS7

    $content = Get-Content -Path $file -Encoding Byte
    $encrypted = $aes.CreateEncryptor().TransformFinalBlock($content, 0, $content.Length)

    Set-Content -Path ($file + ".enc") -Value $encrypted -Encoding Byte
    Remove-Item -Path $file
}

Get-ChildItem -Path $folderPath -Recurse -File | ForEach-Object {
    
    
    Encrypt-File -file $_.FullName -password $password
}
  1. 使用PowerShell脚本加密文件夹
    在PowerShell中运行以下命令
.\encrypt_folder_aes.ps1 -folderPath "C:\path\to\your\folder" -password "your_password"

方法6:使用CMD批处理加密文件夹

使用批处理隐藏文件夹

打开记事本,然后复制并粘贴以下代码

@echo off
set /p "dir=请输入文件夹路径: "
attrib +h +s "%dir%"
echo 文件夹已隐藏
pause
  1. 将文件另存为 .bat 格式,例如:hide_folder.bat。

  2. 双击运行该批处理文件。根据提示输入要隐藏的文件夹路径,然后按回车。这将隐藏指定的文件夹。

  3. 要取消隐藏文件夹,请创建另一个批处理文件,如下所示:

打开记事本,然后复制并粘贴以下代码

@echo off
set /p "dir=请输入文件夹路径: "
attrib -h -s "%dir%"
echo 文件夹已取消隐藏
pause
  1. 将文件另存为 .bat 格式,例如:unhide_folder.bat。
  2. 双击运行该批处理文件。根据提示输入要取消隐藏的文件夹路径,然后按回车。这将取消隐藏指定的文件夹。

使用批处理锁定文件夹

打开记事本,然后复制并粘贴以下代码

@echo off
set /p "dir=请输入文件夹路径: "
attrib +s +h +r "%dir%"
echo 文件夹已锁定
pause
  1. 将文件另存为 .bat 格式,例如:lock_folder.bat。
  2. 双击运行该批处理文件。根据提示输入要锁定的文件夹路径,然后按回车。这将锁定指定的文件夹,使其成为只读、隐藏且系统文件夹。
  3. 要解锁文件夹,请创建另一个批处理文件,如下所示:

打开记事本,然后复制并粘贴以下代码

@echo off
set /p "dir=请输入文件夹路径: "
attrib -s -h -r "%dir%"
echo 文件夹已解锁
pause
  1. 将文件另存为 .bat 格式,例如:unlock_folder.bat。
  2. 双击运行该批处理文件。根据提示输入要解锁的文件夹路径,然后按回车。这将解锁指定的文件夹,使其变为可读、可见且非系统文件夹。

请注意,这些方法不能阻止有高级技能的用户访问您的文件。要实现更高级别的加密和保护,请考虑使用其他加密工具。

猜你喜欢

转载自blog.csdn.net/tuzajun/article/details/130539065