#!/usr/bin/perl -w use strict; if( $#ARGV < 0 ) { die "$0 \n"; } my $key = shift; $key =~ s/[^a-zA-Z]//g; $key ||= "a"; my @offsets = map {$_ <= ord('Z') ? (ord('A') - $_ + 26) % 26 : $_ - ord('a')} unpack 'C*', $key; my %translate = (); for(my $input = 0; $input < 26; $input++) { for(my $offset = 0; $offset < 26; $offset++) { $translate{chr(ord('a') + $input)}{$offset} = chr(ord('a') + ($input + $offset) % 26); $translate{chr(ord('A') + $input)}{$offset} = chr(ord('A') + ($input + $offset) % 26); } } my $index = 0; while( my $line = <> ) { foreach my $i (unpack 'C*', $line) { my $c = chr($i); if( $c =~ /[a-zA-Z]/ ) { my $offset = $offsets[$index++]; $index %= length($key); $c = $translate{$c}{$offset}; } print $c; } }