Created
November 8, 2013 15:11
-
-
Save rd13/7372358 to your computer and use it in GitHub Desktop.
Enigma Perl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
$|=1; #disable output buffering, this is necessary for proper output through pipe | |
my @rotors = ('EKMFLGDQVZNTOWYHXUSPAIBRCJ','AJDKSIRUXBLHWTMCQGZNPYFVOE','BDFHJLCPRTXVZNYEIWGAKMUSQO'); | |
my $reflector = "YRUHQSLDPXNGOKMIEBFZCWVJAT"; | |
my $key = "ABC"; | |
sub li { | |
my($l) = @_; | |
return index("ABCDEFGHIJKLMNOPQRSTUVWXYZ", $l); | |
} | |
sub il { | |
my($i) = @_; | |
return (A..Z)[$i]; | |
} | |
sub ecrypt { | |
my($ct) = @_; | |
$L = li(substr($key, 0, 1)); | |
$M = li(substr($key, 1, 1)); | |
$R = li(substr($key, 2, 1)); | |
$output = ''; | |
while ($ct =~ /(.)/g) { | |
$ct_letter = li($1); | |
$R = ($R + 1) % 26; | |
$a = substr(@rotors[2], ($R + $ct_letter) % 26, 1); | |
$b = substr(@rotors[1], ($M + li($a) - $R) % 26, 1); | |
$c = substr(@rotors[0], ($L + li($b) - $M) % 26, 1); | |
$ref = substr($reflector, ((li($c) - $L) % 26), 1); | |
$d = (index(@rotors[0], il((li($ref) + $L) % 26)) - $L) % 26; | |
$e = (index(@rotors[1], il(($d + $M) % 26)) - $M) % 26; | |
$f = il((index(@rotors[2], il(($e + $R) % 26)) - $R) % 26); | |
$output .= $f; | |
} | |
return $output; | |
} | |
my $starttime=time; | |
my $i=(undef); | |
# while($i++ <= 10000){ | |
ecrypt('PZUFWDSASJGQGNRMAEODZJXQQKHSYGVUSGSU'); | |
# } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment