| 1 | package Crypt::Vigenere; | ||||||||||||
| 2 | |||||||||||||
| 3 | 1 | $VERSION = "0.08"; | |||||||||||
| 4 | |||||||||||||
| 5 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | |||||||||||||
| 7 | 0 | ▶ | sub new { | ||||||||||
|
Subroutine
new: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||
| 8 | 0 | my $class = shift; | |||||||||||
| 9 | 0 | ▶ | my $keyword = shift || ''; | ||||||||||
|
Condition0%0/2
shift() || ''
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||
| 10 | |||||||||||||
| 11 | 0 | ▶ | if( $keyword !~ /^[a-z]+$/ ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 12 | 0 | return; | |||||||||||
| 13 | }; | ||||||||||||
| 14 | |||||||||||||
| 15 | 0 | my $self = { | |||||||||||
| 16 | 'keyword' => $keyword, | ||||||||||||
| 17 | }; | ||||||||||||
| 18 | 0 | bless $self, $class; | |||||||||||
| 19 | |||||||||||||
| 20 | 0 | $self->_init( $keyword ); | |||||||||||
| 21 | |||||||||||||
| 22 | 0 | return( $self ); | |||||||||||
| 23 | }; | ||||||||||||
| 24 | |||||||||||||
| 25 | 0 | ▶ | sub _init { | ||||||||||
|
Subroutine
_init: not called CC=2 SCAR=17.9
| |||||||||||||
| 26 | 0 | my $self = shift; | |||||||||||
| 27 | |||||||||||||
| 28 | 0 | foreach ( split('', lc($self->{keyword})) ) { | |||||||||||
| 29 | 0 | my $ks = (ord($_)-97) % 26; | |||||||||||
| 30 | 0 | my $ke = $ks - 1; | |||||||||||
| 31 | |||||||||||||
| 32 | 0 | my ($s, $S, $e, $E); | |||||||||||
| 33 | |||||||||||||
| 34 | 0 | $s = chr(ord('a') + $ks); | |||||||||||
| 35 | 0 | $e = chr(ord('a') + $ke); | |||||||||||
| 36 | |||||||||||||
| 37 | 0 | push @{$self->{fwdLookupTable}}, "a-z/$s-za-$e"; | |||||||||||
| 38 | 0 | push @{$self->{revLookupTable}}, "$s-za-$e/a-z"; | |||||||||||
| 39 | }; | ||||||||||||
| 40 | |||||||||||||
| 41 | 0 | return( $self ); | |||||||||||
| 42 | }; | ||||||||||||
| 43 | |||||||||||||
| 44 | 0 | ▶ | sub encodeMessage { | ||||||||||
|
Subroutine
encodeMessage: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 45 | 0 | my $self = shift; | |||||||||||
| 46 | 0 | my $string = shift; | |||||||||||
| 47 | 0 | return( $self->_doTheMath($string, $self->{fwdLookupTable}) ); | |||||||||||
| 48 | }; | ||||||||||||
| 49 | |||||||||||||
| 50 | 0 | ▶ | sub decodeMessage { | ||||||||||
|
Subroutine
decodeMessage: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 51 | 0 | my $self = shift; | |||||||||||
| 52 | 0 | my $string = shift; | |||||||||||
| 53 | 0 | return( $self->_doTheMath($string, $self->{revLookupTable}) ); | |||||||||||
| 54 | }; | ||||||||||||
| 55 | |||||||||||||
| 56 | |||||||||||||
| 57 | 0 | ▶ | sub _doTheMath { | ||||||||||
|
Subroutine
_doTheMath: not called CC=3 SCAR=24.8
| |||||||||||||
| 58 | 0 | my $self = shift; | |||||||||||
| 59 | 0 | my $string = shift; | |||||||||||
| 60 | 0 | my $lookupTable = shift; | |||||||||||
| 61 | 0 | my $returnString; | |||||||||||
| 62 | |||||||||||||
| 63 | 0 | my $count = 0; | |||||||||||
| 64 | 0 | foreach( split('', lc($string)) ) { | |||||||||||
| 65 | 0 | ▶ | if( /[a-z]{1}/ ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 66 | 0 | eval "\$_ =~ tr/$lookupTable->[$count % length($self->{keyword})]/"; | |||||||||||
| 67 | 0 | $count++; | |||||||||||
| 68 | 0 | $returnString .= $_; | |||||||||||
| 69 | } | ||||||||||||
| 70 | }; | ||||||||||||
| 71 | |||||||||||||
| 72 | 0 | return( $returnString ); | |||||||||||
| 73 | }; | ||||||||||||
| 74 | |||||||||||||
| 75 | |||||||||||||
| 76 | 1 | 1; | |||||||||||
| 77 | |||||||||||||
| 78 | =head1 NAME | ||||||||||||
| 79 | |||||||||||||
| 80 | Crypt::Vigenere - Perl implementation of the Vigenere cipher | ||||||||||||
| 81 | |||||||||||||
| 82 | |||||||||||||
| 83 | =head1 SYNOPSIS | ||||||||||||
| 84 | |||||||||||||
| 85 | use Crypt::Vigenere; | ||||||||||||
| 86 | |||||||||||||
| 87 | $vigenere = Crypt::Vigenere->new( $keyword ); | ||||||||||||
| 88 | |||||||||||||
| 89 | # Encode the plaintext | ||||||||||||
| 90 | $cipher_text = $vigenere->encodeMessage( $plain_text ); | ||||||||||||
| 91 | |||||||||||||
| 92 | # Decode the ciphertext | ||||||||||||
| 93 | $plain_text = $vigenere->decodeMessage( $cipher_text ); | ||||||||||||
| 94 | |||||||||||||
| 95 | |||||||||||||
| 96 | =head1 DESCRIPTION | ||||||||||||
| 97 | |||||||||||||
| 98 | See the documentation that came with the Crypt::Vigenere package for | ||||||||||||
| 99 | more information. | ||||||||||||
| 100 | |||||||||||||
| 101 | =head2 EXPORT | ||||||||||||
| 102 | |||||||||||||
| 103 | None by default. | ||||||||||||
| 104 | |||||||||||||
| 105 | |||||||||||||
| 106 | =head1 AUTHOR | ||||||||||||
| 107 | |||||||||||||
| 108 | Alistair Mills, http://search.cpan.org/~friffin/ | ||||||||||||
| 109 | |||||||||||||
| 110 | =cut | ||||||||||||