http://www.perlmonks.org?node_id=97075


in reply to Re: A real challenge
in thread A real challenge

or even
#!perl -w use strict; sub encode_decode ($) { my $txt = shift; my $key = 'Copyright © 2000 ActiveState Tool Corp.' x length $txt; my $enc = $txt ^ substr $key, 0, length $txt; return wantarray ? unpack "C*", $enc : $enc; } undef $/; my @encoded_ascii = encode_decode <DATA>; # one or other #my $encoded_string = ~~encode_decode <DATA>; # of these two __DATA__ #!"#$%&'()*+,-./0123456789:;<=>?@ #ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_` #abcdefghijklmnopqrstuvwxyz{|}~ #!"#$%&'()*+,-./0123456789:;<=>?@ #ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_` #abcdefghijklmnopqrstuvwxyz{|}~ #!"#$%&'()*+,-./0123456789:;<=>?@ #ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_` #abcdefghijklmnopqrstuvwxyz{|}~ print "Show me the encoding!\n";

"Argument is futile - you will be ignorralated!"

Replies are listed 'Best First'.
Re3: A real challenge
by bikeNomad (Priest) on Jul 16, 2001 at 20:39 UTC
    You're making $key too long by a factor of 39. You want something more like this (where @encoded is the given byte array):

    my $encodedString = pack('c*', @encoded); my $key = 'Copyright © 2000 ActiveState Tool Corp.'; $key = $key x ( length($encodedString) / length($key) + 1); $key = substr($key, 0, length($encodedString)); print $key ^ $encodedString;
      I know - it's just so that the key can be any length without having to do the length X/length y + 1 thing all on one line :) I have to substr it anyway so what's a few bytes between system calls! I wouldn't have been so frivolous if I was getting the key from STDIN though!

      "Argument is futile - you will be ignorralated!"