简体   繁体   English

Python3读取二进制文件,一次4个字节,xor,4个字节长的密钥

[英]Python3 reading a binary file, 4 bytes at a time and xor it with a 4 byte long key

I want to read a binary file, get the content four bytes by four bytes and perform int operations on these packets. 我想读取二进制文件,获取四个字节乘以四个字节的内容,并对这些数据包执行int操作。

Using a dummy binary file, opened this way: 使用虚拟二进制文件,以这种方式打开:

with open('MEM_10001000_0000B000.mem', 'br') as f:
    for byte in f.read():
            print (hex(byte))

I want to perform an encryption with a 4 byte long key, 0x9485A347 for example. 我想用4字节长的密钥执行加密,例如0x9485A347

Is there a simple way I can read my files 4 bytes at a time and get them as int or do I need to put them in a temporary result using a counter? 有一种简单的方法我可以一次读取我的文件4个字节并将它们作为int或者我需要使用计数器将它们放入临时结果吗?

My original idea is the following: 我最初的想法如下:

        current_tmp = []
        for byte in data:
            current_tmp.append(int(byte))
            if (len(current_tmp) == 4):
                    print (current_tmp)
                    # but current_tmp is an array not a single int
                    current_tmp = []

In my example, instead of having [132, 4, 240, 215] I would rather have 0x8404f0d7 在我的例子,代替具有[132, 4, 240, 215]我宁愿有0x8404f0d7

Just use the "amount" parameter of read to read 4 bytes at a time, and the "from_bytes" constructor of Python's 3 int to get it going: 只需使用read的“amount”参数一次读取4个字节,然后使用Python的3 int的“from_bytes”构造函数来实现它:

with open('MEM_10001000_0000B000.mem', 'br') as f:
    data = f.read(4)
    while data:
        number = int.from_bytes(data, "big")
        ...
        data = f.read(4)

If you are not using Python 3 yet for some reason, int won't feature a from_bytes method - then you could resort to use the struct module: 如果由于某种原因你还没有使用Python 3,那么int将不会使用from_bytes方法 - 那么你可以使用struct模块:

import struct
...
    number = struct.unpack(">i", data)[0]
    ...

These methods however are good for a couple interations, and could get slow for a large file - Python offers a way for you to simply fill an array of 4-byte integer numbers directly in memory from an openfile - which is more likely what you should be using: 然而,这些方法适用于几个交互,并且对于大文件可能会变慢 - Python提供了一种方法,您可以直接从开放文件中直接在内存中填充4字节整数数组 - 这更可能是您应该的使用:

import array, os
numbers = array.array("i")
with open('MEM_10001000_0000B000.mem', 'br') as f:
    numbers.fromfile(f, os.stat('MEM_10001000_0000B000.mem').st_size // numbers.itemsize)
numbers.byteswap()

Once you have the array, you can xor it with something like 一旦你有了数组,就可以用类似的东西对它进行xor

from functools import reduce #not needed in Python2.7
result = reduce(lambda result, input: result ^ input, numbers, key)

will give you a numbers sequence with all numbers in your file read-in as 4 byte, big endian, signed ints. 将为您提供一个numbers序列,文件中的所有数字都以4字节,大端,有符号整数读入。

If you file is not a multiple of 4 bytes, the first two methods might need some adjustment - fixing the while condition will be enough. 如果你的文件不是4个字节的倍数,前两个方法可能需要一些调整 - 修复while条件就足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM