#!/usr/local/bin/perl5 -w ####################################################3 ## ## Paul Garrett, 10 Sept 1997 ## ## Does Vigenere cipher ## ## Usage: vigenere ## ######################################################## ## ## Uses "pack" and "unpack" functions to get ascii number of chars ## and vice-versa ## ## Reads from command line or STDIN if no command-line string given ## ## Only encrypts [a-zA-Z], leaves other chars alone, does _not_ ## advance key counter for [^a-zA-Z] ## ################################################################ require Num; $usage = " -> Usage: vigenere [-decrypt] <key> <plaintext>\n"; $usage .= " or vigenere [-decrypt] <key> < <file> \n"; ($key = shift) || die $usage; chomp($key); if ($key =~ m/-d/ || $key =~m/-decrypt/) { ## to decrypt $decrypt = 1; ($key = shift) || die $usage; } else { $decrypt = 0; } ################################################ die $usage unless ($key =~ m/^[a-z]+$/); @key = &Num::word_to_ints($key); ############################################## if ($in = shift) { chomp($in); @in = ($in); } else { @in = <STDIN>; $in = join(' ',@in); } $in =~ s/[^a-zA-Z ]+//g;## cut out non-char non-space chars $in = join(' ',@in); ## now plaintext is a big string @in = split(//,$in); ## now plaintext is an array ############################################## $m = $#key + 1; $t = 0; if ($decrypt == 0) { ## encrypt case foreach $i (0..$#in) { if ($in[$i] =~ m/[a-zA-Z]/) { $in[$i] = uc(&Num::int_to_char( (&Num::char_to_int($in[$i]) + $key[$t % $m] ) % 26 )); $t++; } print $in[$i]; } } else { ## decrypt case foreach $i (0..$#in) { if ($in[$i] =~ m/[a-zA-Z]/) { $in[$i] = lc(&Num::int_to_char(reduce(&Num::char_to_int($in[$i]) - $key[$t % $m], 26))); print $in[$i]; $t++; } else { print $in[$i]; } } } print "\n"; ###############################################################