From: Meino Christian Cramer Date: 2004-08-19T19:40:06+09:00 Subject: Re: POT (partly offtopic) Re: Q: Shifting a hash anf and array Hi Robert, Hi Rubyaner ! I think in a week or two we can publish "The Enigma Mails" as a paperback at Addison Wesley or so... ;) I will snip some parts to make it easier to...hrmmm...print for Addison's .. ;) > > Ok, this looks more like the Enigma++ or Enigma Pro or Enigma Deluxe > > ;) (by the way...currently it seems that there were 17 different > > models of the encyphering machine...) > > Why Enigma++? Personally I find it the easiest and most natural to create > those classes that abstract exactly the concepts of natural world that I > want to model. So having a Wheel class would be most natural for me, even > if it had only one or two methods. Sorry, Robert! This was only a nonsense joke. The only thing I wanted to say was, that your non-rotating-but-offsetting-algorithm will improve (therefore a kind of "incrementation of the Enigma code") the code of the Enigma. Nothing more... > > > Unfortunately there is not only the one offset needed for rotating > > the wheels -- there is also one offset to be take into account for > > the outer ring or "tyre" of the wheels which was used to adjust the > > alphabet against the internal wireing and an additional offset for > > the start positions of the wheels. > > Well, you can calculate all offsets in one step. Yes, yes, yes....:O) I finally figured it out by myself. It is far less complicate as I first thought. I am using offsets everywhere, where I have to rotate something. > > I think I will do first "rotate" the "wheels" into the initial > > position takeing the outer ring position and the start position of the > > wheels into account. This only happens once. > > > > And then I will "robertize" the "wheels" according to your code > > above, Robert. Since this happens for each letter to encypher it is > > far more important for this part of the code to be fast. And the > > code does not get too complex because of haveing three different > > offsets to be calculated each time. > > Sounds quite reasonable. In that case maybe I'd change the encode method to > accept one offset argument (or a list of offsets) so you only have to do the > modulo once, i.e., you can sum them all up and then do "% @code.size". > Example with arbitrary many offsets: > > def encode(char, *offsets) > @code[(normalize(char) + offsets.inject(@offset){|sum,x| sum + x} ) % > @code.size] > end Oh...I have to do a deeper look into my Enigma code...I think the code isn't rubyish enough... > > > What do you think, Robert? > > > > Thank you very much for all the help I got from this list. > > You're welcome! :O) > > Hopefully I will find the rest of informations I need about the > > technical specs of the different Enigmas. > > Good luck! Some links that Google revealed: > > http://www.mirrors.wiretapped.net/security/cryptography/algorithms/enigma/ > http://www.javaworld.com/jw-08-1998/jw-08-indepth-p3.html > > http://mathforum.org/library/topics/cryptography/ > > > Kind regards > > robert That's really funny, Robert! What do you think, how often I hammered keywords like "Enigma", "cryptography and such into Google the last days...but I never stumbled across these... Thanks a lot...I will start a surf party in a few minutes ;) > > Something like: > > > > objarray=Array.new > > objarray[idx]=Rotor.new(config) > > encchar=objarray[idx].method(param) > > ? > > > > How do I have to tell this to Ruby ? > > Like this? My fault...I thought "It couldn't be THAT easy...but it works right the way as I mailed it... This is the inversion of "Principle of last surprise" Ruby uses: One is surprised, that it is SUCH easy. ;) > encoded = objarray.inject(letter) {|param, rotor| rotor.encode param} Looks very rubyish! Nice code. I will try to use it...there one additional parameter I need to take care of here. Each wheel has a notch (or two), which will turn "the next" wheel one step at a certain point of rotation -- like the carry bit used in assembly programming. I implemented this notch as a object variable, which is set true at the position in question and has to be evaluated by the "next wheel"-object. Like this? @carry=true # cause the first rotor is always rotated by one step if # a new letter arrives to be encoded encoded = objarray.inject(letter) {|param, rotor| rotor.rotate(@carry) @carry=rotor.carry rotor.encode param } > I assume, all Rotors are connected, i.e. the first one receives the original > char and the second the result of the first and so forth. Otherwise you can > use #each or #map. Yes, totally right...only a little more complex. Like this: keyboard-->letter letter-->Steckerboard--->letter (simply letter mapping char->another ch ar) letter-->ETW--->letter (ETW="Eintrittswalze", Entry wheel) letter-->Wheel1--->letter letter-->Wheel2--->letter letter-->Wheel3--->letter letter-->UKW--->letter (UKW="Umkehrwalze", Rfelector) letter-->Wheel3--->letter letter-->Wheel2--->letter letter-->Wheel1--->letter letter-->ETW--->letter (ETW="Eintrittswalze", Entry wheel) letter-->Steckerboard--->letter (simply letter mapping char->another ch ar) lamp signaling the encyphered letter The pressing of one key of the keyboard rotates the first wheel (and the following when the notch appears) and depressing encodes the letter. Only the wheels were rotating, the "Steckers" of the "Steckerbaord" (I like this word mixed from German and English. "steckered" is even funnier!) (plugs in a plugboard) could be changed (only 13 connection are allowed, mapping one letter to _another_ letter), the UKWs and ETWs of some models could be changed against others but as far as I know at this point they do not rotate. Another nice site I found about all this is here: http://www.xat.nl/enigma-e/ > > Regards > > robert > > Thanks!.alot(Robert) Ruby.use! Meino