#!/usr/bin/perl use strict; use Getopt::Std; # name: transpos-decipher # author: ap0calypse (ap0calypse@agitatio.org) # version: 0.1 (2008-06-17) # license: DO WHATEVER YOU WANT! # purpose: reads a sentence from STDIN an returns a # (probably) deciphered sentence. It depends on how high # the cipher-sentence-fence-height was, so you maybe have to try # very often to get results. our ($opt_c, $opt_h); getopt('hc:'); sub usage { print << "EOUSAGE"; usage: ====== echo "im akenose nse" | transpos-decipher -c NUM - NUM must be a numeric value which represents the "height" of the "fence" example: ======== echo "io apion vtnotnglerssii" | transpos-decipher -c 3 will result in following output: [i love transpositioning] explanation: ============ This is because the ciphered sentence was transformed like this: [i] [o] [ ] [a] [p] [i] [o] [n] -> line 1 [ ] [v] [t] [n] [o] [t] [n] [g] -> line 2 [l] [e] [r] [s] [s] [i] [i] -> line 3 ========================================================================= For further information search the web for transposition or visit my blog: http://ap0calypse.agitatio.org Have fun! EOUSAGE } if ($opt_h) { &usage; exit 2; } if ($opt_c !~ m/^\d+$/) { &usage; exit 1; } chomp (my $input = <>); my @a_input = split //, $input; my $fenceheight = $opt_c; my $str_len = length $input; my $overhead = $str_len % $fenceheight; my $std_len = ($str_len - $overhead) / $fenceheight; my $parts = ($str_len - $overhead) / $std_len; my $AoA = [[]]; my ($i, $j, @final_array) = (0, 0, qw()); for (1 .. $parts) { for (1 .. $overhead) { for (0 .. $std_len) { push @{$AoA->[$i]}, shift @a_input; } $i++; } for (0 .. $std_len-1) { push @{$AoA->[$i]}, shift @a_input; } $i++; } while ($j < $str_len) { for (0 .. $parts-1) { $final_array[$j] = shift @{$AoA->[$_]}; $j++; } } print "["; print join "", @final_array; print "]\n";