WASHINGTON — Veteran counterspy Brian Kelley, who broke a code on how Moscow communicated with its agents and was mistakenly hounded later by the FBI, has died. He was 68.

Kelley died in his sleep of an apparent heart attack and was found Monday, his wife, Patricia, told The Washington Times.

Kelley spent 20 years with the Air Force Office of Special Investigations, doing counterintelligence work, until 1984, when he moved to the CIA. While working in the agency’s counterintelligence office, Kelley figured out a method used by Moscow to secretly communicate with its agents. That led to the discovery of State Department diplomat Felix Bloch, a suspected spy meeting a KGB officer in Europe, but the investigation of Bloch was compromised.

“No one could figure out who would have tipped off the Russians,” Kelley later told CBS’ “60 Minutes.” ”We had it so tightly held at CIA that only about seven people knew, and they eliminated everybody but me.”

When CIA counterintelligence officer Aldrich Ames was arrested as a spy for Moscow in 1994, the FBI began searching for a second mole and focused on Kelley because the Bloch probe had been compromised.

In August 1999, FBI agents questioned Kelley for hours in an effort to make him confess. Kelley refused and told the agents, according to the Times: “Your facts are wrong. Your conclusions are wrong. Your underlying hypothesis is wrong.”

FBI agents continued to harass Kelley and his family for the next two years, sidelining his CIA career.

Kelley eventually was cleared and went back to work with the CIA. He retired in 2006.

CIA spokesman Preston Golson told The Associated Press that Kelley kept the faith with his country and his colleagues.

Kelley was born in Waterbury Conn., and graduated from Saint Michaels College in Vermont, before joining the Air Force.