<?xml version="1.0" encoding="windows-1252"?>
<node id="124834" title="Enigma machine." created="2001-11-12 13:46:15" updated="2005-07-27 15:40:24">
<type id="1042">
CUFP</type>
<author id="7824">
frankus</author>
<data>
<field name="doctext">
&lt;p&gt;I was looking for a reasonably good method of encrypting stuff that 
was okay to use on an ipaq,
&lt;br&gt; RSA seems a bit too heavy, and the RSA dolphin
requires stuff that hasn't yet been ported...
&lt;br&gt; I toyed with using Solitaire (p480 of Cryptonomicon)
&lt;br&gt; I've got some C code for GOST, but I've not tried it,
instead for a laugh:
&lt;p&gt;
It's an enigma machine, that'll take a varying number of rotors. I've tested it reasonably thouroughly, please 
report any errors ;), as it was only tested on 5.6.1. 
&lt;p&gt;&lt;b&gt;Notes&lt;/b&gt;&lt;br&gt;
The enigma machine here, can 'emulate' most types of enigma, 
from the pre-war (3 rotor w/o plug-board (AKA Steckerboard)) to the
4 Rotor, plug-boarded enigma used on submarines).&lt;p&gt;
&lt;ul&gt;&lt;li&gt;rotorN represents a disc with a jumbled alphabet corresponding to an ordered alphabet&lt;/li&gt;
&lt;li&gt;reflect represents a partial rotor where letters correspond to other letters and the other letter back (i.e) A-&gt;M, M-&gt;A.&lt;/LI&gt;
&lt;li&gt;Order is the order of occurence of the rotors, since they're interchangeable, except the reflector&lt;/li&gt;
&lt;li&gt;position is the starting letter for each rotor.&lt;/li&gt;
&lt;li&gt;Plug is the plug board where letters are mapped to other letters, as with the reflector&lt;/li&gt;
&lt;/ul&gt; 
&lt;p&gt;Using a programming language it seemed redundant to retain ring-settings.
&lt;code&gt;#!/usr/bin/perl
use strict;

sub r{
        my($r,$p,$c,$n)=@_;
        $r=~s/([a-z])/$n=uc($1),uc($1)/eg &amp;&amp; $r=~s/(.*)($p)(.*)/$2$3$1/;
        sub{
                if($_[0]=~/^-(.)$/){$p=('A'..'Z')[index($r,$1)]}        
                else{$r=~s/(.{$_[1]})(.*)/$2$1/ if $#_&gt;0;$p=substr($r,ord($_[0])-65,1)}
                ($p,eval $c)
        }                   
}

my $d=0;
%_=map{chomp;(split/:/,$_,2)}&lt;DATA&gt;; # These four lines read the setting data ;)
$_{$_}=[split/ /,$_{$_}] for qw(position plug order);
$_{'plug'}={map{@_=(split//);@_[0,1,1,0]}@{$_{'plug'}} };
$_{'switch'}=['$n eq $p','$d=($n eq $p),$d','$d?!($d=0):0','0'];

@_=map{
	&amp;r($_{$_{'order'}-&gt;[$_]},
	   $_{'position'}-&gt;[$_],
	   $_{'switch'}-&gt;[$_])
	}(0..$#{$_{'order'}});

s/([a-z])/$_=uc($1);                             # Enigma takes only alpha chars.
my($a,$i)=(1);$_=$_{'plug'}-&gt;{$_}|| $_;          # go through stecker plugs.     
for $i(0..$#_){($_,$a)=&amp;{$_[$i]}($_,$a)}         # go through rotors.
$_=substr($_{'reflect'},ord($_)-65,1);           # go through reflector plate.
for($i=$#_;$i&gt;=0;){($_,$a)=&amp;{$_[$i--]}("-$_")}   # go back through rotors     
$_=$_{'plug'}-&gt;{$_}|| $_/eigx &amp;&amp; print while(&lt;&gt;) # undergo steckering.   

__END__                                                               
rotor1:EKMFLGDqVZXNTOWYHUSPAIBRCJ
rotor3:AJDKSIRUXBLHWTMCQGZNPYFVOe
rotor2:BDFHJLCPRTXvZNYEIWGAKMUSQO
rotor4:ESOVPZjAYQUIRHXlNFTGKDCMWB
reflect:YRUHQSLDPXNGOKMIEBFZCWVJAT
order:rotor1 rotor3 rotor2
position:D M Z A                  
plug: AT MX CD GR
&lt;/code&gt;
&lt;p&gt;&lt;p&gt;&lt;b&gt;Update:&lt;/b&gt; Bwahahaha.. I was going to write GOST in Perl and someones already done it, &lt;a href="http://vipul.net/gost/software/gostcrypt"&gt;look...&lt;/a&gt; :(
&lt;p&gt;&lt;b&gt;Update:&lt;/b&gt; You know how it is when you keep whittling away at something : [126571|It's a Shark]
&lt;p&gt;

&lt;p&gt;--&lt;br&gt;&lt;/br&gt;
&lt;i&gt;Brother &lt;A HREF="/index.pl?node=Frankus&amp;lastnode_id=1072"&gt;Frankus&lt;/A&gt;. (TJRX OSDKVMG YPBO PKGHZP)&lt;/i&gt;
&lt;p align="center"&gt;&amp;#164;&lt;/p&gt;</field>
</data>
</node>
