Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/bin/perl use warnings; use strict; #map contains our character mapping to expedite character replacement/ +encryption my %map = ( "a" => "i", "A" => "j", "b" => "s", "B" => "4", "c" => "S", "C" => 'Y', "d" => "u", "D" => "t", "e" => "a", "E" => "O", "f" => "&", "F" => " ", "g" => "A", "G" => "v", "h" => "g", "H" => "J", "i" => "%", "I" => "B", "j" => "\$", "J" => "T", "k" => "K", "K" => "k", "l" => "@", "L" => "#", "m" => "!", "M" => "b", "n" => "0", "N" => "M", "o" => "q", "O" => "l", "p" => "r", "P" => "R", "q" => "U", "Q" => "Q", "r" => "n", "R" => "C", "s" => "9", "S" => "G", "t" => "8", "T" => "L", "u" => "i", "U" => "7", "v" => "h", "V" => ",", "w" => "H", "W" => "P", "x" => "6", "X" => "m", "y" => "c", "Y" => "x", "z" => "5", "Z" => "w", "1" => "F", "2" => "y", "3" => "z", "4" => "Z", "5" => "p", "6" => "f", "7" => "W", "8" => "v", "9" => "3", "0" => "E", "!" => "d", "@" => "2", "#" => "1", "\$" => "e", "%" => "D", "^" => ".", "&" => "n", "," => "x", "," => "o", " " => "~" ); print "\n\nWould you like to Encrypt or Decrypt a file [e/d]? "; my $action = <STDIN>; chomp($action); # If action matches the letter e, perform an encryption if ($action =~ m/e/i) { print "\n"; print "Enter input file name: "; my $infile = <STDIN>; chomp($infile); print "Enter output file name: "; my $outfile = <STDIN>; chomp($outfile); # open source file for reading and store it locally (and check for +errors) open(INFILE, $infile) or die "Error opening input file $infile: $!" +; my @source = <INFILE>; close(INFILE) or die "Error closing input file $infile: $!"; # since we stored the data from the source file as an array (like w +e had to) # we need to now join everything together as a large scalar to per +form s/// my $source = join("", @source); foreach my $key (keys %map) { $source =~ s/$key/$map{$key}/g; } open(OUTFILE, ">$outfile") or die "Error creating new file $outfile +: $!"; print OUTFILE $source; close(OUTFILE) or die "Error closing new file $outfile: $!"; sleep(5); # for added affect! print "\n\nEncryption completed.\n\n"; exit; } elsif($action =~ m/d/i) { print "Enter file name to decrypt: "; my $infile = <STDIN>; chomp($infile); print "Enter output file name: "; my $outfile = <STDIN>; chomp($outfile); open(INFILE, $infile) or die "Error opening input file $infile: $!" +; my @source = <INFILE>; close(INFILE) or die "Error closing input file $infile: $!"; # since we stored the data from the source file as an array (like w +e had to) # we need to now join everything together as a large scalar to per +form s/// my $source = join("", @source); foreach my $key (keys %map) { $source =~ s/$map{$key}/$key/g; } open(OUTFILE, ">$outfile") or die "Error creating new file $outfile +: $!"; print OUTFILE $source; close(OUTFILE) or die "Error closing new file $outfile: $!"; print "Decryption completed."; exit; } else { print "Sorry, I did not understand your command."; print "\nPlease run this program again, and"; print "\nType in \"e\" to encrypt a file or \"d\" to decrypt a file +\n\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Using a hash to globally s/// a string
by roboticus (Chancellor) on May 02, 2012 at 21:33 UTC | |
|
Re: Using a hash to globally s/// a string
by toolic (Bishop) on May 02, 2012 at 21:19 UTC | |
|
Re: Using a hash to globally s/// a string
by ww (Archbishop) on May 02, 2012 at 21:28 UTC |