Donald Nichols, an Air Force spy in Korea at the dawn of the Cold War, was the best and worst kind of American warrior. Ferocious, creative and unbreakable, he won more than 20 medals for valor during the Korean War and vacuumed up battlefield secrets that saved countless American lives.

Yet Nichols was callow, untrained and very young—just 23—when he arrived on the Korean Peninsula. He was a seventh-grade dropout. He was virtually unsupervised. He would soon veer out of control, losing touch with morality, with legality, even with sanity—if Air Force psychiatrists are to be believed. He attended mass killings and trained South Korean police who specialized in torture. He was photographed standing beside a severed human head.

Nichols became America’s Kurtz, an uncontrolled commander in a faraway shadowland. The Air Force gave him an astonishingly long leash: his own secret base, his own secret army of spies, and a self-proclaimed “legal license to murder.” His commanding general described him as a “one man war.”

Suddenly and brutally, the Air Force turned against Nichols in 1957. He was spirited out of Korea and subjected to months of electroshock at Eglin Air Force Base Hospital in Florida. He told relatives the U.S. government was trying to destroy his memory.

Seven years before he was ignominiously removed from command, Nichols played a starring—albeit secret—role in the decisive early weeks of the Korean War. His code-breakers helped the Americans halt and turn back a North Korean invasion that stunned the world.

By early August 1950, North Korea had taken nearly 90 percent of South Korea’s territory. Bedraggled American and South Korean forces had fled to the southern end of the peninsula, where they clung to a patch of land that was barely fifty miles wide and a hundred miles long. The port of Pusan, the sole major city not yet controlled by North Korean leader Kim Il Sung, was at the southern end of this toehold, which became known as the Pusan Perimeter. It was there that Lieutenant General Walton “Johnnie” Walker, commander of the Eighth Army and the senior American commander in Korea, announced that U.S. forces would stand or die.

Walker was a short, pudgy, plainspoken Texan whom the press often likened to a bulldog. During World War II, while leading an armored corps across France and Germany, he earned a hard-charging reputation not unlike that of his famous mentor, General George S. Patton. But by the summer of 1950, Walker was sixty. Some generals in Tokyo and Washington believed he had lost a step. General Douglas MacArthur, the supreme commander of American forces in the Pacific, had flown into the Pusan Perimeter on July 27 and told Walker that withdrawal was no longer an acceptable strategy. The upshot of that visit was the most eloquent statement ever attributed to Johnnie Walker:

“There will be no more retreating. There is no line behind us to which we can retreat. Every unit must counterattack to keep the enemy off balance. There will be no Dunkirk, there will be no Bataan, a retreat to Pusan would be one of the greatest butcheries in history. We must fight until the end. Capture by these people is worse than death itself. We will fight as a team. If some of us must die, we will die fighting together.”

Walker’s rhetorical flourishes played well in newspapers back in the States. But many GIs inside the Pusan Perimeter heard it as an absurdity—a Kafkaesque command to “stay and die where you are.” Under Walker’s leadership, the soldiers had staggered south in July, absorbing one humiliating defeat after another. Thousands of them were sick with dysentery and respiratory infections. Bad food, bad water, and lack of sleep had caused battle fatigue that left many unable to control their fear and unwilling to fight. They did not understand why they were in Korea. They certainly did not see why they should be willing to die, as Walker said they must, to defend the little bit of it that was left. In This Kind of War, historian T. R. Fehrenbach described Walker’s infantrymen as “exhausted, dispirited, and bitter.”

Air Force Chief Warrant Officer Donald Nichols was there, too, having fled south from several South Korean cities that had been overrun by the tanks and infantry of the North Korean army. He and his aide-de-camp, Sergeant Serbando J. Torres, set up an intelligence office in a two-story house in a residential neighborhood of Taegu, a refugee-swollen town of four hundred thousand people located roughly in the center of the Pusan Perimeter. Because roads and rail lines came together in Taegu, it had been chosen as the temporary seat of the South Korean government and as Korean headquarters for the Eighth Army and the Fifth Air Force.

There were frequent outbreaks of fighting to the north and west of the city. Nearly every night, Nichols and Torres heard the thud and whistle of artillery shells and saw flashes from explosions. Like Walker’s infantrymen, Nichols had been forced to retreat. Yet he was anything but exhausted, dispirited, or bitter. His war had been exhilarating, winning him praise, medals, and promotion. Now, in Taegu, he had a new trick up his sleeve: a team of code breakers who would cement his status as an invaluable spymaster.

Among the hundreds of North Korean defectors Nichols had interrogated before the outbreak of war, there had been one who fled the North with stolen codebooks from the Korean People’s Army. His name was Cho Yong Il, and Nichols’s discovery of him would prove to be a key to halting the North Korean offensive. Cho had been a private in the Japanese army and a merchant seaman with the Soviet navy. Until he defected from North Korea in 1949, he was a radio operator and cryptographer for Kim Il Sung.

Before the war, when Nichols first learned of Cho’s expertise as a cryptographer, he arranged for Cho to meet his friend Kim Chung Yul, chief of staff of the South Korean Air Force, which Nichols had recently helped create and over which he exercised considerable influence. “Nick was always thinking ahead,” Torres said. “He told the South Koreans to hang on to this guy. He also told Cho to train a team of cryptographers.”

By anticipating a need for code breakers in Korea, Nichols was light years ahead of the rest of the American intelligence community. The cryptographic resources of the United States—such as they were—had ignored North Korea. The secretary of defense, Louis Johnson, issued a top-secret order in 1949 that created the Armed Forces Security Agency (AFSA), which would evolve into the National Security Agency, today’s global data-vacuuming leviathan. But when North Korea invaded in 1950, the agency’s code-breaking capacity was hobbled by budget cuts and warped by a near-exclusive focus on the Soviet Union. The agency had one self-taught Korean linguist, no Korean typewriters, and no Korean dictionaries. Under Johnson’s order, each branch of the military was supposed to create a unit for “tactical” code breaking in the field. But these, too, were poorly funded, understaffed, and had no expertise in the Korean language. They also feuded endlessly with one another. When it came to cryptography and Korea, the Americans were helpless.

As instructed by Nichols, the South Korean Air Force commissioned Cho Yong Il as a second lieutenant and put him in command of a radio intercept station on the outskirts of Seoul. His unit started work on June 1, 1950, which gave them three weeks on the job before North Korea invaded the South and seized the capital.

Though Cho had to abandon much of his equipment, he made it safely to Taegu in early July, where he and his men set up five radio positions and began monitoring them with three receivers. His unit could intercept and understand North Korean army radio messages because the North, in what would prove to be a disastrous blunder, had not bothered to change its rudimentary radio codes. Cho’s stolen codebooks allowed his team of cryptographers to decipher everything they could hear.

By this time, Nichols had set up his shop in Taegu, where he made himself the chief provider of targets for the Fifth Air Force, the Japan-based command responsible for American air power in the Pacific. He was delighted to learn that Cho was decrypting North Korean radio codes—so delighted that he commandeered the lieutenant and his entire code-break team. “I was ordered by Mr. Nichols to move our equipment and members to his house,” Cho said. “This I could not refuse.”

Cho and his code breakers moved into a high-walled residence directly across the street from the building where Nichols lived and worked with Torres. Beginning on July 17, 1950, more than two thousand intercepted and decoded messages were hand carried to Nichols’s house, where interpreters translated them into English. They arrived at all hours of the day and night. Whatever the time, Nichols had given standing orders for either Torres or Sergeant Russ Bauer to type them up immediately and jump in a jeep to deliver the intel.

“It was top priority and I delivered the messages to General Walker’s Eighth Army and to General [Earle E.] Partridge’s Fifth Air Force,” Torres said, referring to Army and Air Force headquarters in Taegu. “We had the feeling we were doing really important work. Of course, we were saving our butts, too. Those North Koreans were right outside our back door.”

These intercepted radio messages allowed the Air Force to find and pound North Korean tanks, supply lines, artillery installations, and troop concentrations. All this greatly impressed General Partridge, who began to refer to Nichols as a “genius” who “didn’t know what it meant to be scared of anything.”

Having tapped a vein of intelligence that pleased the two most powerful American generals in Korea, Nichols pushed aggressively for more code breakers, more linguists, and more radios. He persuaded the South Korean navy to move sixteen of its cryptologists into the building across the street from his quarters, where they worked at the same table with Cho’s men. Nichols also joined forces with South Korean code breakers working with Walker’s Eighth Army.

After building his code-break fiefdom, Nichols ferociously guarded it. He did not want other American spy agencies horning in and stealing his glory. The first outfit to try was the Air Force Security Service, the cryptology arm of the Air Force that was separate from and, in theory, superior to Nichols’s seat-of-the-pants code-break team. In late July, it sent First Lieutenant Edward Murray from Japan to Taegu to find local linguists and set up a radio intercept unit in a mission code-named “Project Willy.” Murray arrived in a transport aircraft and brought along a trailer full of new radio equipment.

