You should use binmode() on your input and output. This might explain your problem. I'd also not read "a line at a time" since during decryption the line endings will be encrypted. No other errors pop out at me so give that a try.

You can tell you converted this from C code. Perl will let you xor entire strings so you could make this faster and simpler, something like:

#!/usr/bin/perl -w use strict; if(@ARGV < 3) { print "Usage: $0 <key> <input file> <output file>\n"; exit(0); } my $key = $ARGV[0]; open(IN, $ARGV[1]) or die "Can't read $ARGV[1]: $!\n"; open(OUT, ">$ARGV[2]") or die "Can't wrote $ARGV[2]: $!\n"; binmode(IN); binmode(OUT); my $in; while( sysread(IN,$in,length($key)) ) { print OUT $in^substr($key,0,length($in)); } close(IN); close(OUT);

I tested this and it works for me.

Update: I tested your code under Win98 and the first part of the file decrypted properly and then it fell apart. Looking at the encrypted file I find that the description fell apart at the first character that got encrypted into a newline. So I may have actually diagnosed the problem correctly.

Also, interestingly enough, when I was in college and worked for their computer department, I helped investigate some cracking that a student was doing. The student had encrypted the source code to their tools using something very much like this. They didn't keep the encrypt/decrypt source code on their account. So working with just the contents of some files, I was able to decrypt their files using fairly simple techniques. So don't consider this simple xoring as other than a very insecure obscuring and not really encrypting.

        - tye (but my friends call me "Tye"

In reply to (tye)Re: xor encrypt-decrypt routine by tye
in thread xor encrypt-decrypt routine by j0e

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.