简体   繁体   English

如何构建反向按位运算

[英]How to build a reverse bitwise operation

Bitwise Reverse Operation位逆运算

I have a group of encrypted bytes which I can decrypt with the following function:我有一组加密字节,我可以使用以下 function 对其进行解密:

        byte b = (byte) (encrypted & 0xFF);
        return b >> 5 & 0x7 | (b & 0x1F) << 3;
    }

    0x51 >> 5 & 0x7 | 0x51 & 0x1F << 3 = 0x98
    ^                 ^

At first glance, it precedes a complex operation, so I have decided to separate it into parts.乍一看,它先于一个复杂的操作,所以我决定将它分成几部分。

I need to get the reverse operation.我需要进行反向操作。 I know how byte operations work;我知道字节操作是如何工作的; the displacements seem easy to reverse but the AND are not more difficult.位移似乎很容易逆转,但 AND 并不难。 Many thought in previous threads that you cannot but would like to know your opinions about it.许多人在以前的帖子中认为您不能但想知道您对此的看法。

I have noticed that the operation (b & 0x1F) only returns numbers of 0x0 and 0x1F and when it reaches 0x20 restarts at 0x0 , so successively until 0x39 that returns 0x20 and 0x40 naturally returns 0x0 .我注意到操作(b & 0x1F)只返回0x00x1F的数字,并且当它到达0x20时在0x0重新启动,所以连续直到0x39返回0x200x40自然返回0x0

Thinking about a possible solution, I have to know of a function "x & y = z" that I know "y" and "z", for example "x + 0x1F = 0x1A" can determine that the original byte could be any of the next {0x1A, 0x3A, 0x5A, 0x7A, 0x9A, 0xBA, 0xDA, 0xFA} , but now the question is how do I select the correct one.考虑一个可能的解决方案,我必须知道一个 function "x & y = z"我知道 "y" 和 "z",例如"x + 0x1F = 0x1A"可以确定原始字节可以是任何下一个{0x1A, 0x3A, 0x5A, 0x7A, 0x9A, 0xBA, 0xDA, 0xFA} ,但现在的问题是我如何 select 正确的一个。

To try to answer this question 0xA1 & 0x1F = 0x1 , assuming that of "x & y = z" , we only know y and "z" the possible x would only be {0x1, 0x21, 0x41, 0x61, 0x81, 0xA1, 0xC1, 0xE1} , as you can see the answer is between 8 possible bytes.尝试回答这个问题0xA1 & 0x1F = 0x1 ,假设"x & y = z" ,我们只知道y"z"可能的 x 只会是{0x1, 0x21, 0x41, 0x61, 0x81, 0xA1, 0xC1, 0xE1} ,如您所见,答案在 8 个可能的字节之间。

Maybe I am posing the problem badly but I cannot build a reverse operation.也许我提出的问题很糟糕,但我无法建立反向操作。 Can you give me your opinion or explain how to put together an operation of this type?你能给我你的意见或解释如何组合这种类型的操作吗?

To find a reverse operation, you should try looking at what the code is doing on the whole , instead of the individual operations.要找到反向操作,您应该尝试查看代码在整体上所做的事情,而不是单个操作。

The left operand of | |的左操作数says,说,

Shift b 5 places to the right >> 5 then clear all the bits except the least significant three bits & 0x7 .b向右移动 5 位>> 5然后清除除最低有效三位& 0x7之外的所有位。 Note that 7 is 111 in binary.请注意,7 是二进制的 111。

The above gets the most significant 3 bits of b and moves them all the way to the right.以上获取b的最高 3 位并将它们一直向右移动。 1010 1100 would become 0000 0101. 1010 1100 将变为 0000 0101。

The right operand of | |的右操作数says,说,

Clear all the bits except b 's least significant 5 bits, and then shift b 3 places to the left.清除除b的最低有效 5 位之外的所有位,然后将b向左移动 3 位。 Note that 1F is 0001 1111 in binary.请注意,1F 是二进制的 0001 1111。

This gets the least significant 5 bits of b then moves all the way to the left.这将获得b的最低有效 5 位,然后一直向左移动。 1010 1100 would become 0110 0000. 1010 1100 将变为 0110 0000。

And finally, the |最后, | combines the two results together.将两个结果结合在一起。

So on a high level of abstraction, this code just switches the first 3 and last 5 bits in a byte.所以在高级抽象上,这段代码只是在一个字节中切换前 3 位和后 5 位。 1110 0000 would become 0000 0111. 1110 0000 将变为 0000 0111。

To reverse this, just switch the first 5, and last 3 bits.要反转这一点,只需切换前 5 位和后 3 位。

byte b = (byte) (decrypted & 0xFF);
return b >> 3 & 0x1F | (b & 0x7) << 5;

You might want to learn about Bit Masks if you find this answer confusing.如果您发现此答案令人困惑,您可能想了解位掩码

Just compute array of 256 bytes: m_encrypt ;只需计算 256 字节的数组: m_encrypt byte -> encrypted byte字节 -> 加密字节

If it is one-to-one then it is reversable, otherwise it isn't.如果它是一对一的,那么它是可逆的,否则它不是。

Compute decryption array m_decrypt via通过计算解密数组m_decrypt

for(int i=0; i< 256; i++)
  m_decrypt[m_encrypt[i]] = i;

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

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