Hi everyone

Sorry for posting all my code but I wanted you to be able to load it up fairly quickly and see what's happening.

Is is a very basic "roll your own basic encryption by s/// chars". I created a hash with my randomoness. When you load the application it asks you to encrypt or decrypt, if you encrypt and encrypt a file you actually have it'll perform an encryption. That part works.

However, when you then go back and choose to decrypt the file you just created it doesn't decrypt. It creates a new file and the only ouput I'm getting is a "^" for every character in the file. No other letters, numbers or characters. The output is just carrets.

What am I doing wrong? I assumed I could just reverse my map with s/$map{$key}/$key)/ but that doesn't seem to be the case.

Can someone see what I'm doing wrong? This is for a class and I know there are better ways to do it but this is what they're looking for.

#!/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"; }

In reply to Using a hash to globally s/// a string by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.