Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Understanding the Enigma machine from “The Imitation Game” with 30 Lines of Ruby (red-badger.com)
84 points by albertstill on Feb 26, 2015 | hide | past | favorite | 21 comments


The movie "Enigma" from 2001 is better.

Here's the NSA's official history of Enigma and the bombe: https://www.nsa.gov/about/_files/cryptologic_heritage/public...

They credit the Polish cryptanalysts who first cracked the Enigma and built the first bombe. Turing and Welchman at Bletchley Park came up with improvements which made it much more effective. Building the new machines was outsourced to the British Tabulating Machine Company.

Then the Germans switched from a three-rotor Enigma to a four-rotor, which the British machines could not solve and increased the effort required by a factor of 26. So the US Navy had National Cash Register build much faster machines. Hundreds of them.

Four wheels was about the limit for a portable rotor machine where the key presses advanced the wheels. The key pressure required for even a 3-wheel Enigma was very high. (I've typed on one, when one was at Stanford. The key travel is huge, almost an inch.) So that was as complex as the ciphers used by German mobile units (ships, trucks) ever got.

For fixed installations, there was the German Lorenz machine, which had 12 wheels. That required a completely different approach to crack. Bombes were a brute-force solution. The Colossus machine used electronics and a more statistical approach. But that's another story.


> They credit the Polish cryptanalysts who first cracked the Enigma and built the first bombe. Turing and Welchman at Bletchley Park came up with improvements which made it much more effective.

Not entirely. The Poles realised that the German keying procedure was flawed; they were doubly-encrypting the start of messages which made key recovery extremely easy.

They (the Germans,) stopped doing this relatively early on, so the key recovery devised by Turing (and then expanded on by the Americans) was a completely different attack. [1]

[1] http://www.cryptomuseum.com/crypto/bombe/


Also known as the enigma machine from that war! What a bizarre appropriation to make.

(I mean I get it, the idea is to link the title to the recent film and make the article somehow more current and clickable. Maybe it is a cultural thing but to me it sounds jarring and crass)


Actually, the article only talks about the Enigma machine featured in the movie, not all the different models (with different rotor counts, etc.) that the Germans used throughout the war. The title might sound linkbaity, but it's accurate.


Yes. It's a bit like noticing that this Turing chap bears a remarkable resemblance to Sherlock Holmes.


There's a minor problem with the implementation. The rotors and reflector always turned a letter into a different letter because of the wiring. It's possible for the shuffling of the alphabet to leave a letter in place.

This is a minor flaw for the explanation but the result was that a letter could never be enciphered to itself on Enigma. And this fact was exploited because it meant that crib positions could be eliminated.


That property is caused by the reflector, rotors themselves can shuffle randomly. The way the reflector is built in the implementation is from pairs of consecutive (unique) letters of the shuffled alphabet (getting a half of the reflector) and then merging the inversion.

IMHO it's correct.


Hmm. Rereading the code I believe you are correct. I'm not strong in Ruby and had misunderstood what this was doing:

    Reflector = Hash[*('A'..'Z').to_a.shuffle]
    Reflector.merge!(Reflector.invert)


> I recently watched the film “The Imitation Game” and it’s brilliant despite Keira Knightley’s failed attempt at a posh British accent.

Really?

Born: March 26, 1985 (age 29), Teddington, London, United Kingdom

Is the problem that her British accent wasn't posh enough?


If anything, the one issue I’d cite with the movie was that it didn’t depict Turing honestly in the slightest.


One Woman, 17 British Accents - Anglophenia Ep 5

https://www.youtube.com/watch?v=FyyT2jmVPAk


Correct.


Why would you think Joan Clarke was any posher than Keira Knightly? She was the daughter of a vicar...



Is that posher or just more old fashioned? Serious question.


Did something similar during a Software Craftsmanship conference at Bletchley Park a couple of years ago.

BP have a "paper" enigma machine that they use for school children and we pair programmed an implementation in a couple of hours. (no plug board IIRC)

Nice programming exercise and a great place to visit, although if you go don't forget to visit the computer museum (BP and the computer museum have fallen out so access to the museum is a bit more awkward than it used to be).


Did the British troops have an enigma machine during the war? If so couldn't they just intercept the arrangement of rotors each day and decode messages as they came in?


The problem is intercepting the codebook. If they had the machine and could somehow intercept the codes then yes.


According to Wikipedia [1], the codebooks were printed in red water-soluble ink. If a German ship wasn't already full of water by the time the Allies gained access to it, any German soldier could simply splash water (or blood) on the codebook, rendering it useless.

[1] https://en.wikipedia.org/wiki/Enigma_machine#Details


Y'know, I program in Ruby. I paid my dues in CoffeeScript. And I also studied computer science.

And sometimes Knuth-style pseudocode beats jerking off in Ruby. This is one of those times.


Hahahah




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: