Mysql: xor key row

I want to beat a bit-XOR string (actually its binary representation) using KEY.

The result of the operation should be presented as HEX.

What I have: 'a' is the UTF-8 string that needs to be changed. "ACF123456" is the key in the HEX.

The result is considered as BIGINT:

select CONV(HEX('a'), 16, 10) ^ CONV('ACF123456', 16, 10); 

The result is considered as HEX:

 select CONV( CONV(HEX('a'), 16, 10) ^ CONV('ACF123456', 16, 10), 10, 16); 

Questions:

  • Is the conversion correct?
  • What happens if the string is too long (for example, instead of "a" we have a "long string veeeeeery")? It seems that conv () function has a limitation (is it 64-bit precision from the documentation)? And besides, the XOR operator also has a constraint associated with nr. bit of the returned result. Any solutions that work for any string (stored procedure allowed)?

Thanks.

+4
source share
1 answer

Your conversions look good to me. And as you point out, CONV() and ^ have really 64-bit precision.

2 ^ 64 = 16 ^ 16, so strings of more than 16 hexadecimal digits must be converted to integers greater than 2 ^ 64. However, such strings will be brutally (silently) truncated to the left when trying to convert them to integers.

The point of my solution here is to slice such lines. Obviously, the result may not be displayed as an integer, but only as a string representation.

Let @input be your "string to be changed" and @key , your "key".

  • Assign HEX(@input) to @hex_input . There is no problem here, since HEX() works with strings.
  • Slice @hex_input in 16 hexadecimal digits with long strings starting from the right
  • Similarly, slice @key into 16-bit long lines.
  • Compute the X-OR each 64-bit @hex_input fragment with each 64-bit @key fragment, starting from the right. Use CONV(@slice, 16, 10) . If either @hex_input or @key has fewer fragments than the other line, then X-OR remaining fragments of the other line with 0.
  • Convert every 64-bit number obtained from X-OR in step 4. back to a hexadecimal string with UNHEX() .
  • Collect the resulting slices. This is your result.

The TEMPORARY table of three columns can be used as an array for storing @hex_input , @mask fragments and resulting fragments.

Put it all together in a stored procedure, and voilà!

It looks like you have some skills in MySQL, you should be able to translate the above into real code. But I will be happy to help if you need further guidance.

+1
source

All Articles