diff options
| author | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2023-03-19 15:20:59 +0100 |
|---|---|---|
| committer | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2023-03-23 18:03:43 +0100 |
| commit | e4508767cb48be672ec35bbd4bc7d1645640af4e (patch) | |
| tree | cf098637d4609c152f66cfc06ef738aa5208e5ac | |
| parent | 35cef449575d8dc586ca8d55803836a0de720a90 (diff) | |
| download | perlweeklychallenge-club-e4508767cb48be672ec35bbd4bc7d1645640af4e.tar.gz perlweeklychallenge-club-e4508767cb48be672ec35bbd4bc7d1645640af4e.tar.bz2 perlweeklychallenge-club-e4508767cb48be672ec35bbd4bc7d1645640af4e.zip | |
Challenge 025 task 2
| -rwxr-xr-x | challenge-025/jo-37/perl/ch-2.pl | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/challenge-025/jo-37/perl/ch-2.pl b/challenge-025/jo-37/perl/ch-2.pl new file mode 100755 index 0000000000..43525ac1c2 --- /dev/null +++ b/challenge-025/jo-37/perl/ch-2.pl @@ -0,0 +1,51 @@ +#!/usr/bin/perl -s + +use v5.16; +use Test2::V0; +use warnings FATAL => 'all'; +use Data::Dump qw(dd pp); +use experimental qw(signatures postderef); + +our ($plain, $cipher, $decrypt); + +die <<EOS unless $plain && $cipher && @ARGV == 1; +usage: $0 -plain=PLAIN -cipher=CIPHER [-decrypt] TEXT + +-plain=PLAIN + use PLAIN as the plain alphabet + +-cipher=CIPHER + use CIPHER as the cipher alphabet + +-decrypt + decrypt TEXT + +TEXT + en- or decrypt TEXT + +EOS + + +### Input and Output + +say chaocipher($plain, $cipher, shift, $decrypt); + + +### Implementation + +sub chaocipher ($plain, $cipher, $text, $decrypt) { + my @keys = ([split //, $plain], + [split //, $cipher]); + + my $result; + while ($text) { + my $char = substr $text, 0, 1, ''; + my $ind; + for ($ind = 0; $keys[!!$decrypt][$ind] ne $char; $ind++) {} + $result .= $keys[!$decrypt][$ind]; + $keys[0] = [($keys[0]->@[$ind + 1 .. 25, 0 .. $ind])[0, 1, 3 .. 13, 2, 14 .. 25]]; + $keys[1] = [($keys[1]->@[$ind .. 25, 0 .. $ind - 1])[0, 2 .. 13, 1, 14 .. 25]]; + + } + $result; +} |
