package AtbashCipher; use strict; use warnings; use Exporter qw; our @EXPORT_OK = qw; my $ord_of_a = ord( 'a' ); my $ord_of_z = ord( 'z' ); my $ord_sum = $ord_of_a + $ord_of_z; my %plain_to_cipher; my %cipher_to_plain; # initialize hashes to store encryption and decryption keys foreach ( $ord_of_a .. $ord_of_z ) { $plain_to_cipher{ chr( $_ ) } = chr( $ord_sum - $_ ); $cipher_to_plain{ chr( $ord_sum - $_ ) } = chr( $_ ); } sub sanitize_input { my $input = shift; $input = lc $input; # convert to lower case $input =~ s/[^a-z0-9]//g; # remove non letters and numbers return [ split ( //, $input ) ]; } sub encode_atbash { my ($phrase) = @_; my @phrase = @{ sanitize_input( $phrase ) }; my $cipher; my $char_position = 1; foreach (@phrase) { $cipher .= ' ' # insert a space every 5 characters if ( $char_position > 1 && ($char_position - 1) % 5 == 0); my $new_char = /[0-9]/ ? $_ : $plain_to_cipher{ $_ }; $cipher .= $new_char; $char_position++; } return $cipher; } sub decode_atbash { my ($phrase) = @_; my @phrase = @{ sanitize_input( $phrase ) }; my $plain; $plain .= /[0-9]/ ? $_ : $cipher_to_plain{ $_ } foreach (@phrase); return $plain; } 1;