cik_ail has asked for the wisdom of the Perl Monks concerning the following question:

$line_a=0; $line_b=0; open(PLAIN, "plain.txt") or die "File does not exist: $!"; while(<PLAIN>){ @my_a[$line_a] = $_; $line_a++; } close PLAIN; open(COUNTER, "counter") or die "File does not exist: $!"; while(<COUNTER>){ @my_b[$line_b] = $_; $line_b++; } close COUNTER; $element1 = @my_a; $element2 = @my_b; $i = 0; $j = 0; while ($element1 > $i) { if ($element2 <= $j){ $j = 0; } @my_c[$i] = @my_a[$i] ^ @my_b[$j]; @my_d[$i] = @my_b[$j] ^ @my_c[$i]; $i++; $j++; }
Here the question,
1. How can i open and read the file in byte[] mode?
2. How do we ensure that the @my_d == @my_a[] after we XOr it back? (i've tried it, but it mess up lots of whitespace)
3. Because this is a Crypto-Counter Mode process, do anyone know whats the simplest way of calling them? (rather than recreating these codes above).

Thanx in advance..

Replies are listed 'Best First'.
Re: Reading two (2) file in byte[] form and then XOring them
by sgifford (Prior) on Feb 24, 2005 at 06:48 UTC

    Note that your code assumes that both files have the same number of lines, and that those lines have the same number of characters. To avoid that assumption, read in blocks with the read function instead of reading a line at a time. You should also use binmode, as PodMaster sggested.

    Also, you may find that Crypt::CTR does some or all of what you want.

    Something like this is a shorter way to do what you want. You can check they're equal with the string equality operators (eq and ne), as shown below.

    # Read in my @my_a = <PLAIN>; my @my_b = <CTR>; # XOR my @my_c = map { $my_a[$_] ^ $my_b[$_] } 0..$#my_a; my @my_d = map { $my_b[$_] ^ $my_c[$_] } 0..$#my_b; # Check foreach (0..$#my_d) { $my_d[$_] eq $my_a[$_] or die "d[$my_d[$_]] != a[$my_a[$_]] at line $_\n"; } print "OK\n";
      Thanx..

      But the problem that arrive in Crypt::CTR (the counter is not be able to be transfer to another PC) limits me.. Could anyone here help me??

      How can I used the same counter on other PC?? Its due change on every file running and how can I decrypt it back??

        It looks like the counter is stored in the object's hashref, in register. So you can get it out as a string by using register, or as an integer by using unpack like this: my ($ctr, undef) = unpack "La*", $cipher->{register}. If you wanted to put it into a new object, you could probably just set that object's register property. Subclassing would be a slightly more elegant way to do this, but any way you slice it, this is a hack, and could change under your feet since it's not an official part of the interface. But, it should work well enough to test, and make the case to the module author that an interface to the counter should be exposed.

        Generally you shouldn't need to get the counter out. What are you doing that requires that?

Re: Reading two (2) file in byte[] form and then XOring them
by PodMaster (Abbot) on Feb 24, 2005 at 06:06 UTC
    or die "File does not exist: $!";
    I would change that to or die "Can't open 'counter' for reading : $!";

    I would also change all that while business to

    my @my_a = <PLAIN>;
    1. How can i open and read the file in byte mode?
    `perldoc -f open' should've told you about binmode.
    2. How do we ensure that the @my_d == @my_a after we XOr it back? (i've tried it, but it mess up lots of whitespace)
    Go ahead and show what you've tried, it could clear up what you're attempting to accomplish
    Because this is a Crypto-Counter Mode process, do anyone know whats the simplest way of calling them? (rather than recreating these codes above).
    Calling who? XOR is not crypto

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.