Nichols refused to cooperate with Murray in any way, declining to lend him a single linguist. As a result, Murray “didn’t have the Korean personnel to get any results,” said Lieutenant Colonel O’Wighton D. Simpson. “It became obvious that Mr. Nichols, who was an independent operator, I might add, was the best source.”

Two weeks after Murray arrived in Korea, Partridge ordered him back to Japan. As for Murray’s equipment—Nichols kept it all.

“The breathless nature of Nichols’s coup left [Murray] spinning,” said a top secret history published in 1995 by the National Security Agency. “Murray’s mission became entangled in one of the most bizarre incidents in the history of American cryptology.” Back in Japan, Murray complained bitterly about Nichols and “a severe jurisdictional battle ensued, encompassing command organizations in the United States, Japan, and Korea.”

Murray won—or so he thought. He returned to Taegu on August 12 armed with “a letter of authority” from General Charles Y. Banfill, deputy for intelligence with the Far East Air Forces. “Lt. Murray is proceeding to Korea to assume overall direction of Project Willy,” said a message that summarized his new and expanded authority. “[Nichols] is being relieved of this function with all equipment, personnel, and documents to become Murray’s responsibility and he will direct all activity.”

Nichols, on his own authority, decided that the orders were insufficiently explicit. He was “still unwilling to relinquish control,” according to the NSA history. Five days later, the Fifth Air Force again ordered Murray out of Korea.

This time, though, Nichols threw Murray a bone, allowing him to depart with North Korean codebooks and technical information, which he used in Japan to set up a code-break team. Two years after Murray’s double dose of humiliation, the Air Force Security Service commissioned a study to find out what had happened and why. It concluded that the early code-breaking history of Korea was a “struggle for empire” that Nichols won; he made himself a “king” and sold his fellow Americans “right down the river.”

Nichols picked the perfect moment to become the king of code breaking. The Pusan Perimeter, more than any other area the Americans had tried to defend in Korea, was defensible. It had natural barriers against invaders: rugged mountains to the north; a major river, the Naktong, to the west; and everywhere else, the sea, which was controlled by the U.S. Navy.

West Point–trained infantry and armor commanders knew how to fight in such a place, with clear battle lines, allies at their flanks, and reserves in the rear. They also had a major logistical edge. Inside the perimeter there was a modern and relatively well-maintained network of roads and railways, a legacy of Japanese colonial rule that meant they could quickly move troops, ammunition, and other supplies to where they were needed most. As important, the American war machine was finally beginning to hum. In the late summer and fall of 1950, more well-trained soldiers and marines, tanks, and artillery arrived at Pusan with each passing week. As South Korea’s only deep-water port, Pusan could accommodate thirty oceangoing ships at a time.

There was even a psychological advantage to being trapped: it concentrated the mind. “Bug out fever,” the fear-driven panic that had taken hold in the first weeks of the war, began to subside. For an infantryman trapped on the Pusan Perimeter, the only real choice was to dig in and prepare to fight or die.

As the Americans resupplied, the Korean People’s Army struggled to keep its supply lines from collapsing. American control of the air and sea all but stopped the North’s efforts to move large shipments of food, fuel, and ammunition. Mechanical and maintenance problems, along with napalm air strikes made more deadly by the intelligence discoveries of Nichols and his team, had slashed the number of battle-ready T-34 tanks from 150 to about 40. Lacking doctors, drugs, and medical facilities, injured North Korean soldiers began to die of treatable wounds.

Although combatants on both sides were unaware of it, a fundamental shift had taken place. The number of troops on the ground had swung to the advantage of the Americans and the South Koreans. By August, North Korea’s 70,000 troops faced a combined American and South Korean force of 92,000 soldiers and marines. The imbalance would soon get much worse for North Korea. MacArthur was getting ready for a massive counterattack. At Inchon, about thirty miles west of Seoul, he was pre- paring an amphibious landing that on September 15 would insert 70,000 American troops into the middle of the peninsula, where they could cut supply lines to North Korean forces besieging the Pusan Perimeter.

General Walker knew he did not have to hold the line forever—just long enough. When the North Koreans tried to punch a hole in the Pusan Perimeter, Walker had to be ready to fill the hole and punch back. As he admitted in early August, if the North were to focus all its forces, create a single gaping hole in the line, and then drive “straight and hard for Pusan,” he could not stop them.

