U.S. spy agencies were on the verge of an espionage breakthrough, closing in on the clandestine purchase of a Swiss company that could give American intelligence the ability to crack much of the world’s encrypted communications.

But the deal fell apart, done in by one of many behind-the-scenes battles between the CIA and the National Security Agency detailed in classified documents tracing one of the most remarkable intelligence operations in American history.

The terms of the transaction were all in place when the NSA abruptly “opted out” of negotiations to acquire the Swiss firm, Crypto AG, in the late 1950s, according to the documents. NSA’s last-minute balk is depicted as a typically misguided move by a code-breaking agency known for risk aversion, raising petty objections and “dithering.”

It took more than a dozen years to put the transaction back on track. The CIA and West German intelligence went on to acquire the Swiss firm, Crypto AG, in 1970, and used the company as an espionage platform targeting more than 100 countries.

The operation continued to be marred by frictions between the United States’ preeminent but perpetually squabbling spy siblings. Their rivalry is widely acknowledged in Washington, and has been cited as a factor in intelligence failures including the lack of warning before the terrorist attacks of Sept. 11, 2001.

But the classified history of the Crypto operation, which was written by the CIA’s Center for the Study of Intelligence, provides unusually direct insight into the nature of their antagonism and its consequences over years of unavoidable collaboration on high-stakes espionage.

The document contains the names of dozens of CIA operatives and NSA cryptologists, but the agencies emerge as characters themselves, with distinct institutional personalities.

The CIA comes across as an overbearing elder, impatient with its more timid counterpart, dismissive of its intermittent objections. CIA officials “made the rules as they went along,” according to the history, “and were much more inclined to ask forgiveness than permission.”

The NSA was full of people who were technically brilliant but struggled to grasp the potential of the operation, impeded efforts to expand its scope and at times put the program’s secrecy in jeopardy with sloppy tradecraft.

“NSA people traveled in true name, and sent far more people to meetings than CIA felt was advisable from a security standpoint,” the CIA history says. “One of the continuing irritants on the CIA side was this apparent lack of appreciation for traditional [agency] clandestine operational procedures.”

The account bears the clear stamp of a record compiled by the CIA‘s historical branch. But the document, which was completed around 2004, was drafted by an NSA historian and based to a large degree on files and input from both agencies. The Post obtained access to the CIA history and a separate account drafted in 2008 by the German intelligence service, the BND, as part of a reporting project with German broadcaster ZDF and the Swiss news channel SRF.

Current and former U.S. officials said many of the characteristics attributed to the two agencies still exist and that the institutional rivalry continues. But the often dueling spy services have had nearly 70 years to settle into their distinct roles and manage ongoing frictions.

“There was not a lot of love lost between CIA and NSA over the years,” said Larry Pfeiffer, who held senior positions at both agencies. Pfeiffer, who is now director of the Hayden Center for Intelligence at George Mason University, said the frictions have subsided in the post-9/11 period that triggered a major restructuring of the intelligence community and required extensive cooperation against terrorist networks.

But the two agencies still have sharp disagreements over their respective roles in cyberespionage and other issues, Pfeiffer said. “Their cultures can clash. The languages can be different.”

The CIA history describes the Crypto operation as “the intelligence coup of the century,” a program that enabled American spy agencies to read the messages of governments across the Middle East, Africa and Latin America. Under CIA and BND control, Crytpo also sold rigged devices to friendly governments including Spain and Italy as well as the Vatican. The communist world did not buy the machines, wary of doing business with a Western company.

The CIA controlled most aspects of the operation, though the agency’s role was known only to a handful of Crypto executives. It was up to the NSA to monitor messages being relayed around the world by Crypto’s far-flung customers, then decipher the coded messages so they could be shared with U.S. officials at the White House, Pentagon and State Department.

The CIA and the NSA declined to comment.

The operation’s peak years came after the CIA and BND purchase. But the files show that the deal was delayed for more than a decade by arguments between the U.S. spy services.

Crypto’s founder, Swedish engineer and entrepreneur Boris Hagelin, had cooperated with U.S. spy agencies starting in the early 1950s as part of a “gentlemen’s agreement” in which he restricted sales of his most sophisticated encryption machines to keep them out of the hands of Western adversaries.

But Hagelin made clear early in that arrangement that he wished to retire and was open to selling his company to U.S. spy agencies. He began formal negotiations in 1957 with William Friedman, a pioneer of American cryptology, but Friedman “ran into immediate opposition from NSA” after presenting the proposal to top officials.

