Crypt::OTP26 - a classic form of encryption
This implements a mod-26 One Time Pad encryption, similar to the sort classically used with pen and paper, as described in http://en.wikipedia.org/wiki/One_time_pad
Its primary use is to explore the intriguing situation detailed at http://itre.cis.upenn.edu/~myl/languagelog/archives/003314.html
NB: We don't handle the cases of generating or securely transmitting the pads themselves.
Also, only lower case alpha (ascii 'a-z') characters are handled. If you are actually encrypting and transmitting useful and valuable data, you should use a proper strong crypto module.
And though it's based on the OneTimePad concept, it actually supports the pad being shorter or longer than the encrypted text, in which case it is truncated or repeated as appropriate. So... don't do that!
my $otp = Crypt::OTP26->new();
Encrypts an alpha text (a-z) with an alpha pad (a-z), by performing mod26 addition on it.
my $encrypted = $otp->crypt( $pad, $text ); # though it's commutative, so can be in either order my $encrypted = $otp->crypt( 'aced', 'scam' ); # returns 'seep'
Decrpyts a previously encrypted text using mod26 sutraction.
my $encrypted = $otp->decrypt( $crypt, $pad ); my $encrypted = $otp->decrypt( 'aced', 'seep' ); # returns 'scam'
Return the mod26 integer value of an ascii character.
my $int = $otp->char2int('a'); # returns 0
my $char = $otp->int2char( 1 ); # returns 'b'
Will always return 'a'-'z'
my $char = $otp->crypt_char( 'a', 's' ); # returns 's'
Crypts 2 characters by performing mod26 addition on them. Called internally by crypt above.
Decrypts the character with the appropriate letter from the pad, by performing mod26 subtraction. Called internally decrypt above.
my $char = $otp->decrypt_char( $crypt_char, $pad_char ); my $char = $otp->decrypt_char( 't', 's' ); # returns 'b'
Private method for iterating the pad and the string.
(C) 2009 firstname.lastname@example.org May be distributed under the same conditions as Perl itself
Repo is at http://github.com/osfameron/crypt-otp26/
(Clone url: git://github.com/osfameron/crypt-otp26.git )