Michael Cohen, a former lawyer for President Donald Trump, was sentenced to three years in prison Wednesday after denouncing Trump and explaining that "I felt it was my duty to cover up his dirty deeds."

Cohen gave an emotional apology to the court for his involvement in a hush-money scandal that could threaten the Trump presidency — a scheme to buy the silence of two women who said they had affairs with Trump to protect his chances before the 2016 election. Cohen said his blind loyalty to Trump led him to ignore "my own inner voice and my moral compass."

Separately Wednesday, federal prosecutors said that they had struck a non-prosecution agreement with American Media Inc., the company that produces the National Enquirer tabloid, for its role in squelching the women's stories. AMI paid $150,000 to one of the women, Karen McDougal, before the 2016 election. As part of the agreement, AMI admitted it made the payment principally "in concert" with Trump's campaign to "suppress the woman's story so as to prevent it from influencing the election," according to a statement from the U.S. Attorney's Office for the Southern District of New York.

The sentencing in federal court in Manhattan capped a startling fall for Cohen, 52, who had once hoped to work by Trump's side in the White House but ended up a central figure in the inquiry into payments to McDougal, a former Playboy model, and Stormy Daniels, an adult-film star.

Judge William H. Pauley III called Cohen's crimes a "veritable smorgasbord of fraudulent conduct" and added, "Each of the crimes involved deception and each appears to have been motivated by personal greed and ambition."

He added that Cohen's particular crimes — breaking campaign finance laws, tax evasion and lying to Congress — "implicate a far more insidious harm to our democratic institutions."

"As a lawyer, Mr. Cohen should have known better," the judge said.

Cohen had pleaded guilty in two separate cases, one brought by federal prosecutors in Manhattan, the other by the office of the special counsel, Robert Mueller, who is investigating Russian interference in the 2016 election.

Before he was sentenced, a solemn Cohen, standing at a lectern, sounded emotional, but resolved, as he told the judge he had been tormented by the anguish and embarrassment he had caused his family.

"I blame myself for the conduct which has brought me here today," he said, "and it was my own weakness and a blind loyalty to this man" — a reference to Trump — "that led me to choose a path of darkness over light."

Cohen then apologized to the public: "You deserve to know the truth and lying to you was unjust."

Rudy Giuliani, one of Trump's lawyers, called Cohen's assertion he had acted out of loyalty to Trump "a complete lie."

"I feel sorry for him," Giuliani added. "He's a pathetic serial liar."

Federal agents raided Cohen's office and home in April, and he later turned on Trump, making the remarkable admission in court that Trump had directed him to arrange the payments.

Trump at first denied knowing anything about the payments, but then acknowledged that he had known about them. This week, he insisted that the payments were "a simple private transaction" — not election-related spending subject to campaign-finance laws.

He also maintained that even if the hush-money payments were campaign transactions in violation of election law, that should be considered only a civil offense, not a criminal one.

Since Cohen came under investigation, Trump has mocked him as a "weak person" who was giving information to prosecutors in an effort to obtain leniency.

In fact, Cohen did not sign a formal cooperation agreement with the U.S. attorney's office in Manhattan or with Mueller. In addition to the campaign-finance violations, Cohen pleaded guilty to charges of tax evasion, making false statements to a bank and lying to Congress.

He took a calculated gamble in pleading guilty to this litany of federal crimes without first entering into a cooperation agreement with the government. He offered to help prosecutors, but only on his terms, and there were some subjects he declined to discuss.

His lawyers argued he should not serve time in prison. Federal prosecutors in Manhattan said he deserved around four years.

Pauley said Cohen's assistance to the special counsel's office, though useful, did not "wipe the slate clean," and a "significant term" of prison was justified.

In the end, the judge gave Cohen three years for the crimes he committed in New York and two months for lying to Congress, to be served at the same time. He was also asked to pay nearly $2 million in fines, forfeitures and restitution. The judge ordered Cohen to begin serving his sentence March 6.

Cohen's sentencing was unusual because it involved guilty pleas he had made in cases brought by the two separate prosecutors.

In the case brought by Mueller's office, Cohen pleaded guilty to lying to Congress about the duration of negotiations to build a Trump Tower in Moscow, as well as about the extent of the involvement of Trump.

Cohen's three-year sentence is the first substantial prison term in a case stemming from Mueller's inquiry. The special counsel had referred the case to the U.S. attorney's office in Manhattan.

The Washington Post contributed to this report.