Robert P. Hanssen, a former F.B.I. agent who spied for Moscow off and on for more than two decades during and after the Cold War in one of the most damaging espionage cases in American history, was discovered dead in his prison cell in Colorado on Monday, federal authorities announced. He was 79.

The Federal Bureau of Prisons said in a statement that Mr. Hanssen was found unresponsive just before 7 a.m. at the United States Penitentiary Florence, where he was serving a life sentence. He was pronounced dead after lifesaving efforts by emergency medical workers. The statement did not identify a cause.

Mr. Hanssen’s case was considered one of the most notorious spy scandals of his generation, shocking F.B.I. leaders and other government officials when they learned that one of their own had been feeding information to the other side with impunity for so many years. To this day, the F.B.I. describes him as “the most damaging spy in bureau history.”

In exchange for $1.4 million in cash, bank funds and diamonds, Mr. Hanssen passed along a torrent of secrets to Moscow, including one disclosing that the United States government had dug a tunnel underneath the Soviet embassy in Washington to eavesdrop on diplomatic and other communications. He also informed Moscow about three K.G.B. officers who were secretly spying for the United States, two of whom were later executed.

“The magnitude of Hanssen’s crimes cannot be overstated,” Paul J. McNulty, who was the U.S. attorney who prosecuted him, said on Monday in response to reports of his death. “They will long be remembered as being among the most egregious betrayals of trust in U.S. history. It was both a low point and an investigative success for the FBI.”

Mr. Hanssen’s arrest, in 2001, briefly ruptured relations between the United States and Russia at a time when the two former enemies were seeking to build friendlier ties after the collapse of the Soviet Union. President George W. Bush expelled about 50 Russian diplomats, and President Vladimir V. Putin of Russia retaliated with a tit-for-tat expulsion of 50 American diplomats. But both sides were determined to end the matter there and not allow it to result in a more lasting rift.

The discovery of Mr. Hanssen’s espionage embarrassed the F.B.I. and resulted in changes to security procedures. He told investigators after his arrest that security at the bureau was so lax that it amounted to “criminal negligence.” He said it was a simple matter to gain access to classified material on official computers with only routine security clearances.

“Any clerk in the bureau could come up with stuff on that system,” Mr. Hanssen said, according to a Justice Department report on his case in 2002. “It’s criminal what’s laid out.”

Mr. Hanssen pleaded guilty to 15 counts of espionage and conspiracy to avoid the death penalty and expressed remorse for his betrayal. “I am shamed by it,” he said during the 2002 hearing where he was sentenced to life in prison without parole.

Since July 17, 2002, Mr. Hanssen had been in custody at Florence, the supermax facility that is considered the most secure prison in the federal system and used in recent years to house convicted terrorists. Inmates there are typically held in solitary confinement for 23 hours a day.

Mr. Hanssen joined the F.B.I. in 1976 as a special agent and went on to hold several counterintelligence positions that gave him access to classified information. He began spying for the Soviet Union three years after joining the bureau, when he was assigned to a counterintelligence unit in New York, by walking into the New York offices of Amtorg, a Soviet trade organization that was known to be a front for the Soviet military intelligence agency.

He stopped spying for several years starting in 1980, after his wife, Bonnie, walked in on him in the basement of their home in Westchester County, N.Y., and he quickly tried to cover up his papers. He confessed to her and to a priest affiliated with Opus Dei, the conservative Catholic organization to which the couple belonged.

In 1985, he began spying again, providing information to the K.G.B. This time he did a better job of covering his tracks, using encrypted communications and other secret methods; even the Russians never knew who he was. Identifying himself only by code names like B and Ramon Garcia, Mr. Hanssen turned over sensitive information said to include specific satellite intelligence collection capabilities.

He stopped spying again after the Soviet Union collapsed, then resumed again in 1999. His betrayal went undetected for years as he collected at least $600,000 in cash and diamonds from the K.G.B. and its post-Soviet successor, S.V.R., which told him that they had set aside another $800,000 for him in a Moscow bank, according to prosecutors.

In the 1990s, after the arrest of Aldrich Ames, a C.I.A. agent who had also spied for the Russians, the F.B.I. and the C.I.A. realized that someone else was still providing Russia with classified information, and they began “Graysuit,” a hunt for the unknown double agent. But it was not until 2000 that investigators were able to narrow the search, when the F.B.I. paid $7 million to a former Russian intelligence officer for a file on the anonymous mole who called himself B — a file that included an audio recording with a voice that two F.B.I. analysts who knew Mr. Hanssen eventually recognized.

Using fingerprints, the F.B.I. confirmed that the mole was Mr. Hanssen and surveilled him for months, even promoting him to keep better track of him. In February 2001, agents arrested him in Foxstone Park in the Washington suburb of Vienna, Va., a few blocks from his home, after he had left classified documents in a garbage bag at a “dead drop” for his Russian handlers under a wooden footbridge.

Mr. Hanssen seemed unsurprised at finally being caught. “What took you so long?” he reportedly asked when arrested.

Robert Philip Hanssen was born on April 18, 1944, in Chicago to Vivian and Howard Hanssen, a career Chicago police officer who did intelligence work for the department. An only child who was seen as nerdy and never fit in, Robert had a difficult relationship with his father, who emotionally abused him. He grew up obsessed with James Bond, collecting spy gadgets and even opening a Swiss bank account.

Mr. Hanssen received a bachelor’s degree in chemistry in 1966 from Knox College in Illinois, where he also studied Russian, but after graduation he was rejected by the National Security Agency when he applied for a position in cryptography. He enrolled in dentistry school at Northwestern University, but later transferred to the business school, where he received a master’s degree in business administration.

While in dentistry school, he met and married Bonnie Wauck and converted from Lutheran to join her Roman Catholic faith. After a year working at an accounting firm, he took a position with the Chicago Police Department specializing in forensic accounting. Four years later he moved to the F.B.I.

Bright but brittle, Mr. Hanssen was said to have burned with resentment that he did not receive the respect and assignments he felt he deserved. With six children in parochial schools or college, he attributed his decision to spy for Moscow to money, although his reasons were never fully understood.

“Many of the factors that have motivated or influenced traitors in the past — such as greed, ideology, career disappointments and resentment, and drug and alcohol abuse — do not apply to Hanssen or do not fully explain his conduct,” a Justice Department inspector general’s report on the case said in 2003.

Mr. Hanssen led a double life in more ways than one. An active member of the Roman Catholic lay organization Opus Dei, he presented himself as a religious and committed anti-communist conservative. But according to reports, he also visited strip clubs, allowed a friend to clandestinely watch him having sex with his wife and engaged in what was said to be a secret but nonsexual relationship with an exotic dancer whom he plied with gifts and took on an F.B.I. trip to Hong Kong.

Mr. Hanssen’s ability to avoid detection was a signal failure of the American intelligence apparatus. His own brother-in-law, who also worked for the F.B.I., reported suspicions about Mr. Hanssen to the bureau a decade before his arrest, but the supervisor he told had dismissed his concerns.

Mr. Hanssen was the subject of multiple books and films, including a television movie in 2002 in which he was played by William Hurt and a full-screen movie called “Breach” in 2007, in which he was played by Chris Cooper.

“Hanssen was a thicket of paradoxes, a suburban dad and outwardly devoted family man who professed to be deeply religious while at the same time betraying family, faith and country, all and everyone who ever mattered to him,” Ann Blackman, a co-author of “The Spy Next Door,” said on Monday. “For 21 years, through the terms of four presidents and three F.B.I. directors, he fooled them all.”

Jesus Jiménez contributed reporting from New York.