Fixed up, your code would be
use strict;
use warnings;
my $rot = 6;
chomp(my $message = lc(<>));
$message =~ s/([a-z])/
chr( ( ord($1) - ord('a') + $rot + 26 ) % 26 + ord('a') )
/ge;
print("$message\n");
or
use strict;
use warnings;
my $rot = 6;
my $src = join '', 'a'..'z';
my $dst = substr($src, $rot) . substr($src, 0, $rot);
chomp(my $message = lc(<>));
eval "\$message =~ tr/$src/$dst/;";
print("$message\n");
or for repeated use
use strict;
use warnings;
my $rot = 6;
my $src = join '', 'a'..'z';
my $dst = substr($src, $rot) . substr($src, 0, $rot);
my $tr = eval "sub {
local \$_ = \@_ ? \$_[0] : \$_;
tr/$src/$dst/;
return \$_;
}";
chomp(my $message = lc(<>));
$message = $tr->($message);
print("$message\n");
Set $rot to -6 to "decrypt".
This method of encrpytion is particularly bad because
- the frequency of the letters remains unchanged making the cypher easy to identify and the cyphertext easy to decrypt by observing the statistical distribution of the letters, and
- the punctuation remains unencrypted, making the cyphertext easy to decrypt by observing the statistical length and organization of the words.
|