要解决“不运行Python代码解密文件”的问题,可以使用以下方法:
方法一:使用Python的加密库
示例代码如下:
from cryptography.fernet import Fernet
# 生成密钥
key = Fernet.generate_key()
# 初始化加密器
cipher = Fernet(key)
# 加密文件
with open('encrypted_file.txt', 'wb') as file:
with open('original_file.txt', 'rb') as f:
encrypted_data = cipher.encrypt(f.read())
file.write(encrypted_data)
print("文件已加密并保存为 encrypted_file.txt")
方法二:使用第三方的命令行工具
示例代码如下:
import subprocess
# 使用GPG工具加密文件
subprocess.run(['gpg', '--output', 'encrypted_file.txt.gpg', '--encrypt', 'original_file.txt'])
print("文件已加密并保存为 encrypted_file.txt.gpg")
# 使用OpenSSL工具加密文件
subprocess.run(['openssl', 'aes-256-cbc', '-salt', '-in', 'original_file.txt', '-out', 'encrypted_file.txt', '-pass', 'pass:password'])
print("文件已加密并保存为 encrypted_file.txt")
以上是两种不运行Python代码解密文件的示例解决方法,具体选择哪种方法取决于你的需求和环境。请根据实际情况进行适当调整。