WASHINGTON — As a candidate for president, Donald J. Trump embraced the hackers who had leaked Hillary Clinton’s emails to the press, declaring at a rally in Pennsylvania, “I love WikiLeaks!”

To the cheering throngs that night, Mr. Trump marveled that “nothing is secret today when you talk about the internet.” The leakers, he said, had performed a public service by revealing what he called a scandal with no rival in United States history.

Now, after less than four weeks in the Oval Office, President Trump has changed his mind.

At a news conference on Wednesday and in a series of Twitter postings earlier in the day, Mr. Trump angrily accused intelligence agencies of illegally leaking information about Michael T. Flynn, his former national security adviser, who resigned after reports that he had lied about conversations with the Russian ambassador.

“It’s a criminal action, criminal act,” Mr. Trump fumed at the White House. In a Twitter message, he asserted that “the real scandal here is that classified information is illegally given out by ‘intelligence’ like candy. Very un-American!”