No such punch ever came.

North Korea squandered its greatest advantage. It ignored a fundamental battlefield tactic of concentrating power. The failure was typical of Kim Il Sung, the North Korean leader. He was a sloppy war planner and an incompetent battlefield tactician. His only real experience as a commander had been leading a few hundred guerrillas—armed with knives and rifles—in hit-and-run strikes against Japanese police in the 1930s. He had bragged to Stalin that the war in South Korea would be won in three days, but he did not know how to sustain an armored invasion force for a longer fight and his sub- ordinates were afraid to question his judgment. In his hurry to invade South Korea by the end of June, Kim declined to wait for eighty-nine additional T-34s that Stalin would send him in July and August. They would have doubled the number of tanks in his initial invasion, allowed Kim to attack with three armored divisions instead of just one, and increased his chances of a crushing victory in the Pusan Perimeter. His generals, as they faced the perimeter, chose to divide their ten armored divisions and strike from four locations. It diluted their strength, giving Walker a fighting chance.

Moving his men on trains, borrowing men from one regiment and lending them to another, and hustling newly arrived weapons and fresh troops to where they were most needed, Walker managed—just barely— to beat back the Korean People’s Army. The Pusan Perimeter held. American and South Korean forces withstood a half dozen major assaults. Walker’s reputation was rehabilitated. The Fifth Air Force knew where to strike behind enemy lines and did so with more than fifteen thousand sorties between late June and mid-September. U.S. napalm and bombing attacks caused catastrophic shortages of ammunition. After the success of MacArthur’s Inchon landing, some North Korean divisions lost 80 percent of their soldiers.

“Walker lived in crisis,” wrote historian Fehrenbach. “His command decision had to be a never-ending series of robbing Peter to pay Paul. . . . He had to guess where the greatest peril lay, and guess correctly, for in war there is no prize for being almost right.”

In fact, thanks to Nichols’s code-break operation, Walker did not need to guess.

The Americans had “perfect intelligence,” recalled James K. Woolnough, then a junior commander in the Eighth Army in Taegu. “They knew exactly where each platoon of North Koreans were [sic] going, and they’d move to meet it. . . . is was amazing, utterly amazing.”

Walker had “what every military commander around the world secretly dreams about, near complete and real-time access to the plans and intentions of the enemy forces he faced,” wrote intelligence historian Matthew Aid.

Some of this intelligence came from Americans working desperately hard in Japan and the United States. The U.S. government might have been cryptographically clueless on the day of the invasion, but it quickly recovered. Using listening stations in Japan, American code breakers in Tokyo and Arlington, Virginia, discovered that the North Korean military communicated with easily breakable radio codes—and sometimes with no codes at all. Within a month, Americans knew everything the North Korean military was planning. Still, nearly two thirds of what they intercepted could not be turned into useful intelligence. For outside of Taegu, the Americans were crippled by a severe shortage of Korean translators. Another problem was getting intelligence to Walker in time for him to use it.

While Nichols did not have the analytical talent or the equipment that was available in the United States and Japan, he and his large stable of Korean code breakers and linguists were far better positioned inside the war zone. They had speedy access to Walker and Partridge, via a short ride in Torres’s jeep. The decoded information that Nichols produced was also speedily circulated in Taegu because it was not “so over-classified that it was unusable,” according to officers at Fifth Air Force intelligence, which relied almost exclusively on Nichols for bombing information.

When code breakers in Virginia learned in late August that North Korea was about to launch a major attack west of Taegu, there “appears to have been no mechanism” for their decoded information to reach Walker, according to the CIA’s Center for the Study of Intelligence. It said the general probably relied on intelligence reports from the team assembled by Nichols.

Walker, though, never had a chance to explain exactly how much he depended on Nichols in Taegu. He was killed on December 23, 1950, when his jeep collided with a South Korean army truck. The general had always insisted that his driver go fast on crowded roads and he often stood up in his jeep, chest thrust out, steadying himself by holding on to a grab bar.

Before Walker’s death, however, he made it clear that Nichols had helped save the Eighth Army, personally commending him for “his untiring efforts in procuring timely and invaluable information.” The South Korean government also gave Nichols credit for helping to save the Pusan Perimeter. By order of President Syngman Rhee, Nichols received the country’s second-highest military honor for valor, citing as the primary reason his work in obtaining and decoding enemy messages.

