in reply to xor encrypt-decrypt routine

What's happening to your newlines? Do you need a chomp? Or possibly an explicit newline at the end of the loop?

On second thought, is your array subscript right? Or maybe you should have >= instead of > in the if statement?

I think I've just disproved Jack Kerouac's old saying, "First thought, best thought."

Please note: I've diddled this node about five times in the last three minutes.

Replies are listed 'Best First'.
Re: Re: xor encrypt-decrypt routine
by j0e (Acolyte) on Jan 21, 2001 at 08:08 UTC
    The newline chars in the file should be encrypted/decrypted with everything else. Using the >= operator instead of > is probably a good idea, as whilst doing a little "print debugging" I found that there was a NULL or something at the end of the $key variable. That didn't seem to fix it, but oddly enough I seem to get much better results when I use the following loop:
    while(my $char = getc(IN)) { if($kp >= length($key)) { $kp = 0; } my $kc = substr($key, $kp++, 0); $char ^= $kc; print OUT $char; }
    That encrypts/decrypts perfectly, but seems to end abruptly after running through 1k of data. Now I'm even more confused. What's the difference between this code and the code I posted before?

      You probably finally hit a point that encrypts to a CTRL-Z which is end-of-file under Windows unless you use binmode as I suggested below.

              - tye (but my friends call me "Tye")