From 9d54ef4a8339ee15fc01f6122c9bfad2ae406796 Mon Sep 17 00:00:00 2001 From: jason123 <72319307+Answerr@users.noreply.github.com> Date: Wed, 11 Dec 2024 12:30:50 +0800 Subject: [PATCH] Add files via upload --- xor.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 xor.py diff --git a/xor.py b/xor.py new file mode 100644 index 0000000..f6f4ad3 --- /dev/null +++ b/xor.py @@ -0,0 +1,31 @@ +def xor_encrypt_decrypt(input_file, output_file, key=0x5A): + """ + 使用 XOR 加密或解密二进制文件。 + + :param input_file: 输入文件路径 + :param output_file: 输出文件路径 + :param key: XOR 密钥,默认为 0x5A + """ + try: + # 打开输入文件(以二进制模式读取) + with open(input_file, 'rb') as f_in: + data = f_in.read() + + # 对数据进行 XOR 操作 + encrypted_data = bytearray([byte ^ key for byte in data]) + + # 将加密/解密后的数据写入输出文件 + with open(output_file, 'wb') as f_out: + f_out.write(encrypted_data) + + print(f"文件已成功处理,输出文件为: {output_file}") + except FileNotFoundError: + print(f"错误: 找不到文件 {input_file}") + except Exception as e: + print(f"发生错误: {e}") + +# 示例用法 +if __name__ == "__main__": + input_file = "input.bin" # 输入文件路径 + output_file = "output.bin" # 输出文件路径 + xor_encrypt_decrypt(input_file, output_file) \ No newline at end of file