一、Python加密方法
AES加密(推荐)
使用`cryptography`库实现AES加密,支持文件加密。
```python
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend
import os
def encrypt_file(file_path, password):
salt = os.urandom(16)
kdf = algorithms.PBKDF2HMAC(
algorithm=algorithms.SHA256(),
length=32,
salt=salt,
iterations=100000,
backend=default_backend()
)
key = kdf.derive(password.encode())
iv = os.urandom(16)
cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend())
encryptor = cipher.encryptor()
with open(file_path, 'rb') as f:
plaintext = f.read()
ciphertext = encryptor.update(plaintext) + encryptor.finalize()
with open(file_path + '.enc', 'wb') as f:
f.write(salt + iv + ciphertext)
def decrypt_file(encrypted_path, password):
with open(encrypted_path, 'rb') as f:
salt = f.read(16)
iv = f.read(16)
ciphertext = f.read()
kdf = algorithms.PBKDF2HMAC(
algorithm=algorithms.SHA256(),
length=32,
salt=salt,
iterations=100000,
backend=default_backend()
)
key = kdf.derive(password.encode())
cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend())
decryptor = cipher.decryptor()
plaintext = decryptor.update(ciphertext) + decryptor.finalize()
with open(encrypted_path[:-4], 'wb') as f:
f.write(plaintext)
```
Base64编码(非加密)
通过编码混淆代码,但需注意此方法不安全,仅适用于简单场景。
```python
import base64
def encode_code(code):
return base64.b64encode(code.encode()).decode()
def decode_and_run(encoded_code, password):
decoded_code = base64.b64decode(encoded_code)
exec(decoded_code)
```
密码管理(如PBKDF2)
使用`passlib`库安全存储密码。
```python
from passlib.hash import pbkdf2_sha256
def hash_password(password):
return pbkdf2_sha256.hash(password)
def verify_password(stored_hash, password):
return pbkdf2_sha256.verify(password, stored_hash)
```
二、PowerShell加密方法
使用`AES`模块加密文件夹或文件。
```powershell
加密文件
function Encrypt-File {
param (
[string]$filePath,
[string]$password
)
[System.Security.Cryptography.AES]::Create().Encrypt(
[System.IO.File]::ReadAllBytes($filePath),
[System.Security.Cryptography.RNGCryptoServiceProvider]::Create().GetBytes(16),
[System.Security.Cryptography.AES]::Create().CreateEncryptor(
[System.Security.Cryptography.HMAC]::Create($password, [System.Security.Cryptography.SHA256]::Create()).GetHash([System.Text.Encoding]::UTF8.GetBytes($password)), [System.Security.Cryptography.AES]::Create().GetIV()
)
) | Set-Content "$filePath.enc"
}
解密文件
function Decrypt-File {
param (
[string]$encryptedFilePath,
[string]$password
)
[System.Security.Cryptography.AES]::Create().Decrypt(
[System.IO.File]::ReadAllBytes($encryptedFilePath),
[System.Security.Cryptography.RNGCryptoServiceProvider]::Create().GetBytes(16),
[System.Security.Cryptography.AES]::Create().CreateDecryptor(
[System.Security.Cryptography.HMAC]::Create($password, [System.Security.Cryptography.SHA256]::Create()).GetHash([System.Text.Encoding]::UTF8.GetBytes($password)), [System.Security.Cryptography.AES]::Create().GetIV()
)
) | Set-Content $encryptedFilePath -Encoding Byte
}
```
三、系统级加密工具
Windows BitLocker
系统级加密工具,支持硬盘、U盘等设备加密,无需