(Inside Science) -- Everyone knew it was coming. The day before it came they even knew when. What the U.S. couldn't figure out was where.

No one expected an attack on the U.S. naval base at Pearl Harbor. Even after the U.S. decrypted the Japanese diplomatic cipher, the Japanese still managed to nearly destroy America’s Pacific fleet and guarantee the U.S.’s entry into World War II.

The story of the U.S. code breakers at the dawn of America's involvement in WWII is one of brilliant technology, ingenuity and intuition hampered by incompetence, miscommunication, and lethal assumptions.

Ever since that day, Dec. 7 1941, which President Franklin Roosevelt described as “a date which will live in infamy,” conspiracy theorists have been busy, mostly blaming Roosevelt and the military for either not paying attention to intelligence that would have predicted the attack, or knowing the attack was coming but choosing for political purposes to ignore it. Neither is true.

Robert Hanyok, retired historian at the National Security Agency, said that even in hindsight, there was no way the code breakers could have predicted an attack on Hawaii from what they uncovered.

The British were luckier in breaking the German cipher. The Germans used a cipher machine called Enigma to send secret messages, but the cipher for Enigma had been broken by three Polish mathematicians in the early 1930s. The Poles gave a replica of Enigma to the British, who reproduced them and used them throughout the war. Even then, the British, led by the brilliant mathematician Alan Turing, had to invent a single-purpose computer to decode the messages. Eventually, the British knew which cities would be bombed before the raids and where the U-boats were.

Breaking the Japanese cipher was a splendid example of American ingenuity even if it resulted in failure, according to journalist Steve Twomey in his new book Countdown to Pearl Harbor, the Twelve Days to the Attack. American cipher breakers did it the hard way.

Unlike the British, American code breakers had no model of the Japanese cipher machine to work with. Instead, they had to recreate one by intuition.

“They did not use a stolen Japanese machine,” said Twomey. “They did not have photographs of one. They did not have blueprints of one. They simply imagined how it must work and they turned out to be right.” Essentially, they tried to imagine what an encryption machine would look like if Japanese engineers built one and they intuited correctly.

Their machine was called Purple and the decrypted messages were called Magic. Twomey wrote that the offices of only ten people knew about Purple and could read Magic, and one was President Roosevelt's. It was a more closely guarded secret than the atomic bomb. Those ten offices did not include Congress, diplomats, or the military command in Hawaii.

Both Purple and Enigma were cipher machines, said Hanyok. A cipher is not the same as a code. And breaking code or a cipher does not mean you can read what it says, only that you know how the code was constructed. The Americans broke the Japanese cipher in early 1939 but did not get translations until the fall of 1941 Hanyok said.

In a simple code, one letter, a group of letters, numbers or other symbols are substituted for something else. For instance, the name of a ship, say the Lexington, could appear in a code as zkeigfer every time. The machine at the other end would know what zkeifger means.

“That is the main vulnerability,” Hanyok said. It is possible to see patterns. Analyzing the patterns can reveal clues about the content of the message, and eventually, the meaning.

Cipher, on the other hand, doesn’t make consistent substitutions. It transposes letters or numbers according to a key and the key is changed regularly. Lexington may be zkeigfer the first time it is used in a message but might not the second time and certainly wouldn’t be after the key is changed. Both sending and receiving machines would be working off the same key.

The Japanese changed the diplomatic keys daily. The Americans even guessed what keys the Japanese would use, based on knowledge of past Japanese methods and encryption tendencies, Twomey wrote.

The fatal disadvantage, Twomey wrote, was that the U.S. could read only some of the diplomatic ciphers, and none of the military communications. The Japanese military had their own encryption system, which was not broken until well into the war.

Americans reading the decrypted messages assumed they were getting a fuller picture of Japanese intentions than they in fact were, he wrote.

The team intercepting the messages was on Bainbridge Island, Washington State, which relayed them in their original form -- sometimes by Western Union -- to Washington, D.C. for decrypting. Bainbridge couldn’t read the messages but could read what we now call the metadata -- who was signaling whom, when, how often, and the identity of all the stations.

Navy intelligence knew where much of the Japanese fleet was on Dec. 6, but some of the heavy carriers had dropped off the map weeks earlier and were presumed to be safely in port in Japan.

While negotiations to end the tensions between the two countries continued fruitlessly in Washington, on Nov. 26, a large task-force pulled anchor from a naval base in northernmost Japan: two battleships, three cruisers, nine destroyers, three submarines, seven tankers and six aircraft carriers carrying 353 planes, including bombers, torpedo planes, and fighters to protect the squadrons.

Twomey wrote they sailed across the north Pacific in total radio silence. Purple heard nothing.

On Nov. 27, the War Department had sent a message to Pacific commands that began: “This dispatch is to be considered a war warning.” Washington then assumed the proper precautions would be taken across the Pacific. They were not, Twomey wrote. No one asked if the commanders complied.

No one ordered air patrols of the sea surrounding Hawaii, even to the North where an attack was most likely. No one ordered the American fleet out of the trap that was Pearl Harbor except for some American carriers that had sailed west earlier. They were easy targets for the Japanese planes.

In Washington on Dec. 6, Purple reported messages that Tokyo ordered the embassy in Washington and the consulate in Honolulu to get ready to burn documents and destroy one of its two code machines and then, finally, the second one, a sure sign war was about to begin. In Bainbridge it was noted that the radio call signs, usually changed once a month, were suddenly altered out of sequence. The amount of radio traffic exploded. Clearly something was about to happen, but where?

All the betting was on the southwestern Pacific, Twomey wrote, with the Philippines (an American colony), Singapore (British), Indonesia (Dutch), Indochina (French) being the most likely targets. No one thought of Hawaii because, they believed, the Japanese were not capable of such a feat, lacking the audacity, the technology, Twomey wrote. Also, no one thought they could keep that big a secret.

Even when the huge squadrons appeared on army radar on Oahu, the operators were told by their officers “don’t worry about it,” saying they were probably American bombers being ferried to Hawaii. The operators were told to go home.

Two thousand, four hundred and two Americans died in the attack. The battleship USSArizona remains at the bottom of Pearl Harbor with the bodies of many of the 1,177 sailors and marines still entrapped.