in reply to Mod-2 add 2 binary files
Basically, (X xor Y) xor X = Y for all X,Y. And, because the xor operation is equivalent to addition modulo 2 (or, as the algebraist might say "addition in the finite field with two elements"), this works. A canned solution? Here's a quicky:thulben@bravo:~/perl 125> perl -le 'print "A" ^ "C"' | perl -nle 'print $_^"C"' A thulben@bravo:~/perl 126> perl -le 'print "A" ^ "C"' | perl -nle 'print $_^"A"' C thulben@bravo:~/perl 127>
I don't do a lot of error checking here, so there is room to improve.use warnings; use strict; my $cipher = shift; my $key = shift; open( my $cipher_fh, $cipher ) or die $!; open( my $key_fh, $key ) or die $!; my $buffer_length = 4_096; while ( sysread( $cipher_fh, my $cipher_text, $buffer_length, ) && sysread( $key_fh, my $key_text, $buffer_length, ) ) { print $cipher_text ^ $key_text; }
thor
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Mod-2 add 2 binary files
by zude (Scribe) on May 19, 2004 at 05:08 UTC | |
by thor (Priest) on May 19, 2004 at 11:56 UTC |