A Manhattan federal judge slammed a celebrity hacker with a larger-than-expected sentence because of the man’s secret plot — hatched from his prison cell — to sell the dirt he’d stolen from celebrities once released from prison.

Judge Paul Englemayer sentenced Alonzo Knowles — who prosecutors say hacked hundreds of celebrities — to five years in the slammer Tuesday. That’s almost double what Knowles was told to expect when he copped a plea last May.

But that was before he was caught bragging to friends and family — from prison — about selling a book that would feature “juicy” material “that ain’t public,” and that would “shake up Hollywood,” prosecutor Kristy Greenberg told the judge at his sentencing Tuesday.

Further demonstrating a lack of remorse, Knowles allegedly bragged to his fellow prisoners about information he had hacked from a well-known female celebrity — prompting them to beg him for her phone number, Greenberg said.

Greenberg also blasted Knowles for looking to profit from “sexually explicit” material he stole, including videos.

Knowles’ lawyer begged for leniency, saying his client was merely bragging and had no real plans to write a tell-all.

“I don’t think we can say, just because he said it means he was going to do it,” federal defender Clay Kaminsky said.

Judge Englemayer disagreed, calling the harsher-than-expected sentence a “wake-up call” for Knowles, 24, who was arrested last year after he was lured to the US in a sting operation. Knowles, who is from the Bahamas, was caught trying to sell unreleased movie and TV scripts to an undercover agent.

The judge called the prison emails “troubling,” and said they suggest he hasn’t learned his lesson and remains a danger to society.

“When you thought that no one was watching, you revealed your true colors,” the judge scolded.

“I can’t wait to get out, I already know how the cover is going to look,” Englemayer said, reading from one of Knowles’ emails.

The judge also blasted Knowles for initially resisting the government’s efforts to get him to turn over the computer where the hacked information was stored. Emails show him bragging to a paramour that he was willing to “risk an extra year in jail” in order to hold on the information, court documents show.

“They want me to be a loser and not be able to afford women like you,” Knowles allegedly told the paramour.

Knowles has since agreed to let US authorities destroy the computer, which was stored in the Bahamas. Judge Englemayer said he shaved a good year off Knowles’ sentence as a result.

The government has not released the names of Knowles’ victims, but they include a “popular hip-hop artist,” an executive with movie studio Twentieth Century Fox and R&B singer-turned-actress Naturi Naughton, and other people in the sports, entertainment and media industries, according to court documents.

Both Naughton and the Fox executive have provided the judge with victim impact statements.