| 1 | package Crypt::Caesar; | ||||||||||
| 2 | 1 | ▶ | use base 'Exporter'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use vars qw($VERSION @EXPORT); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 1 | $VERSION = '0.01'; | |||||||||
| 7 | 1 | @EXPORT = qw(caesar); | |||||||||
| 8 | |||||||||||
| 9 | 1 | my %weight = qw( | |||||||||
| 10 | a 7.97 b 1.35 c 3.61 d 4.78 e 12.37 f 2.01 g 1.46 h 4.49 | ||||||||||
| 11 | i 6.39 j 0.04 k 0.42 l 3.81 m 2.69 n 5.92 o 6.96 p 2.91 | ||||||||||
| 12 | q 0.08 r 6.63 s 8.77 t 9.68 u 2.62 v 0.81 w 1.88 x 0.23 | ||||||||||
| 13 | y 2.07 z 0.06 | ||||||||||
| 14 | ); | ||||||||||
| 15 | |||||||||||
| 16 | 1 | $_ = log($_) + log(26/100) for values %weight; | |||||||||
| 17 | |||||||||||
| 18 | 3 | ▶ | sub caesar ($) { | ||||||||
|
Subroutine
caesar: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||
| 19 | 3 | my ($string) = @_; | |||||||||
| 20 | 3 | my $copy = lc $string; | |||||||||
| 21 | 3 | $copy =~ tr/a-z//cd; | |||||||||
| 22 | 3 | ▶ | return $string unless length $copy; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 23 | 2 | my $winner = 0; | |||||||||
| 24 | 2 | my $winscore = 0; | |||||||||
| 25 | 2 | for my $i (1..26) { | |||||||||
| 26 | 52 | my $score = 0; | |||||||||
| 27 | 52 | $copy =~ tr/a-z/b-za/; | |||||||||
| 28 | 52 | $score += $weight{$_} for split //, $copy; | |||||||||
| 29 | 52 | ▶ | if ($score > $winscore) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 30 | 3 | $winner = $i; | |||||||||
| 31 | 3 | $winscore = $score; | |||||||||
| 32 | } | ||||||||||
| 33 | } | ||||||||||
| 34 | 2 | $string =~ tr/A-Za-z/B-ZAb-za/ for 1..$winner; | |||||||||
| 35 | 2 | return $string; | |||||||||
| 36 | } | ||||||||||
| 37 | |||||||||||
| 38 | 1 | 1; | |||||||||
| 39 | |||||||||||
| 40 | =head1 NAME | ||||||||||
| 41 | |||||||||||
| 42 | Crypt::Caesar - Decrypt rot-N strings | ||||||||||
| 43 | |||||||||||
| 44 | =head1 SYNOPSIS | ||||||||||
| 45 | |||||||||||
| 46 | use Crypt::Caesar; | ||||||||||
| 47 | print caesar("Vn tjp xvi nzz, do rjmfn.\n"); | ||||||||||
| 48 | |||||||||||
| 49 | =head1 DESCRIPTION | ||||||||||
| 50 | |||||||||||
| 51 | This module is based on the caesar utility from the bsdgames package, made by | ||||||||||
| 52 | Stan King and John Eldridge, based on the algorithm suggested by Bob Morris. | ||||||||||
| 53 | |||||||||||
| 54 | The caesar utility attempts to decrypt caesar cyphers using English letter | ||||||||||
| 55 | frequency statistics. | ||||||||||
| 56 | |||||||||||
| 57 | =head2 C<caesar> | ||||||||||
| 58 | |||||||||||
| 59 | This is the only function this package provides. It is exported by default and | ||||||||||
| 60 | prototyped C<($)>. | ||||||||||
| 61 | |||||||||||
| 62 | =head1 AUTHOR | ||||||||||
| 63 | |||||||||||
| 64 | Juerd | ||||||||||
| 65 | |||||||||||
| 66 | =cut | ||||||||||
| 67 | |||||||||||