import os
from Crypto.Cipher import AES
from Crypto.Util.Padding import unpad
import binascii
# 密钥文件路径
key_file = "enc.key"
# IV初始化向量 (数据为全零)
iv = bytes([0] * 16)
# 解密函数
def decrypt_aes_128(cipher_text, key, iv):
cipher = AES.new(key, AES.MODE_CBC, iv)
decrypted = unpad(cipher.decrypt(cipher_text), AES.block_size)
return decrypted
# 从文件读取密钥
def read_key(file_path):
with open(file_path, 'rb') as f:
key = f.read(16) # 读取16字节的AES-128密钥
return key
# 解密PList文件夹中的所有文件
def decrypt_files_in_directory(directory):
# 读取密钥
key = read_key(key_file)
# 遍历目录下的所有文件
for filename in os.listdir(directory):
file_path = os.path.join(directory, filename)
# 只处理文件
if os.path.isfile(file_path):
try:
# 打开并读取加密的文件内容
with open(file_path, 'rb') as f:
encrypted_data = f.read()
# 解密
decrypted_data = decrypt_aes_128(encrypted_data, key, iv)
# 保存解密后的文件
decrypted_filename = f"decrypted_{filename}"
decrypted_file_path = os.path.join(directory, decrypted_filename)
with open(decrypted_file_path, 'wb') as df:
df.write(decrypted_data)
print(f"文件 '{filename}' 解密成功,已保存为 '{decrypted_filename}'")
except Exception as e:
print(f"解密文件 '{filename}' 时发生错误: {e}")
# 主函数
if __name__ == "__main__":
# 设置存储已加密文件夹的路径
plist_folder = './Encrypted'
if os.path.exists(plist_folder):
decrypt_files_in_directory(plist_folder)
else:
print(f"文件夹 {plist_folder} 不存在!")
|