John R. Bolton, a former top adviser to President Trump who became one of his most outspoken critics, pleaded guilty on Friday to mishandling classified information in a case that could send him to prison for up to five years.

Mr. Bolton, appearing in Federal District Court in Greenbelt, Md., admitted to a single charge of illegal retention of classified information over notes he compiled for a book that excoriated Mr. Trump.

“I’m sorry for it,” he told Judge Theodore Chuang, who set a sentencing date for Oct. 28.

Under the terms of the plea deal, Mr. Bolton must also pay a fine of $2.25 million and will forfeit his federal pension. Court documents suggest he is likely to receive close to the maximum sentence — five years — under federal guideline calculations that the judge could still alter.

During a proceeding that lasted 50 minutes, Mr. Bolton, in a dark suit and a striped tie, sat hunched forward at the defense table, flanked by his lawyers. He answered firmly and clearly as the judge described the terms and conditions of the plea deal.

If Mr. Bolton had gone to trial and lost, he could have faced decades in prison.

When he was first indicted, Mr. Bolton sought to frame the case against him as part of a push by the president to misuse the Justice Department to punish his perceived political enemies. The case against Mr. Bolton, however, began in the first Trump administration and gained momentum during the Biden administration, as investigators gathered additional evidence. The case became more serious when officials learned Mr. Bolton’s emails, and the secrets they contained, were stolen by foreign hackers.

“When guardians of our nation’s secrets play fast and loose with classified information, it opens the door for foreign adversaries to get their hands on it, which is exactly what happened,” said Roman Rozhavsky, a senior F.B.I. counterintelligence official.

In a statement, Mr. Bolton’s lawyer, Abbe Lowell, compared the case with the indictment against Mr. Trump in 2023, which accused him of mishandling classified information by keeping secret documents at his Mar-a-Lago estate after his first term. The judge overseeing the case against Mr. Trump, Aileen M. Cannon, dismissed those charges before the case went to trial.

Mr. Bolton “did what real leaders do,” Mr. Lowell said. “He took responsibility for a mistake he made. By contrast, President Trump thumbed his nose at the classified information laws, took actual classified documents to his Florida mansion, interfered with the investigation of that conduct and has never accepted any accountability for his conduct.”

Mr. Bolton, he added, “kept a record to preserve history, but Donald Trump kept secrets to serve himself.”

The accusations against Mr. Bolton center on his notes for “The Room Where It Happened,” his 2020 memoir about his time as Mr. Trump’s national security adviser. The original 18-count indictment against Mr. Bolton accused him of using personal email and a messaging app to share more than 1,000 pages of notes, which included national defense information, with two family members who did not have security clearances.

Those relatives were Mr. Bolton’s wife and daughter, according to people with knowledge of the matter, who spoke on the condition of anonymity to describe details of the case that were not in court filings.

According to the indictment, Mr. Bolton’s notes revealed that he understood that he was documenting intelligence secrets. One entry began, “The intel briefer said,” while another read, “While in the Situation Room, I learned.”

The first Trump administration fought unsuccessfully to prevent the publication of Mr. Bolton’s book, but the criminal investigation ultimately focused not on what was in the published manuscript, but instead on what Mr. Bolton wrote in private notes and correspondence.

Unlike some other investigations involving classified information, including the charges filed in 2023 against Mr. Trump, Mr. Bolton was not accused of retaining the secret documents themselves, but rather of keeping diaries and sending emails that mentioned details of his daily work in national security.

Mr. Bolton’s emails, however, were later hacked by someone associated with the government of Iran, the indictment said.

“A representative for Bolton notified the U.S. government of the hack in or about July 2021,” according to the filing, “but did not tell the U.S. government that the account contained national defense information, including classified information, that Bolton had placed in the account from his time as national security adviser.”

One section of the indictment described Mr. Bolton apparently being taunted by his hacker. A message on July 25, 2021, warned, “I do not think you would be interested in the F.B.I. being aware of the leaked content of John’s email (some of which have been attached).”

The email went on to declare: “This could be the biggest scandal since Hillary’s emails were leaked, but this time on the G.O.P. side! Contact me before it’s too late.”

A representative for Mr. Bolton forwarded the email to the F.B.I.

Kelly O. Hayes, the U.S. attorney in Maryland, sat in the courtroom during the proceedings and spoke to the news media outside the courthouse.

“No one is above the law,” she said, “and I hope that this prosecution sends a clear message that we will vigorously investigate and prosecute individuals who violate our national security laws without fear and without favor.”

Mr. Bolton left the courthouse about an hour after the proceedings ended and ignored questions from reporters as he climbed into a black S.U.V.