XOR Binary Stream Against a Known Binary Key
I'm trying to xor a known binary key against a stream of incoming binary. The key is a fixed length that repeats over and over again because the stream is continuous.
I've come up with this is perl: Code:
#!/usr/bin/perl Here's my sample input: Code:
$ cat bin.txt Code:
$cat bin.txt | perl xor.pl |
You are XORing two strings.
Code:
$ perl -e 'print 2 ^ 1, "\n"' |
Quote:
However I've tried the statement above and I'm not getting the results I would hope for: Code:
perl -e 'print 0110 ^ 1001, "\n"' |
Code:
~/tmp$ perl -e 'printf ("%04b\n" ,(0b0110 ^ 0b1001))' |
Quote:
Code:
#!/usr/bin/perl Code:
echo 1111111111111111111011000101000001010000010100000000001000000100000001100000000 | perl scripts/descrambler.pl |
By my count, that's a 79-bit number.
perldoc Math::BigInt |
Ok so I added biginit with a use statement "use Math::BigInt;" however the function still errors out with an error of "Integer overflow in binary number at scripts/descrambler.pl"
|
DESCRIPTION
All operators (inlcuding [sic] basic math operations) are overloaded if you declare your big integers as $i = new Math::BigInt '123_456_789_123_456_789'; Operations with overloaded operators preserve the arguments which is exactly what you expect. |
I don't think that "BigInt" is the right way to solve this problem. That tool is really intended for unlimited-precision mathematics. Not the right Swiss Army knife for this job, methinks.
I'm not actually quite sure what is the best tool for XORing a buffer in Perl. Maybe a group like [url]http://www.perlmonks.org[/i] could point you in the right direction and you could summarize it here. |
Quote:
Code:
#!/usr/bin/perl Code:
$ echo 1111111111111111111011000101000001010000010100000000001000000100000001100000000 | perl xor.pl |
I had another go at this and I'm still getting funky results, in one iteration I got a NaN error which apparently means not a number, so I took the quotes off of both the input and key which means they are no longer character strings but instead pure numbers, however it still doesn't work:
Code:
#!/usr/bin/perl Code:
echo 1111111111111111111011000101000001010000010100000000001000000100000001100000000 | perl xor.pl |
OK, so one of the problems I've determined is still that the input is not being represented as binary. By manually a-fixing 0b to the input from the echo that it gets rid of one problem but I can't seem to get it to read in that way automatically.
|
Right, "use" or "require" import the library and its namespace into your program. You must actually invoke the methods or functions, in this case, to overload the data types such that the arithmetic operators "just work" as you'd expect. But until you've called "new" in this case, your data types are standard perl data types.
I was wondering why you don't split the data into 32-bit chunks, and perform your XOR on the 32-bit integers. Why try to perform large integer arithmetic, when instead you can let the math processor work for you. Split your 79 character binary string into 32, 32 and 15-bit binary representations, XOR each of those chunks, and output as you see fit. |
Well, it sounds like a good idea, however the sequence will repeat as more and more data flows in, for instance if I have a binary stream that lets say for the sake of argument 1000 bits long, my key which is 79 bits long will repeat over and over again until it's been XOR'd against the entire stream.
Perhaps i should look at this another way, My plan was to create by hand the full 1000bit key based on a stream length of 1000 / a 79 bit key ~= 13 repetitions of the key - and extra 27 bits... and then use that full key to XOR against the full stream. Maybe I should go about this by creating 79 individual variables and assigning them each 1 bit of the key, then by doing so I would be able to have a loop run a xor of the first variable against the first incoming bit in the stream and so on for 79 times then have the loop reset to variable 1 "key bit one". Does that seem like a feasible solution? |
You could do that, pre-creating an array of key length size, and creating a shift register, xor'ing one bit at a time. Here's an example;
Code:
#!/usr/bin/perl This certainly is very inefficient for large data sets. It would be better to work in architecture-sized ints, so that the hardware could do a single XOR on all 32 bits. You could pre-compute N full length key segments, and the various permutations of the cycled remainder key bits. |
All times are GMT -5. The time now is 01:50 PM. |