The NSA cast doubt on the need for such a deal, believing that it could crack Hagelin’s machines without company help, and regarding many of the countries buying the devices as “low priority.” NSA officials also warned that “there was a very high likelihood that the whole clandestine arrangement would be exposed, and the thing would unravel.”

Friedman found the CIA to be far more enthusiastic, and drafted a detailed plan designed to surmount NSA objections. In one of the more pointed digs contained in the documents, the CIA history notes that Friedman’s case was so convincing that it temporarily “numbed even NSA into rational behavior.”

Friedman and the CIA thought they had found a clear path to buy out Hagelin in 1958 when the NSA “abruptly turned over all its files to CIA, and got out of the negotiating loop.” But the NSA raised new objections months later, and the plans to buy Crypto were put off indefinitely.

The collaboration smoothed somewhat after that acquisition, a deal that NSA acquiesced to after a final flurry of protests, according to the documents. At that point, U.S. spy agencies were forced to act to outmaneuver a rival bid for Hagelin’s company involving the French intelligence service.

Crypto became, in essence, a subsidiary of the CIA. But the two U.S. spy agencies continued to square off over primacy and influence — a dynamic quickly detected by their German counterparts.

“Between the CIA and the NSA there were always disputes about which of these services had the say,” a senior BND official said in that agency’s history of the operation. “CIA saw itself as the one in charge and emphasized this by having a CIA man posted at the operation in Munich,” the location of a CIA base for overseeing Crypto.

The BND files include gripes about several CIA officials. But some of the Germans’ sharpest — and, arguably, most petty — barbs are aimed at NSA operatives.

“The NSA people could speak only English,” one BND official complained. “Some of them even had heavy accents, for example, like a Texan that was so extremely difficult to understand especially in cryptologic topics.”

The Germans found NSA official Richard Kern, who was head of the agency branch responsible for the Crypto operation, particularly abrasive. Kern’s “confrontational style” caused such a “ruckus,” according to the BND account, that German officials complained directly to then-NSA Director Bobby Ray Inman. Kern died in 2013.

The CIA and the NSA also disagreed over issues of espionage tradecraft, although both were prone to careless behavior that risked exposure of the operation.

CIA and NSA officials visited Crypto’s factory in Switzerland several times in the 1970s, posing as consultants working on a contract with Motorola. They handed out business cards “printed up by the CIA cover staff” that carried the name of a phony front company. But the names on the cards were real, rather than aliases.

“They would sign the visitor register, sometimes in true name,” according to the CIA history, “and could be seen by factory hands” touring the Crypto compound. The cavalier approach enabled Crypto employees, and, later, journalists, to identify NSA employees including Nora Mackebee, a senior cryptanalyst, directly involved in meetings at Crypto. Mackebee could not be reached for comment.

In the mid-1990s, the CIA launched an overhaul of the operation’s security, cutting back on agency interactions with company executives, requiring “all direct contacts . . . to be from public telephones,” and scrubbing company documents, presumably including visitor logs, of “incriminating information.”

The documents contain sections in which CIA officials compliment the work of their NSA counterparts. The CIA history devotes substantial praise, for example, to an NSA cryptologist, Peter Jenks, who conceived of increasingly sophisticated ways to rig Crypto machines and dupe foreign governments. Jenks died in 1979 of cancer, according to the documents, but his ideas fueled the program’s success for years afterward.

The NSA also rescued the operation at a time when some at CIA began to raise doubts about its viability. A decade after acquiring Crypto, one of the agency’s top operatives in Germany warned headquarters of a “downward spiral” of risk and exposure as foreign governments became more technology-savvy.

After so many years of skepticism, officials at the NSA mobilized. They brought tables and graphs to show then-CIA Director Stansfield Turner how critical the operation had become to their ability to monitor the communications of dozens of foreign governments.

NSA officials also proposed recruiting a top cryptologist to install inside the company. This, according to the CIA account, led to the hiring of a Swedish mathematician, Kjell-Ove Widman, who proved remarkably adept at improving the company’s compromised designs and fending off the suspicions of both employees and foreign customers.

By the mid-1990s, the view within NSA was that “the project was a winner,” according to the CIA history. The program’s significance faded in recent years, as the spread of strong encryption software online reduced the market for dedicated machines from a Swiss manufacturer.

The CIA essentially dissolved Crypto in 2018 in a series of transactions that divided up its assets. By then, the NSA had long since moved on to more important targets that focused on finding ways to exploit the global reach of U.S. technology giants including Microsoft and Google, according to the trove of agency files exposed by Edward Snowden.

Peter F. Mueller in Cologne, Germany, contributed to this report.