b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | #!/usr/bin/env python3 |
| 2 | |
| 3 | import argparse |
| 4 | import binascii |
| 5 | import hashlib |
| 6 | import os |
| 7 | import struct |
| 8 | |
| 9 | def create_header(key, version, iv, random, size): |
| 10 | header = struct.pack('32s32s32s32s32s', key, version, iv, random, size) |
| 11 | |
| 12 | return header |
| 13 | |
| 14 | def create_output(args): |
| 15 | in_st = os.stat(args.input_file) |
| 16 | in_size = in_st.st_size |
| 17 | |
| 18 | key = "".encode('ascii') |
| 19 | version = args.version.encode('ascii') |
| 20 | iv = "".encode('ascii') |
| 21 | random = "".encode('ascii') |
| 22 | size = str(in_size).encode('ascii') |
| 23 | header = create_header(key, version, iv, random, size) |
| 24 | |
| 25 | out_f = open(args.output_file, 'w+b') |
| 26 | out_f.write(header) |
| 27 | out_f.close() |
| 28 | |
| 29 | md5 = hashlib.md5() |
| 30 | md5.update(header[0x60:0x80]) |
| 31 | md5.update(header[0x20:0x40]) |
| 32 | md5_1 = md5.digest() |
| 33 | |
| 34 | md5 = hashlib.md5() |
| 35 | md5.update(header[0x80:0xA0]) |
| 36 | md5.update(header[0x20:0x40]) |
| 37 | md5_2 = md5.digest() |
| 38 | |
| 39 | key = md5_1 + md5_2 |
| 40 | |
| 41 | key_f = open(args.key_file, 'w+b') |
| 42 | key_f.write(binascii.hexlify(bytearray(key))) |
| 43 | key_f.close() |
| 44 | |
| 45 | print("AES 256 CBC Key:", binascii.hexlify(bytearray(key))) |
| 46 | |
| 47 | def main(): |
| 48 | global args |
| 49 | |
| 50 | parser = argparse.ArgumentParser(description='') |
| 51 | |
| 52 | parser.add_argument('--input-file', |
| 53 | dest='input_file', |
| 54 | action='store', |
| 55 | type=str, |
| 56 | help='Input file') |
| 57 | |
| 58 | parser.add_argument('--key-file', |
| 59 | dest='key_file', |
| 60 | action='store', |
| 61 | type=str, |
| 62 | help='AES 256 CBC Key File') |
| 63 | |
| 64 | parser.add_argument('--output-file', |
| 65 | dest='output_file', |
| 66 | action='store', |
| 67 | type=str, |
| 68 | help='Output file') |
| 69 | |
| 70 | parser.add_argument('--version', |
| 71 | dest='version', |
| 72 | action='store', |
| 73 | type=str, |
| 74 | help='Version') |
| 75 | |
| 76 | args = parser.parse_args() |
| 77 | |
| 78 | if ((not args.input_file) or |
| 79 | (not args.key_file) or |
| 80 | (not args.output_file) or |
| 81 | (not args.version)): |
| 82 | parser.print_help() |
| 83 | |
| 84 | create_output(args) |
| 85 | |
| 86 | main() |