j0e has asked for the wisdom of the Perl Monks concerning the following question:
Seems simple enough, but when I encrypt a file and then attempt to de-crypt it with the same key *some* of the file decrypts correctly, but most of it is garbage. A realativly simple task in C, this has had me beating my head against a wall for some time. I'm not exactly a perl kung-fu master, so if anyone has any insight as to what I might be doing wrong, I'd greatly appreciate hearing it.#!/usr/bin/perl 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 open infile"; open(OUT, ">$ARGV[2]") or die "Can't open outfile"; my $kp = 0; while(<IN>) { for my $i (0..length($_)) { if($kp > length($key)) { $kp = 0; } my $kc = substr($key, $kp++, 1); my $char = substr($_, $i, 1); $char ^= $kc; print OUT $char; } } close(IN); close(OUT);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
(tye)Re: xor encrypt-decrypt routine
by tye (Sage) on Jan 21, 2001 at 07:48 UTC | |
by 2501 (Pilgrim) on Jan 23, 2001 at 02:33 UTC | |
by tye (Sage) on Jan 23, 2001 at 04:07 UTC | |
|
Re: xor encrypt-decrypt routine
by chromatic (Archbishop) on Jan 21, 2001 at 09:08 UTC | |
by Anonymous Monk on Jan 21, 2001 at 18:49 UTC | |
|
Re: xor encrypt-decrypt routine
by adamsj (Hermit) on Jan 21, 2001 at 07:23 UTC | |
by j0e (Acolyte) on Jan 21, 2001 at 08:08 UTC | |
by tye (Sage) on Jan 21, 2001 at 10:06 UTC |