Skip to content

Instantly share code, notes, and snippets.

@jeffa
Created August 29, 2013 14:19
Show Gist options
  • Save jeffa/6378711 to your computer and use it in GitHub Desktop.
Save jeffa/6378711 to your computer and use it in GitHub Desktop.
Twice the power of Rot 13.
=head1 NAME
Crypt::Rot26 - Encrypt data with TWICE the power of Rot13!!
=head1 SYNOPSIS
use strict;
use Crypt::Rot26;
my $crypt = Crypt::Rot26->new();
my $bonham = 'whole lotta luv!';
my $crypted = $crypt->crypt($bonham);
print $crypted, "\n";
print $crypt->decrypt($crypted), "\n";
=cut
package Crypt::Rot26;
sub new {
my ($proto) = @_;
my $class = ref $proto || $proto;
my $self = {
rot => 26,
offset => { map { $_ => 97, uc $_ => 65 } ('a'..'z') },
};
bless $self, $class;
return $self;
}
sub crypt { do_it(@_,'+'); }
sub decrypt { do_it(@_,'-'); }
sub do_it {
my ($self,$str,$op) = @_;
my $crypted = '';
my $rotation = $self->{rot};
foreach my $chr (split('',$str)) {
if ($chr =~ /[A-Za-z]/) {
my $offset = $self->{offset}->{$chr};
my $shift = eval "$offset $op $rotation";
$chr = chr((ord($chr) - $shift) % 26 + $offset);
}
$crypted .= $chr;
}
return $crypted;
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment