Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Re: Mod-2 add 2 binary files

by zude (Scribe)
on May 19, 2004 at 05:08 UTC ( [id://354526]=note: print w/replies, xml ) Need Help??


in reply to Re: Mod-2 add 2 binary files
in thread Mod-2 add 2 binary files

Close but no cigar, you can't xor arbitrary strings that way. The following will cipher STDIN to STDOUT using the contents of the keyfile on the command line. Assumption is that the key file is much smaller than memory (maybe 5% or less):
use warnings; use strict; @ARGV or die "Usage: cipher keyfile < source > dest\n"; open K,shift or die "Can't open keyfile: $!\n"; undef $/; my $k = <K>; close K; # slurp length $k or die "error reading key file\n"; while (read STDIN, my $t, length $k) { my @k = unpack "C*", $k; print pack "C*", map { $_^shift @k } unpack "C*", $t; }
Now let's try it:
# make a key file, ok for demo purposes $ perl -e 'print chr int rand 256 for 0..10000' > key.file # make a test source file $ ls -al /dev > plain.txt # encipher $ perl cipher.pl key.file < plain.txt > cipher.txt # decipher $ perl cipher.pl key.file < cipher.txt > deciphered.txt # verify $ diff -s plain.txt deciphered.txt Files plain.txt and deciphered.txt are identical


~%{${@_[0]}}->{0}&&+++ NO CARRIER

Replies are listed 'Best First'.
Re: Re: Re: Mod-2 add 2 binary files
by thor (Priest) on May 19, 2004 at 11:56 UTC
    I'm not sure that I understand why you can't bit-wise xor two strings:
    $foo = "baz"; $bar = "foo"; print unpack("B*", $foo); print unpack("B*", $bar); print unpack("B*", ($bar ^ $foo)); __END__ 011000100110000101111010 011001100110111101101111 000001000000111000010101
    Seems to do the right thing to me...

    thor

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://354526]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-24 04:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found