#!/usr/bin/perl use strict; defined ($ARGV[1]) or die "Usage: machine.pl \n"; my (@rotor_one, @rotor_two, @rotor_three, @rotor_four, $logfile, @rotor_five, @rotor_six, @rotor_seven, $first, $second, $third, $fourth, $fifth, $sixth, $seventh, $size_one, $size_two, $size_three, $size_four, $size_five, $size_six, $size_seven, $answer, $A, $B, $C, $D, $E, $F, $G, $temp, @K, @S, $i, $k, $j, $c, $t, $open_me, $doclength, $print_loop, $count, $add, $buf); $logfile = "unmachine.log"; open (O,">".$ARGV[1]) or die "Output file open failed: $!\n"; open (L,">".$logfile) or die "Output file open failed: $!\n"; print ("start A [<4]: "); $first = ; chomp ($first); print ("start B [<7]: "); $second = ; chomp ($second); print ("start C [<11]: "); $third = ; chomp ($third); print ("start D [<13]: "); $fourth = ; chomp ($fourth); print ("start E [<17]: "); $fifth = ; chomp ($fifth); print ("start F [<19]: "); $sixth = ; chomp ($sixth); print ("start G [<23]: "); $seventh = ; chomp ($seventh); open(IN,"$ARGV[0]") || die "Sorry. Couldn't open: $!\n"; $doclength = 0; while (read (IN, $buf, 1)) { if ($buf ne "\n") { $doclength++; } } print ("$ARGV[0] has $doclength characters.\n"); close (IN); open(IN,"$ARGV[0]") || die "Sorry. Couldn't open: $!\n"; ### TESTING ### print "Key: "; $k = ; chomp ($k); # Initialize the state array: for ($i = 0; $i < 94; $i++) { $S[$i] = $i; } $j = 0; $t = length($k); $buf = 0; # Initialize the key array: for ($i = 0; $i < 94; $i++) { $buf = (substr($k,$j,1)); $K[$i] = ord($buf); $j = ($j + 1) % $t; } # mix the state and the key for ($i = 0; $i < 94; $i++) { $buf = $S[$i]; $j = ($j + $S[$i] + $K[$i]) % 94; $t = $S[$j]; $S[$i] = $S[$j]; $S[$j] = $t; } #### End Of Experimental ### $size_one = 4; $size_two = 7; $size_three = 11; $size_four = 13; $size_five = 17; $size_six = 19; $size_seven = 23; $i = 0; for ($count = 0; $count < $size_one; $count++) { $rotor_one[$count] = $S[$i]; $i++; } for ($count = 0; $count < $size_two; $count++) { $rotor_two[$count] = $S[$i]; $i++; } for ($count = 0; $count < $size_three; $count++) { $rotor_three[$count] = $S[$i]; $i++; } for ($count = 0; $count < $size_four; $count++) { $rotor_four[$count] = $S[$i]; $i++; } for ($count = 0; $count < $size_five; $count++) { $rotor_five[$count] = $S[$i]; $i++; } for ($count = 0; $count < $size_six; $count++) { $rotor_six[$count] = $S[$i]; $i++; } for ($count = 0; $count < $size_seven; $count++) { $rotor_seven[$count] = $S[$i]; $i++; } # for testing print L ("\nafter mixing, the rotors looks like this:\n"); $i = 1; for ($count = 0; $count < $size_one; $count++) { print L ("one[$count] : $rotor_one[$count]\n"); } for ($count = 0; $count < $size_two; $count++) { print L ("two[$count] : $rotor_two[$count]\n"); } for ($count = 0; $count < $size_three; $count++) { print L ("three[$count] : $rotor_three[$count]\n"); } for ($count = 0; $count < $size_four; $count++) { print L ("four[$count] : $rotor_four[$count]\n"); } for ($count = 0; $count < $size_five; $count++) { print L ("five[$count] : $rotor_five[$count]\n"); } for ($count = 0; $count < $size_six; $count++) { print L ("six[$count] : $rotor_six[$count]\n"); } for ($count = 0; $count < $size_seven; $count++) { print L ("seven[$count] : $rotor_seven[$count]\n"); } #end for testing $A = $first; $B = $second; $C = $third; $D = $fourth; $E = $fifth; $F = $sixth; $G = $seventh; $temp = 1; for ($count = 1; $count <= $doclength; $count++) { $print_loop++; read (IN, $buf, 1); while ($buf eq "\n") { read (IN, $buf, 1); } $buf = ord($buf) - 32; $add = ((3*$rotor_one[$A]) + (5*$rotor_two[$B]) + (7*$rotor_three[$C]) + (11*$rotor_four[$D]) + (13*$rotor_five[$E]) + (17*$rotor_six[$F]) + (19*$rotor_seven[$G]) + ($temp % 17)); $add = ($add % 94); $answer = ($buf - $add) % 94; $answer = chr($answer + 32); print O ("$answer"); # print O (chr($add + 32)); if ($print_loop == 70) { print O ("\n"); $print_loop = 0; } $A = $count % $size_one; # fix these $B = $count % $size_two; # so they don't all move each time $C = $count % $size_three; # or is this way better? $D = $count % $size_four; $E = $count % $size_five; $F = $count % $size_six; $G = $count % $size_seven; $temp = $add } print ("\n"); close (IN); close(O);