When FBI special agent Steven Carr opened a package FedExed to him from the FBI’s New York office in December 2000, he assumed it would contain the typical inter-office issue. Instead, he found three letters that had been encrypted and mailed out to the Libyan consulate.

“I am a Middle East North African analyst for the Central Intelligence Agency,” the letter read. “I am willing to commit espionage against the US by providing your country with highly classified information.”

Carr then found items marked “CLASSIFIED SECRET” and “CLASSIFIED TOP SECRET” — mostly satellite images of Middle Eastern military operations. If Libya wanted more secrets, the spy wrote, it would have to take out an ad in the classified section of the Washington Post and pay $13 million.

“I will remain anonmus . . . I will be putting myself and my family at great risk,” one part read.

A US intelligence agency had a mole — and now the FBI had to figure out who he was. One detail stood out among the rest: He was a terrible speller.

A year’s worth of government secrets had been copied, encrypted and smuggled out of one of the nation’s 16 intelligence agencies, including hundreds upon hundreds of top-secret satellite images. They all shared one common detail: The encrypted letters included wrong letter choices, switched letters and outright missing ones in almost every word.

Carr could only hope that the spy would make a few real-world mistakes as obvious as the spelling mistakes in his letters. By searching the computer-network traffic for egregious spelling errors, Carr was able to narrow his search to one agency.

The National Reconnaissance Office (NRO), considered one of the “big five” US intelligence agencies, had been unwittingly harboring a traitor. The NRO was alerted to the breach in April 2000 and was stunned by the spy’s information.

“The sheer volume of material blew everyone’s mind,” writes journalist Yudhijit Bhattacharjee in his book about the case, “The Spy Who Couldn’t Spell” (New American Library, out now).

As Carr looked closely at persons of interest, a recently retired Air Force master sergeant stood out. He had written a rebuttal to a mediocre performance review that was impudent and riddled with spelling errors.

By March 2001, FBI agents launched an official investigation. After hundreds of suspiciously downloaded URLs were discovered on NRO hard drives, Carr felt confident they were homing in on a primary suspect. When they discovered a manual in that retired suspect’s former-and-still-unoccupied office titled Joint Tactical Exploitation of National Systems, whose copied table of contents had been included in the packages sent out to enemy intelligence, Carr became certain he had his man.

“Inside, on the cover page, written in block letters with a marker pen, was the name of the retired master sergeant,” Bhattacharjee writes. “Brian Patrick Regan.”

Regan, then 38, grew up on Long Island, the son of Irish immigrants. He was the third child of eight and the oldest son. He had a hard time at school. If he’d been able to get a diagnosis of dyslexia as a child, he might have had it easier, but as things stood, he fought hard for below-average grades.

When Regan graduated from high school, he joined the Air Force, where he discovered a knack for cracking codes. While stationed in Crete, Greece, he met a Swedish tourist whom he would later marry. In 1994, after he was promoted to master sergeant, he moved to DC to work for the NRO, one of the most secretive US intelligence agencies — in its earliest years during the Cold War, almost no one knew it even existed.

‘Regan was attempting to blackmail the United States government by holding hostage the secrets he had hoped to sell for cash.’

As the years went by, Regan flew under the radar. He was quiet and socially awkward. His relationship with his wife was cool at best, and on work trips, Regan had been known to have affairs, but he was nevertheless an attentive father who enjoyed playing with his four children.

Still, he could never quite make ends meet. He kept detailed records of the many credit cards he had taken out in both his and his wife’s names, along with the balances owed on each. They were rarely, if ever, paid down, and the bottom line totals steadily increased. By 1997, he owed $47,000.

With retirement looming, Regan decided he deserved more than a pension from the government for all his years of service.

“The idea of espionage began to take shape in Regan’s mind through the early months of 1999, as he found himself in the vortex of a perfect storm,” writes Bhattacharjee. “With the clock ticking toward retirement, Regan’s anxieties about the future transformed into a rising sense of panic.”

The FBI knew Regan had a ticket to fly to Switzerland on Aug. 23, 2001, and that he was planning to go door-to-door to embassies, peddling US secrets. Carr was given the go-ahead to make the arrest at the Dulles International Airport.

Three weeks before 9/11, the FBI quietly foiled what could have been a catastrophic case of espionage, following the largest data-security breach in United States history.

After Regan’s grand jury indictment, the NRO offered him a deal: Give up the encryption codes to allow the NRO to recover the documents stolen, in exchange for a wrist-slap sentence whereby Regan would serve fewer than nine years.

Regan was defiant. He refused the deal. Instead, he offered the government a deal of his own. If it didn’t agree to drop all charges, it would risk never recovering thousands of pages of state secrets.

“Regan was attempting to blackmail the United States government by holding hostage the secrets he had hoped to sell for cash,” writes Bhattacharjee.

Lydia Jechorek, Carr’s supervisor, voiced her concerns. Imagine the implications of letting Regan off the hook for his crime? But, the FBI couldn’t break the codes on its own. That’s when Regan slipped up.

He arranged for a letter to be smuggled to his wife, asking her to bury a series of playful items with the intention of justifying a Garmin GPS unit, a shovel and some handwritten maps, during his defense. Regan wanted to make it look like he had been preparing a giant region-wide treasure hunt for his children.

When the NRO discovered the letter, it threatened to charge his wife with obstruction of justice. In exchange for his wife’s freedom and a life sentence, Regan agreed to break his codes.