#!/usr/bin/perl # # Written by Travis Kent Beste # Wed Aug 1 17:56:12 CDT 2007 # # $Id: rot16.pl,v 1.1 2007/09/13 02:26:47 travis Exp $ # $Source: /cvs_repository/encryption/rot16.pl,v $ # y/A-Za-z/N-ZA-Mn-za-m/; # A character range may be specified with a hyphen, so # tr/A-J/0-9/ does the same replacement as "tr/ACEGIBDFHJ/0246813579/". use Getopt::Std; my %opts; getopts('f:s:dhev', \%opts); #----------------------------------------# # main #----------------------------------------# my $filename = $opts{'f'} || ''; my $string = $opts{'s'} || ''; my $encrypt = $opts{'e'} || 0; my $decrypt = $opts{'d'} || 0; my $verbose = $opts{'v'} || 0; my $help = $opts{'h'} || 0; if ( ($help) || ( (!$filename) && (!$string) ) ) { usage(); } # default to encrypt $encrypt = 1 if ( (!$encrypt) && (!$decrypt) ); # process a file if ($filename) { if (! -f $filename) { print "file '$filename' doesn't exist or isn't a file\n"; exit(1); } open(FP, "< $filename") || die "Unable to encrypt file '$filename': $!\n"; while() { chomp(my $line = $_); $line = encrypt($line) if ($encrypt); $line = decrypt($line) if ($decrypt); print "$line\n"; } close(FP); } # process a string if ($string) { chomp(my $line = $string); $line = encrypt($line) if ($encrypt); $line = decrypt($line) if ($decrypt); print "$line\n"; } exit(0); #----------------------------------------# # subroutines #----------------------------------------# # encrypt the input string sub encrypt { my $str = shift; $str =~ y/A-Za-z0-9.\-/g-vw-z0-9.\-A-PQ-Za-f/; return $str; } # decrypt the input string sub decrypt { my $str = shift; $str =~ y/A-Za-z0-9.\-/g-vw-z0-9.\-A-PQ-Za-f/; return $str; } # usage sub usage { print "usage: $0\n"; print "\t-e : encrypt (default)\n"; print "\t-d : decrypt\n"; print "\t-s : string to convert\n"; print "\t-f : filename to process\n"; print "\t-h : help, this menu\n"; exit(0); }