The British politician Lord Hailsham once said, "The one case in which I think I can see the finger of God in history, is Churchill's arrival at the premiership at that precise moment in 1940." Another candidate for the intervention of the Almighty in the Second World War might be the Allied cracking of the German codes encrypted by the Enigma machine, which produced a huge stream of top-secret information known by its British special security classification, Ultra. This allowed the Allies for much of the war to read many of the highest-classified communications that were sent and received by Hitler's HQ, the Army High Command, the individual commanders of the Wehrmacht, Luftwaffe and German Navy, the Nazi Intelligence Services, SS and even the Reichsbahn (railways), amounting to several million items of raw, invaluable information.

From the correspondence of Adolf Hitler himself right down to that of the harbor master of Olbia in Sardinia, messages were decoded by the Allies. As the military historian Professor Sir Michael Howard has put it, it made the Second World War "like playing poker with marked cards". The vital importance of the Enigma decrypts can be gaged from the jokey acronym given to it by the Americans, 'BBR', which stood for 'Burn Before Reading'.

Although the Enigma decrypts were invaluable for reading the minds of the Axis commanders during the battles in North Africa in 1942-43, the Italian campaign of 1943-44, and before and during the Operation Overlord invasion of Normandy in 1944, they really came into their own during the Battle of the Atlantic, where they could be used to pinpoint where German U-boat 'wolfpacks' were about to congregate prior to attacking the Allied convoys bringing vital food and weapons supplies to Britain. Churchill described the Battle of the Atlantic as "hard, widespread and bitter, a war of groping and drowning, a war of ambuscade and stratagem, a war of science and seamanship". That it was won by the end of August 1943 was largely down to the breaking of the Enigma codes.

The design of the Germans' Enigma machine – one of which is offered by Bonhams in November – was first patented by a Dutchman, H.A. Koch, in 1919. By 1929 it had been bought by the German Army and Navy (which used different versions). Looking like a normal typewriter but with three, four or five 26-spoke rotor wheels attached, as well as lights and plugs resembling a telephonist's board, it could transform a typed message into a code so complicated that the Germans assumed it could never be broken. It could compute an incredible 1,252,962,387,456 combinations.

Although Polish Intelligence worked out the key to cracking Enigma, changes in the machines instituted by the Germans in January 1939 – particularly doubling the number of plugboard sockets – plunged the Allies into the dark. After the fall of Poland and France, the operation to break back into Ultra was concentrated at the British Code and Cipher School at Bletchley Park in Buckinghamshire. Churchill called the 10,000 code-breakers there "the geese who laid the golden eggs" who "never cackled". They were also almost all amateurs, recruited from civilian life, although their contribution was far to outweigh that of the career Intelligence officers of the day.

At its fastest, Wehrmacht and Luftwaffe signals were decoded in the makeshift huts at Bletchley between three and six hours after they were sent, and naval signals during the Battle of the Atlantic could be read as swiftly as an hour after transmission. It was not until April 1941 that the German naval codes were broken. Although German Intelligence set up regular investigations into the security of Enigma – and the commander of the U-boat section of the German Navy, Karl Dönitz, had himself questioned whether it could have been broken – they only refined the existing machine settings, and, luckily for the Allies, never instituted a brand-new communications system.

Although the Allies could not be seen to rely on it too much, for fear the Germans would realize it had been compromised, information gleaned from Ultra was used to great advantage in many key moments of the war: the battle off Cape Matapan; the sinkings of the Bismarck and Scharnhorst; discovering Rommel's weaknesses and shortages prior to the Battle of El Alamein; Montgomery's advance into Tunisia in March 1943; the planning for the invasions of Sicily and southern France; finding the whereabouts of German divisions before D-Day, and many more. (The day before Cape Matapan, Admiral Sir Andrew Cunningham strode ashore at Alexandria carrying his golf clubs, so as to lull the suspicions of spies there. The next day, 28 March 1941, he sunk three Italian destroyers and two cruisers whose whereabouts and intentions he knew from the Ultra decrypts he received.)

Yet it was undoubtedly in the Battle of the Atlantic that Ultra was put to greatest use. Britain had to import two-thirds of all the food she consumed during the war; if she lost the Battle of the Atlantic she would have starved, or surrendered. "The U-boat attack was our worst evil," Churchill recalled. "It would have been wise for the Germans to stake all on it." It certainly saw some nerve-wracking moments: in March 1941 alone, U-boats sank 41 ships. Yet due to Enigma having been cracked at Bletchley in May, between July and December 1941 Allied convoys were re-routed so expertly that not one was intercepted in the North Atlantic. Although that does not mean there were not losses – over 720,000 tons were sunk in that period – experts calculate that over 1.6m tons were saved.

The worst moment for the Allies in the Battle of the Atlantic came on 1 February 1942, when the Germans suddenly introduced a fourth rotor wheel to the Enigma machines used by U-boats in the Atlantic, hugely increasing the difficulty of cracking Enigma-encrypted texts. Every effort was made to crack the new code, dubbed 'Shark', yet for over 10 months Bletchley was thrust into the dark, its computers producing only gobbledegook. Unable to be re-routed away from peril, convoy sinkings increased disastrously.

Yet at 10pm on Friday 30 October 1942, U-559 was forced to the surface after no fewer than 288 depth charges were dropped on it by four British destroyers in the eastern Mediterranean. Its captain opened its stopcocks to scuttle the vessel, but Lieutenant Francis Fasson, Able Seaman Colin Grazier and a 16-year-old NAAFI assistant Tommy Brown (who had lied about his age to join the Navy) from HMS Petard stripped off and swam over to the sinking submarine. Making their way to the captain's cabin, they used a machine gun to break into a locked cabinet and retrieve codebooks and documents. After Brown made three journeys to deliver these to another party from the destroyer, the U-boat sank, drowning Fasson and Grazier, who were trapped on board. Although their gallantry was up to the standard required for the Victoria Cross, since it was not technically 'in the face of the enemy', as the criteria stipulates, they were awarded the George Cross posthumously, and Brown received the George Medal.

No decorations were more deserved: once Bletchley received the documents (on 24 November), they were found to include the all-important indicator list, code and weather tables, and allowed the code-breakers to break into 'Shark' on 13 December. The tide of battle had turned, and this time for good. (Yet that did not prevent Tommy Brown GM from being discharged from the Navy, for volunteering while underage.)

If at any stage the Germans had recognized the truth about what had happened, it could have proved disastrous for the Allies – lengthening the war for what military historians estimate might have been for another two years – but the cracking of Enigma turned out to be the best-kept secret of the 20th century, and hundreds of thousands – perhaps millions – of lives were saved as a result.

The award-winning historian and biographer Andrew Roberts's most
recent book is The Storm of War (Penguin).

Related auctions