forked from manwar/perlweeklychallenge-club
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ch-2.p6
executable file
·56 lines (44 loc) · 1.16 KB
/
ch-2.p6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/perl6
sub decrypt(@key, $keylength, %tabulaRecta, $c) {
state $i = 0;
return chr(ord('A') + index(%tabulaRecta{@key[$i++ % $keylength]}, $c));
}
sub encrypt(@key, $keylength, %tabulaRecta, $c) {
state $i = 0;
return substr(%tabulaRecta{@key[$i++ % $keylength]}, ord($c) - ord('A'), 1);
}
sub makeSquare {
return (0 .. 25)
.map({ ['A' .. 'Z'].rotate($_).join; })
.map({ $_.substr(0, 1) => $_});
}
sub prep {
my ($string) = @_;
$string = uc $string;
$string ~~ s:g/<-[A .. Z]>//;
return $string;
}
sub vigenère($k, $m, &op) {
my %tabulaRecta = makeSquare();
my @key = prep($k).comb;
my $keylength = @key.elems;
my @message = prep($m).comb;
@message
.map({ op(@key, $keylength, %tabulaRecta, $_) })
.join
.say;
}
multi sub MAIN (
Bool :$d!, #= decrypt a message
Str :$k!,
Str :$m!
) {
vigenère($k, $m, &decrypt);
}
multi sub MAIN (
Bool :$e!, #= encrypt a message
Str :$k!, #= key for encryption/decryption
Str :$m! #= message to encrypt/decrypt
) {
vigenère($k, $m, &encrypt);
}