But with the Sept. 11 attacks came a new group of enemies. In recent years, the C.I.A. has been closely tethered to Colonel Qaddafi’s intelligence service as it hunts for information about operatives of Al Qaeda in North Africa.

Now, the uprising against the Libyan leader, along with the revolts that drove out the presidents of Tunisia and Egypt and threaten other rulers, have cast a harsh light on the cozy relationships between America’s intelligence agencies and autocratic, often brutal Arab governments. The C.I.A. faces questions about whether such ties blinded it to undercurrents of dissent and may now damage America’s standing with emerging democratic governments.

Top American officials say that the C.I.A.’s close ties to Libya brought important benefits: the dismantling of Colonel Qaddafi’s nascent nuclear weapons program, and a partnership to track terrorist cells in the country.

But Dennis C. Blair, the former top American intelligence official, said that while spy services in places like Libya and Egypt were cooperating with the United States against Al Qaeda, they were “aggressively and sometimes brutally suppressing dissent in their own countries.”

“Not only did these intelligence relationships interfere with our ability to understand opposition forces, but in the eyes of the citizens of those countries they often identified the United States with the tools of oppression,” said Mr. Blair, who served until last May as President Obama’s director of national intelligence. He added that the recent uprisings offer an opportunity to “align our intelligence relationships with our national values.”

The seeming collision of American interests was evident in 2009, when the State Department’s human rights report on Libya was a gruesome inventory of disappearances and torture. Months earlier, however, a diplomatic cable, obtained by WikiLeaks, called the Qaddafi government a “strong partner in the war against terrorism” and declared the relationship with Libya’s spy service “excellent.”

A C.I.A. spokesman declined to comment about the agency’s ties to foreign intelligence services. But Michael Scheuer, who spent two decades at the C.I.A. in counterterrorism operations, said it was absurd to believe that such work could be done without the help of unpalatable allies.

“Foreign policy and intelligence doesn’t have anything to do with values,” Mr. Scheuer said. “It has to do with material interests and security. We would be blind in most of the world if we only dealt with Rebecca of Sunnybrook Farm.”

Some former C.I.A. operatives believe that Libya’s help in counterterrorism has been overstated. Vincent Cannistraro, a former head of the C.I.A.’s Libya branch, said Colonel Qaddafi often tried to paint his political enemies as terrorists, and that the C.I.A. became “sucked in” to working with a ruthless government because the spy agency was desperate for information about potential militants.

“I understand that to get intelligence about bad people, you can’t just deal with nuns and Boy Scouts,” said Mr. Cannistraro. “But in the case of Qaddafi, you’re talking about dealing with a murderer.”

One cautionary tale on American dealings with both Egypt and Libya is that of a Libyan Qaeda trainer known as Ibn al-Shaikh al-Libi, caught in Pakistan late in 2001 and delivered to Egypt. He claimed to know of ties between Al Qaeda and Saddam Hussein — but recanted after the invasion of Iraq, claiming he had been tortured by his Egyptian captors. Returned to Libya by the Egyptians, he was reported by the Libyan state press in 2009 to have hanged himself in prison. Specialists on Libya say that more likely he was killed by the authorities.

In other countries in the region, the United States may have had a clearer view of internal politics than it did in Libya. But American intelligence officials nonetheless may be in an uncomfortable position in a country like Bahrain, where a diplomatic cable in December 2009 reported that the head of the Bahrain National Security Agency, Sheik Khalifa bin Abdallah al-Khalifa, “unabashedly positions his relationship with the U.S. intelligence community above all others, insisting that his key lieutenants communicate openly with their U.S. liaison partners and actively seek new avenues for cooperation.”

In the current crisis, those close intelligence relations may offer a channel for candid communications. But this week, the same Bahraini intelligence and security officials who have worked so closely with their American counterparts have again used force to crush pro-democracy demonstrations, acting on orders from the country’s monarchy and with the backing of Saudi Arabia.

In Libya, the United States withdrew its ambassador in 1972, three years after Colonel Qaddafi and other young officers seized power, and shut its embassy in Tripoli in 1979 after a mob set fire to the building.

Libyan and American spies had virtually no contact for two decades, until the C.I.A. in 1999 began a series of clandestine meetings with Libyan intelligence operatives in Europe to gather information about Islamic terror networks working in North Africa.

In his 2007 autobiography, the former director of central intelligence, George J. Tenet, said that the awkward meetings with Moussa Koussa, the Libyan intelligence chief, were “illustrative of the surreal world in which we had to operate” as C.I.A. operatives had to exchange pleasantries with the man they believed responsible for the Pan Am 103 bombing, among other terrorist plots.

But Mr. Tenet cited the easing of tensions with Libya as one of the major successes of his tenure, as it led to cooperation between the two spy services against Al Qaeda and the end of Libya’s banned weapons programs.

Paul R. Pillar, the National Intelligence Council’s top Middle East analyst from 2000 to 2005 and a member of the American team that negotiated with the Libyans over their nuclear program, said counterterrorism was one of the “most durable forms of intelligence cooperation.”

“There’s simply no substitute for working with people who are in the line of fire of some of these terrorist groups and have all kinds of advantages in language and culture and local knowledge,” he said.

Yet the C.I.A.’s counterterrorism relationship with Libyan intelligence hardly gave American officials insight into the Qaddafi government or its internal tensions and opponents, said a Middle East expert who has consulted with the agency.

“I think the C.I.A. has found it extraordinarily difficult to get intelligence on Libya,” said the consultant, who would speak only on condition of anonymity because of his dealings with the agency.

Developing independent contacts in Libyan society was virtually impossible, he said, in part because American personnel who left the embassy were routinely accompanied by multiple Libyan security vehicles. As Libyan officials explained it, the convoys were for the Americans’ own protection.