Hello everybody, my name is Paul and I'm beginner in perl language. I'm doing a college work on the transposition cipher, and decided to do in Perl, because I enjoyed the language, but I'm having some problems of logic to solve this in the Perl language, could you guys could help me? I wrote an encryptor and decryptor transposition perl, apparently the encryption is working perfectly, but the decryption is not working, would you orient me about this problem? Follow the code below

Transposition Cipher
open KEYWORD, "<", "key.txt" or die $!; open CLEANTEXT, "<", "cleanText.txt" or die $!; open CIPHERTEXT, ">", "cipherText.txt" or die $!; my ($data, $n, $offset); my @cleanTextArray; while(($n = read CLEANTEXT, $data, 1)!=0) { push @cleanTextArray, $data; } my $textSize = $#cleanTextArray; $key = &to26(<KEYWORD>); # Get keys from command line # and transform to upper case. #$option = $ARGV[1]; $keylength = length($key); print "Length of key: $keylength --- "; @keylist = split(//, $key); # Split key into single # characters. print "Key: "; foreach $k (@keylist) {print "$k ";} print "\n"; $permut[0] = 0; for ($i = 1; $i < $keylength; $i++) { $j = 0; $found = 0; while (!$found) { if (($j >= $i) || ($keylist[$i] lt $keylist[$permut[$j]])) { splice(@permut, $j, 0, $i); # Insert $i at position $j $found = 1;} else {$j++;} } } print "Permutation:"; foreach $p (@permut) {print " $p";} print "\n"; my $lines = $keylength; my $columns = int($textSize/$keylength); if (($textSize % $keylength) > 0) { $columns += 1; } $matrixSize = $columns*$lines; print "Number of rows: $colums -- size of tableau: $matrixSize\n"; for ($i = $textSize; $i < $matrixSize; $i++) {$cleanTextArray[$i] = "0 +";} print "Arrangement of plaintext in columns:\n"; for ($i = 0; $i < $columns; $i++) { for ($j = 0; $j < $lines; $j++) { print "$cleanTextArray[$lines*$i + $j] "; } print "\n"; } for ($i = 0; $i < $columns; $i++) { for ($j = 0; $j < $lines; $j++) { print CIPHERTEXT "$cleanTextArray[$lines*$i + $ permut[$j]]"; } print "\n"; } close KEYWORD; close CLEANTEXT; close CIPHERTEXT; sub to26 { local($mytext) = $_[0]; # Get input string. # $mytext =~ tr/a-z/A-Z/; # Translate to upper case. $mytext =~ s/0/zero/g; # Numbers to verbal description. $mytext =~ s/1/um/g; $mytext =~ s/2/dois/g; $mytext =~ s/3/tres/g; $mytext =~ s/4/quatro/g; $mytext =~ s/5/cinco/g; $mytext =~ s/6/seis/g; $mytext =~ s/7/sete/g; $mytext =~ s/8/oito/g; $mytext =~ s/9/nove/g; return $mytext; }
Transposition Decipher
open KEYWORD, "<", "key.txt" or die $!; open CIPHERTEXT, "<", "cipherText.txt" or die $!; open DECIPHERTEXT, ">", "decipherText.txt" or die $!; my ($data, $n, $offset); my @cipherTextArray; while(($n = read CIPHERTEXT, $data, 1)!=0) { push @cipherTextArray, $data; } my $textSize = $#cipherTextArray; $key = &to26(<KEYWORD>); # Get keys from command line # and transform to upper case. #$option = $ARGV[1]; $keylength = length($key); print "Length of key: $keylength --- "; @keylist = split(//, $key); # Split key into single # characters. print "Key: "; foreach $k (@keylist) {print "$k ";} print "\n"; $permut[0] = 0; for ($i = 1; $i < $keylength; $i++) { $j = 0; $found = 0; while (!$found) { if (($j >= $i) || ($keylist[$i] lt $keylist[$permut[$j]])) { splice(@permut, $j, 0, $i); # Insert $i at position $j $found = 1;} else {$j++;} } } $invpermut[0] = 0; for ($i = 1; $i < $keylength; $i++) { $j = 0; $found = 0; while (!$found) { if (($j >= $i) || ($permut[$i] lt $permut[$invpermut[$j]])) { splice(@invpermut, $j, 0, $i); # Insert $i at position $j $found = 1;} else {$j++;} } } print "Permutation:"; foreach $p (@permut) {print " $p";} print "\n"; print "Inverse Permutation:"; foreach $p (@invpermut) {print " $p";} print "\n"; my $lines = $keylength; my $columns = int($textSize/$keylength); if (($textSize % $keylength) > 0) { $columns += 1; } $matrixSize = $columns*$lines; print "Number of rows: $colums -- size of tableau: $matrixSize\n"; for ($i = $textSize; $i < $matrixSize; $i++) {$cipherTextArray[$i] = " +0";} print "Arrangement of ciphertext in columns:\n"; for ($i = 0; $i < $columns; $i++) { for ($j = 0; $j < $lines; $j++) { print "$cipherTextArray[$lines*$i + $permut[$j]] "; } print "\n"; } print "Arrangement of deciphertext in columns: \n"; for ($i = 0; $i < $columns; $i++) { for ($j = 0; $j < $lines; $j++) { print "$cipherTextArray[$lines*$i + $invpermut[$j]]"; } print "\n"; } close KEYWORD; close CIPHERTEXT; close DECIPHERTEXT; sub to26 { local($mytext) = $_[0]; # Get input string. # $mytext =~ tr/a-z/A-Z/; # Translate to upper case. $mytext =~ s/0/zero/g; # Numbers to verbal description. $mytext =~ s/1/um/g; $mytext =~ s/2/dois/g; $mytext =~ s/3/tres/g; $mytext =~ s/4/quatro/g; $mytext =~ s/5/cinco/g; $mytext =~ s/6/seis/g; $mytext =~ s/7/sete/g; $mytext =~ s/8/oito/g; $mytext =~ s/9/nove/g; return $mytext; }
cleanTxt.txt contains "ola eu sou o paulo 1234567890" key.txt contains "secretmachine"

In reply to Transposition Cipher by pauloesb

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.