Victor Marchetti, a high-ranking CIA officer who grew disillusioned and co-wrote a best-selling book in the 1970s about the agency’s inner workings, resulting in a legal battle over whether the CIA could censor the writings of its past employees, died Oct. 19 at his home in Ashburn, Va. He was 88.

He had complications from dementia, his son Chris Marchetti said.

Mr. Marchetti joined the CIA in 1955, after a college interview with a mysterious figure who had two missing fingers. Mr. Marchetti thought he had found his spiritual and professional home.

“I was going to be a mysterious person — adventurous, romantic, living in foreign countries,” he told The Washington Post in 1971.

He rose in the agency’s ranks, with laudatory performance evaluations. He became an intelligence analyst who specialized in Soviet military affairs and by 1968 was executive assistant to the CIA’s deputy director, Rufus Taylor.

A year later, Mr. Marchetti resigned for “personal reasons.”

“Sitting up there I began to see how it’s all pulled together, the interplay with the rest of the executive branch of the government,” he told The Post. “The agency is the most romantic segment of the intelligence community, but I began to lose faith in it and its purpose, in intelligence in general.”

In 1971, he published a novel, “The Rope-Dancer,” which portrayed an inept “National Intelligence Agency” whose director was a spy for the Soviet Union. The CIA grew concerned that Mr. Marchetti had gone rogue.

Notable deaths in 2018 and 2019: Nipsey Hussle, George H.W. Bush, Stan Lee, John McCain, Aretha Franklin and other famous faces we’ve lost

Nipsey Hussle, a Grammy nominated rapper who sought to revive South Los Angeles, died at age 33 on March 31. Read the obituary (Matt Winkelmeyer/Getty Images for Warner Music)

At the time, revelations were emerging about clandestine wars, secret airlines and CIA ma­nipu­la­tion of international rebellions and coups. With his inside knowledge, Mr. Marchetti wrote a nonfiction manuscript, “The CIA and the Cult of Intelligence,” with former State Department intelligence officer John D. Marks. They described what they considered an agency that had too much money and not enough supervision.

“He was a man deeply offended by what he perceived to be wrong,” Marks said Saturday about Mr. Marchetti. “He showed great courage in standing up for what he believed.”

CIA officers are required to sign an agreement that any books or articles they write about espionage, whether fact or fiction, must be cleared by the agency beforehand. When Mr. Marchetti and Marks submitted their book to the CIA for review, it came back with demands that 339 passages be removed because they could compromise national security.

The authors and their publisher, Alfred A. Knopf, supported by the legal arm of the American Civil Liberties Union, filed suit. They charged that the CIA was improperly imposing “prior restraint” before publication and therefore violating the First Amendment right of freedom of the press.

“The agency’s attempt to muzzle Marchetti was one of the first maneuvers to put a curtain of secrecy in front of itself,” historian John Prados, the author of several books about the CIA, said Saturday in an interview.

Mr. Marchetti and his legal team argued that much of the information in the book was already on the public record or was absurdly benign — such as descriptions of wood-paneled offices.

Over time, the CIA’s lawyers relented on about half of their suggested changes. When the book was published in 1974, it contained 168 blank sections marked with the word “Deleted.” (By the time a federal judge in Virginia further reduced the number of deletions to 27, the book was already in production.)

“The CIA and the Cult of Intelligence” became a best seller, and Mr. Marchetti appeared on “The Dick Cavett Show” and was interviewed in Penthouse magazine.

In 1975, CIA Director William E. Colby admitted the agency had violated its charter by spying on U.S. citizens — one of whom was Mr. Marchetti. The same year, a Senate committee led by Frank Church (D-Idaho) investigated abuses by the CIA. Marks and Mr. Marchetti’s book “gave the Church Committee a road map that this is what the CIA does,” Prados said.

Mr. Marchetti’s legal battle with the CIA was hardly over, however. In May 1975, the U.S. Supreme Court declined to review a lower-court order upholding the CIA’s policy of requiring its officers to submit any manuscripts for review. Mr. Marchetti had, in the words of the federal court decision, “effectively relinquished his First Amendment rights,” the moment he signed his employment agreement with the CIA.

It was a principle later applied to books written by CIA officers Philip Agee and Frank Snepp. Agee was stripped of his U.S. passport, and Snepp was sued by the government and forced to surrender his book royalties to the CIA.

Victor Leo Marchetti Jr. was born Dec. 23, 1929, in Hazleton, Pa. His father was a professional boxer who later worked in the family hardware store and plumbing business.

After high school, Mr. Marchetti moved to New York and later Paris, then joined the Army, working in an intelligence unit in Germany. By the time he graduated from Pennsylvania State University in 1955, he had been recruited by the CIA.

Survivors include his wife of 65 years, the former Bernice Baran of Ashburn; three sons, Victor Marchetti III of Herndon, Va., Jeffrey Marchetti of Centreville, Va., and Chris Marchetti of Ashburn; eight grandchildren; and five great-grandchildren.

His ordeal with the CIA made Mr. Marchetti something of a role model for whistleblowers. He occasionally testified in court cases — but often had to obtain CIA approval to do so.

“I’m beginning to get a bit uncomfortable about this,” he said in 1977. “Maybe they want to hurt my credibility by having me rush to the defense of every rotten red pinko and defector they can find.”

Mr. Marchetti worked as a consultant, speaker and freelance writer, sometimes publishing articles in right-wing journals known for propagating conspiracy theories. He often looked back on his days in the CIA.

“I lost everything I had,” he told The Post in 1980. “If I had it to do all over again, I’d have kept my mouth shut. I’d have played the game.”