In short, it appears that Mr. Trump and the Saudis have helped the government achieve what years of repression could never accomplish: widespread public support for the hard-line view that the United States and Riyadh cannot be trusted and that Iran is now a strong and capable state capable of staring down its enemies.

On the day of a state-orchestrated commemoration ceremony for Mr. Hojaji, Morteza Hosseinzadeh, a 33-year-old graduate of theater studies at Tehran University who considers himself a reformist, came out early to pay his respects. Dressed in black and holding a poster bearing the portrait of the new martyr, he looked every bit like the hard-line supporters of the clerical government.

“There are many here like me, who don’t care for the Islamic Republic and its rules,” he said. “But today is about something bigger than that, one of us has been killed. At the same time this American president is breaking our hearts with his rhetoric and threats. We have to choose sides. I choose for my country.”

Iran’s hard-liners are savoring the reversal in their fortunes, after losing influence in the Obama years. “Thanks to Trump’s dishonest, cheating and crazy remarks, he has proved what we have said for a long time: America cannot be trusted,” said Hamidreza Taraghi, a hard-line political analyst. “Many didn’t believe us, but now they do.”

The seeds of the new nationalism were planted with the election of the moderate Hassan Rouhani as president in 2013. It was the first glimmer of hope for Iran’s urban middle classes since the brutal crackdown on street protests following the 2009 presidential election, widely seen as rigged.

President Rouhani promised a nuclear deal, to escape the suffocating international sanctions over Iran’s nuclear program that had depressed and distorted the economy and isolated the country. When the treaty was signed in 2015, Iranians rejoiced over the chance, finally, to become a “normal” country.

Then came the Trump administration, and its singular focus on Iran as the source, as the defense secretary, Jim Mattis, has said on numerous occasions, of all the troubles in the Middle East.