| 1 | #!/usr/bin/perl -w | ||
| 2 | use strict; | ||
| 3 | use Crypt::Enigma; | ||
| 4 | |||
| 5 | # Make sure that the output is not buffered... | ||
| 6 | $| = 1; | ||
| 7 | |||
| 8 | |||
| 9 | # Check our input. | ||
| 10 | if (!@ARGV) { | ||
| 11 | print "Usage: $0 <plaintext> <cipertext> <loop positions> <num rotors>\n"; | ||
| 12 | print "Example: $0 akldfaa kdkdkdkd 1.2.3 3\n"; | ||
| 13 | exit (); | ||
| 14 | } | ||
| 15 | |||
| 16 | |||
| 17 | # Get our input from the command line. | ||
| 18 | my (@plaintext) = split (//, $ARGV[0]); | ||
| 19 | my (@ciphertext) = split (//, $ARGV[1]); | ||
| 20 | my (@loops) = split (/\./, $ARGV[2]); | ||
| 21 | my ($num_rotors) = $ARGV[3]; | ||
| 22 | |||
| 23 | |||
| 24 | # Find all possible $num_rotors combinations of 12345. | ||
| 25 | my (@array) = qw (1 2 3 4 5); | ||
| 26 | my (@combos) = find_combos (\@array, $num_rotors); | ||
| 27 | |||
| 28 | # Build our initial enigma settings... | ||
| 29 | # We start with all a's... | ||
| 30 | my ($isetting) = ""; | ||
| 31 | my ($i); | ||
| 32 | foreach $i (1 .. $num_rotors) { | ||
| 33 | $isetting .= "a"; | ||
| 34 | } | ||
| 35 | |||
| 36 | |||
| 37 | # For each possible combination of rotors... | ||
| 38 | my ($combo); | ||
| 39 | foreach $combo (@combos) { | ||
| 40 | |||
| 41 | # Initialize our views array... | ||
| 42 | my (@views) = (); | ||
| 43 | |||
| 44 | # Initalize our group of enigmas. | ||
| 45 | my (%enigmas); | ||
| 46 | # Create an enigma for each link in the loop... | ||
| 47 | my ($link); | ||
| 48 | foreach $link (@loops) { | ||
| 49 | |||
| 50 | $enigmas{$link} = Crypt::Enigma->new(); | ||
| 51 | $enigmas{$link}->setup($combo, $isetting, $isetting); | ||
| 52 | |||
| 53 | # Advance this enigma the proper number of turns... | ||
| 54 | my ($i); | ||
| 55 | for ($i = $link; $i > 0; $i--) { | ||
| 56 | |||
| 57 | # We only need to remember the views of | ||
| 58 | # the first enigma. | ||
| 59 | if ($link eq $loops[0]) { | ||
| 60 | # Look at the enigma, and remember | ||
| 61 | # each view for each forward rotation. | ||
| 62 | my ($view) = $enigmas{$link}->view(); | ||
| 63 | push (@views, $view); | ||
| 64 | } | ||
| 65 | |||
| 66 | # Advance the enigma... | ||
| 67 | $enigmas{$link}->advance_rotors(); | ||
| 68 | } | ||
| 69 | } | ||
| 70 | |||
| 71 | # Store the final view for loops[0] in @views... | ||
| 72 | my ($view) = $enigmas{$loops[0]}->view(); | ||
| 73 | push (@views, $view); | ||
| 74 | |||
| 75 | # Next, using the enigma setup we have created, | ||
| 76 | # attempt the encryption. If we get #@loops | ||
| 77 | # matches, then we have found a state in which | ||
| 78 | # all links of the loop are true. | ||
| 79 | |||
| 80 | print "Scanning rotor combination $combo\n"; | ||
| 81 | |||
| 82 | # For every possible positional combination of rotors... | ||
| 83 | # 26^num_rotors... | ||
| 84 | foreach $i (1 .. (26**$num_rotors)) { | ||
| 85 | |||
| 86 | # Initialize the number of successful input/output matches. | ||
| 87 | my ($num_matches) = 0; | ||
| 88 | |||
| 89 | # Step though each enigma for this combination | ||
| 90 | foreach $link (sort (keys (%enigmas))) { | ||
| 91 | |||
| 92 | # We only need to remember the views of the | ||
| 93 | # first enigma... | ||
| 94 | if ($link eq $loops[0]) { | ||
| 95 | # remove the first in @views, | ||
| 96 | # and put the current view into @views. | ||
| 97 | |||
| 98 | shift (@views); | ||
| 99 | $view = $enigmas{$link}->view(); | ||
| 100 | push (@views, $view); | ||
| 101 | } | ||
| 102 | |||
| 103 | # Figure out the input and the output we are looking for. | ||
| 104 | my ($input) = $plaintext[$link]; | ||
| 105 | my ($output) = $ciphertext[$link]; | ||
| 106 | |||
| 107 | # Encrypt the input... | ||
| 108 | my ($result) = $enigmas{$link}->input($input); | ||
| 109 | |||
| 110 | # If the result we get is the output we were | ||
| 111 | # looking for, add one to num_matches. | ||
| 112 | if ($output eq $result) { | ||
| 113 | $num_matches++; | ||
| 114 | } | ||
| 115 | } | ||
| 116 | |||
| 117 | # Only update the user on results if we have $#loops | ||
| 118 | # or greater matches. | ||
| 119 | if ($num_matches >= $#loops) { | ||
| 120 | print "$num_matches"; | ||
| 121 | |||
| 122 | # And if we have $#loops + 1 matches (all)... | ||
| 123 | if ($num_matches == ($#loops + 1)) { | ||
| 124 | # print the good news. | ||
| 125 | print "\nMatch for rotor combo $combo - try inital of $views[0]\n"; | ||
| 126 | } | ||
| 127 | } | ||
| 128 | } | ||
| 129 | } | ||
| 130 | |||
| 131 | |||
| 132 | sub find_combos { | ||
| 133 | my ($arrayref) = $_[0]; | ||
| 134 | my ($combo_length) = $_[1]; | ||
| 135 | my (@combos) = (); | ||
| 136 | |||
| 137 | my (@array) = @$arrayref; | ||
| 138 | |||
| 139 | # if the combo_length is 1 - then we have no work to do... | ||
| 140 | if ($combo_length == 1) { | ||
| 141 | return (@array); | ||
| 142 | } | ||
| 143 | |||
| 144 | foreach $i (0 .. $#array) { | ||
| 145 | my ($char) = $array[$i]; | ||
| 146 | my (@temp_array) = @array; | ||
| 147 | my ($new_length) = $combo_length - 1; | ||
| 148 | |||
| 149 | # Remove the current element from the array... | ||
| 150 | splice (@temp_array, $i, 1); | ||
| 151 | |||
| 152 | # call find_combos again with the new, shorter array... | ||
| 153 | my (@results) = find_combos (\@temp_array, $new_length); | ||
| 154 | |||
| 155 | # Push the results into @combos | ||
| 156 | my ($result); | ||
| 157 | foreach $result (@results) { | ||
| 158 | push (@combos, "$char$result"); | ||
| 159 | } | ||
| 160 | } | ||
| 161 | |||
| 162 | # And return the results... | ||
| 163 | return (@combos); | ||
| 164 | } |