Partridge and his boss, General George E. Stratemeyer, commander of the Far East Air Forces, began pushing aggressively in the summer of 1950 for Chief Warrant Officer Nichols to be promoted—and not just because of his code-break triumph. Nichols had begun to play such a pivotal role in the war that his low rank raised awkward and embarrassing questions: Why is the best intelligence officer in the region a lowly chief warrant officer? Why is this twenty-something spy so important that he can snub and humiliate other officers? Don’t you have West Point–trained officers who are more capable than this middle-school dropout? By promoting Nichols from chief warrant officer to lieutenant, to captain, and finally to major, generals Partridge and Stratemeyer were rushing to keep up with the de facto authority that Nichols had grabbed amid the anarchy of war.

Even before U.S. forces broke out of the Pusan Perimeter, Partridge was giving Nichols new responsibilities. In early August, he asked Nichols to locate and map airfields, supply dumps, and other enemy targets that could be bombed by the Fifth Air Force. With the help of South Korean intelligence officers, Nichols found forty-eight agents, plucking them out of the South Korean army, air force, and police and sending them for a week’s training in Japan. They were parachuted behind North Korean lines on August 23.

“One Korean agent had the misfortune to break both of his legs in striking them on the horizontal stabilizer of the C-47 from which he parachuted,” said an Air Force history. “He was nevertheless able to convince a North Korean farmer to defect to the South and to carry him to safety. . . . Not only did the injured agent return safely, but he brought the important information of the whereabouts of an enemy fuel dump.”

This heartening story, however, was not at all typical of what happened to agents Nichols recruited and sent North. Most of his agents did not return from that first mission and the pattern would recur again and again. “Nobody expected them to return alive,” said Kim Bok-dong, a translator who worked for Nichols in 1950. “It was as if they were being sent to be killed.”

Several of the agents who beat the odds and managed to slip through North Korean territory were later killed by friendly fire as they tried to cross battle lines. Nichols would later tell his agents to “discard all their clothing and approach with their hands in the air,” which he claimed reduced losses. But the number of lives lost in parachute drops would remain staggering throughout the war. The toll stains the memory that many South Korean war veterans have of Nichols.

“Nichols was very focused, but he sacrificed too many Koreans to accomplish his mission,” said Lee Kang-hwa, a retired general in the South Korean Air Force who fought in the war and knew Nichols. “It is very unfortunate that a lot of Koreans were sent to die.”

Nichols was not the only American sending South Koreans to near-certain death. CIA operations during the war were “not only ineffective but probably morally reprehensible in that the number of lives lost and the amount of time and treasure expended was enormously disproportionate to attainments there from,” an agency review concluded years after the war.

In the panicked summer of 1950, however, the losses were acceptable, at least to the Americans. The deaths certainly did nothing to hurt Nichols’s reputation among Air Force and Army generals in Korea and in Tokyo. A measure of his rising status was the changed behavior of his prewar nemesis, army General Charles A. Willoughby, chief of intelligence for MacArthur in Tokyo. In April of 1950, two months before the outbreak of the Korean War, Willoughby had predicted there would be no war and he tried to throw Nichols out of Korea for writing intelligence reports that warned of an imminent North Korean invasion. By August, as the U.S. military struggled to stop that invasion, Willoughby wanted Nichols working exclusively for him.

In mid-September, Willoughby was “about to absorb” Nichols and his code-break team, a possibility that horrified Partridge, the Fifth Air Force commander who had come to depend on Nichols for reports that provided bomb targets across the Korean Peninsula.

“I strenuously oppose this move,” Partridge wrote in his diary. In the final months of 1950, Partridge referred five times in his diary to his turf war with Willoughby over Nichols. Panicked about what he should do to keep his best agent, Partridge even sought Nichols’s advice. Telling Partridge exactly what the general wanted to hear, Nichols said he was an Air Force man and wanted to remain an Air Force man. He seems to have used the tussle between the army and the Air Force to increase his own power.

Nichols wagered that he would have more resources and less supervision under Partridge than under Willoughby. It was a canny bet. Within six months, Nichols would take command of a special operations unit created especially for him (although the term “special operations” was not yet used in the U.S. military). He would also begin building spy bases on islands around North Korea. Willoughby, meanwhile, would soon be banished from the Far East Command and spend much of the rest of his life sidestepping responsibility for his errors in judgment in Korea.

Adapted from KING OF SPIES: The Dark Reign of America’s Spymaster in Korea by Blaine Harden, published by Viking, an imprint of Penguin Publishing Group, a division of Penguin Random House, LLC. Copyright © 2017 by Blaine Harden.