Either way, what started as a cordial two-hour discussion about combating Islamic extremism ended with the White House and Mr. Cook agreeing to disagree — foreshadowing a bitter battle between a president long enamored of Apple products and Silicon Valley and a tech titan who has spoken enthusiastically of Mr. Obama.

Although the president and Mr. Cook are not personal friends, associates say they have developed a relationship of professional admiration and mutual self-interest. At the least, the two share similar traits: discipline, a cerebral nature and impatience with office drama. Now they find themselves in roles no one ever imagined, as the central antagonists in the raging debate between personal privacy and the nation’s security.

By refusing demands from Mr. Obama’s Justice Department to help unlock a phone used by one of the San Bernardino, Calif., terrorists, Mr. Cook has become the leading voice in Silicon Valley for encryption. By voicing strong support for his F.B.I., Mr. Obama is now the effective chief prosecutor of the administration’s case for allowing law enforcement to penetrate iPhones.

If Apple had more of a presence in Washington, as do Google, Facebook and Microsoft, technology executives say there is a chance the dispute might have been quietly resolved. But few top Apple veterans have moved through the revolving door that has brought engineers and executives from other technology companies to the Obama White House to serve in a variety of security, technology and scientific positions. Apple’s lobbying budget in Washington is far smaller than its competitors’.

“I have not talked to the president. I will talk to the president,” Mr. Cook said in an interview with ABC News last week, a day before his company filed legal papers opposing the government. Mr. Cook said he planned to ask Mr. Obama “for his help in getting this on a better path.”