| 1 | ############################################################################### | ||||||||||||||||||||||||||||
| 2 | # Purpose : Substitution cipher based on a keyword alphabet | ||||||||||||||||||||||||||||
| 3 | # Author : John Alden | ||||||||||||||||||||||||||||
| 4 | # Created : Jan 2005 | ||||||||||||||||||||||||||||
| 5 | # CVS : $Id: KeywordAlphabet.pm,v 1.5 2005/03/20 20:02:11 aldenj20 Exp $ | ||||||||||||||||||||||||||||
| 6 | ############################################################################### | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | package Text::Cipher::KeywordAlphabet; | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use Text::Cipher; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use vars qw($VERSION $AUTOLOAD); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 2 | $VERSION = sprintf "%d.%03d", (q$Revision: 1.5 $ =~ /: (\d+)\.(\d+)/); | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 7 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 17 | 7 | my ($class, $keywords, $offset) = @_; | |||||||||||||||||||||||||||
| 18 | 7 | ▶ | croak("offset must be an integer") unless($offset =~ /^\-?\d*$/); #Integer or blank | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 19 | 6 | my %seen; | |||||||||||||||||||||||||||
| 20 | 6 | ▶ | my $alphabet = join("", map {uc} grep {/^[a-z]$/i && !$seen{$_}++} (split //, $keywords), 'a'..'z'); | ||||||||||||||||||||||||||
|
Condition100%3/3
/^[a-z]$/i && !$seen{$_}++
MC/DC100%2/2
/^[a-z]$/i && !$seen{$_}++
/^[a-z]$/i
!$seen{$_}++
Truth table100%3/3
A && not(B)
A= /^[a-z]$/iB= !$seen{$_}++
| |||||||||||||||||||||||||||||
| 21 | 6 | $alphabet = _rotate_alphabet($alphabet, $offset); | |||||||||||||||||||||||||||
| 22 | my $self = { | ||||||||||||||||||||||||||||
| 23 | 6 | cipher => new Text::Cipher(join("", 'A'..'Z', 'a'..'z'), join("", $alphabet, map {lc} $alphabet)), | |||||||||||||||||||||||||||
| 24 | 6 | decipher => new Text::Cipher(join("", $alphabet, map {lc} $alphabet), join("", 'A'..'Z', 'a'..'z')), | |||||||||||||||||||||||||||
| 25 | alphabet => $alphabet | ||||||||||||||||||||||||||||
| 26 | }; | ||||||||||||||||||||||||||||
| 27 | 6 | return bless($self, $class); | |||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 6 | ▶ | sub alphabet { | ||||||||||||||||||||||||||
|
Subroutine
alphabet: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 31 | 6 | my ($self) = shift; | |||||||||||||||||||||||||||
| 32 | 6 | return $self->{alphabet}; | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 8 | ▶ | sub AUTOLOAD { | ||||||||||||||||||||||||||
|
Subroutine
AUTOLOAD: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 36 | 8 | my ($self) = shift; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | # DESTROY messages should never be propagated | ||||||||||||||||||||||||||||
| 39 | 8 | ▶ | return if $AUTOLOAD =~ /::DESTROY$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | # Remove the package name | ||||||||||||||||||||||||||||
| 42 | 2 | my $package = __PACKAGE__; | |||||||||||||||||||||||||||
| 43 | 2 | $AUTOLOAD =~ s/^${package}:://; | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | # Pass on to either the enciphering or deciphering object | ||||||||||||||||||||||||||||
| 46 | 2 | ▶ | if($AUTOLOAD =~ /^decipher/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 1 | $AUTOLOAD =~ s/^decipher/encipher/; | |||||||||||||||||||||||||||
| 48 | 1 | $self->{decipher}->$AUTOLOAD(@_); | |||||||||||||||||||||||||||
| 49 | } else { | ||||||||||||||||||||||||||||
| 50 | 1 | $self->{cipher}->$AUTOLOAD(@_); | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | # Based on routine in Text::Shift | ||||||||||||||||||||||||||||
| 55 | 6 | ▶ | sub _rotate_alphabet { | ||||||||||||||||||||||||||
|
Subroutine
_rotate_alphabet: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 56 | # Get parameters | ||||||||||||||||||||||||||||
| 57 | 6 | my($string,$mag) = @_; | |||||||||||||||||||||||||||
| 58 | 6 | my $strlng = length($string); | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | # Handle outliers | ||||||||||||||||||||||||||||
| 61 | 6 | ▶ | $mag %= $strlng if(abs($mag) > $strlng or $mag < 0); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
abs $mag > $strlng or $mag < 0
MC/DC100%2/2
abs $mag > $strlng or $mag < 0
abs $mag > $strlng
$mag < 0
Truth table100%3/3
A or B
A= abs $mag > $strlngB= $mag < 0
| |||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | # Return rotated string | ||||||||||||||||||||||||||||
| 64 | 6 | ▶ | return $string if($mag == 0); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 4 | $string .= substr($string,0,$mag, ""); | |||||||||||||||||||||||||||
| 66 | 4 | return $string; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 2 | 1; | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | =head1 NAME | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | Text::Cipher::KeywordAlphabet - Substitution cipher based on a keyword alphabet | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | #Create a keyword alphabet with a left shift of 5 | ||||||||||||||||||||||||||||
| 78 | $cipher = new Text::Cipher::KeywordAlphabet("the quick brown fox", -5); | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | #Fetch the generated alphabet | ||||||||||||||||||||||||||||
| 81 | $keyword_alphabet = $cipher->alphabet(); | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | #Encipher a string | ||||||||||||||||||||||||||||
| 84 | $ciphered = $cipher->encipher($message); | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | #Decipher an enciphered message | ||||||||||||||||||||||||||||
| 87 | $message = $cipher->decipher($ciphered); | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | #Some convenience methods | ||||||||||||||||||||||||||||
| 90 | $cipher->encipher_scalar(\$some_scalar); | ||||||||||||||||||||||||||||
| 91 | $cipher->decipher_scalar(\$some_scalar); | ||||||||||||||||||||||||||||
| 92 | @ciphered = $cipher->encipher_list(@list); | ||||||||||||||||||||||||||||
| 93 | @list = $cipher->decipher_list(@ciphered); | ||||||||||||||||||||||||||||
| 94 | $cipher->encipher_array(\@some_array); | ||||||||||||||||||||||||||||
| 95 | $cipher->decipher_array(\@some_array); | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | #Other uses | ||||||||||||||||||||||||||||
| 98 | $null_cipher = new Text::Cipher::KeywordAlphabet(); #no-op cipher | ||||||||||||||||||||||||||||
| 99 | $rot13_cipher = new Text::Cipher::KeywordAlphabet(undef, 13); #Caesar cipher | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | This module generates a monoalphabetic substitution cipher from a set of words, resulting in what's sometimes referred to as a "keyword (generated) alphabet". | ||||||||||||||||||||||||||||
| 104 | Here's a good definition, plagiarised from an anonymous source: | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | "A keyword alphabet is formed by taking a word or phrase, deleting the second and subsequent occurrence of each letter and then writing the remaining letters of the alphabet in order. | ||||||||||||||||||||||||||||
| 107 | Encipherment is achieved by replacing each plaintext letter by the letter that appears N letters later in the (cyclic) keyword alphabet." | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | The keyword alphabet is case-insensitive - both uppercase and lowercase characters will be transformed with the same mapping. | ||||||||||||||||||||||||||||
| 110 | The offset (N in the definition above) can be a positive or negative integer. | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | L<http://www.trincoll.edu/depts/cpsc/cryptography/substitution.html> is an introductory tutorial on how substitution ciphers can be broken. | ||||||||||||||||||||||||||||
| 113 | L<http://www-math.cudenver.edu/~wcherowi/courses/m5410/exsubcip.html> contains a full worked example. | ||||||||||||||||||||||||||||
| 114 | L<http://www.muth.org/Robert/Cipher/query_scb.html> provides an online substitution cipher breaker. | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | At the risk of stating the obvious, since substitution ciphers are easy to break, it's advisable not to use them for protecting important data. | ||||||||||||||||||||||||||||
| 117 | Look at some of the more heavy-duty ciphers in the Crypt:: namespace which plug into Crypt::CBC if you want to protect data. | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | =head1 METHODS | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | =over 4 | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | =item $obj = new Text::Cipher::KeywordAlphabet($keyword_phrase, $offset) | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | Create a new keyword alphabet | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | =item $keyword_alphabet = $obj->alphabet(); | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | Return the keyword alphabet created by the constructor | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | =item $ciphered = $obj->encipher($message) | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | Enciphers a string using the keyword alphabet | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | =item $message = $obj->decipher($ciphered) | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | Reverse of encipher() | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | =item $obj->encipher_scalar(\$some_scalar); | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | By-reference equivalent of encipher() | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | =item $obj->decipher_scalar(\$some_scalar); | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | By-reference equivalent of decipher() | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | =item @ciphered = $obj->encipher_list(@list); | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | Convenience method provided by Text::Cipher | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | =item @list = $obj->decipher_list(@ciphered); | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | Reverse of encipher_list(). | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | =item $obj->encipher_array(\@some_array); | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | Convenience method provided by Text::Cipher | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | =item $obj->decipher_array(\@some_array); | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | Reverse of encipher_array(). | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | =back | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | =head1 VERSION | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | See $Text::Cipher::KeywordAlphabet::VERSION. | ||||||||||||||||||||||||||||
| 168 | Last edit: $Revision: 1.5 $ on $Date: 2005/03/20 20:02:11 $ | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | =head1 BUGS | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | None known. This module has not been used heavily in production so it's not impossible a bug may have slipped through the unit tests. | ||||||||||||||||||||||||||||
| 173 | Bug reports are welcome, particularly with patches & test cases. | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | John Alden <johna@cpan.org> | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | =over 4 | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | =item Text::Cipher and Regexp::Tr | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | Useful building blocks for substitution ciphers | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | =item Text::Shift and Crypt::Rot13 | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | Caesar (aka shift or rot-N) ciphers (see L<http://www.trincoll.edu/depts/cpsc/cryptography/caesar.html>) | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | =item Crypt::Caesar | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | Crack Caesar ciphers using letter frequency (see L<http://www.trincoll.edu/depts/cpsc/cryptography/caesar.html>) | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | =item Crypt::Vigenere | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | Vigenere polyalphabetic cipher (see L<http://www.trincoll.edu/depts/cpsc/cryptography/vigenere.html>) | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | =item Crypt::Enigma and Crypt::OOEnigma | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | Implementations of Enigma ciphers (see L<http://www.trincoll.edu/depts/cpsc/cryptography/enigma.html>) | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | =back | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | Copyright 2005 by John Alden | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | =cut | ||||||||||||||||||||||||||||