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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
#!/usr/bin/perl
# https://perlweeklychallenge.org/blog/perl-weekly-challenge-025/
# Task #2
# Create script to implement Chaocipher. Please checkout wiki page for more information.
# https://en.wikipedia.org/wiki/Chaocipher
use strict;
use warnings;
my $plain = shift || 'WELLDONEISBETTERTHANWELLSAID';
print "Ciphered text: ".encipher($plain).$/;
print "Deciphered text: ".decipher(encipher($plain)).$/;
# OUTPUT
# $ ./ch-2.pl
# Ciphered text: OAHQHCNYNXTSZJRRHJBYHQKSOUJY
# Deciphered text: WELLDONEISBETTERTHANWELLSAID
sub encipher {
return chao($_[0],0);
}
sub decipher {
return chao($_[0],1);
}
sub chao {
my $text = shift;
my $decipher = shift;
my ( $left , $right ) = ( get_left() , get_right() );
my $ret = '';
for my $c (split //, uc $text) {
my $input = $decipher ? $left : $right;
my $pos = index $input,$c;
my $output = $decipher ? $right : $left;
$ret .= substr $output,$pos,1;
permute(\$left, $pos,0);
permute(\$right,$pos,1);
}
return $ret;
}
sub get_left {
return 'HXUCZVAMDSLKPEFJRIGTWOBNYQ'
};
sub get_right {
return 'PTLNBQDEOYSFAVZKGJRIHWXUMC'
};
sub permute{
my ($alpha,$pos,$right) = @_;
#shift to zenith (right alphabet shifts one more)
$$alpha = (substr $$alpha,$pos+$right) . (substr $$alpha,0,$pos+$right);
#permute upto nadir (left alphabet starts at zenith+1, right one at zenith+2)
$$alpha = (substr $$alpha,0,1+$right)
. (substr $$alpha,2+$right,12-$right)
. (substr $$alpha,1+$right,1)
. (substr $$alpha,14);
}
|