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(); # 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; } #### 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(); # 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; }