A short, handsome man bounces outside the colossal courthouse on Walnut Street, Newark, New Jersey. He's doing it to keep warm – it is freezing today – and also because he's fired up. His name is Andrew Auernheimer, but he's known across the internet as "weev". His 20 or so friends are young and pale, as if they spend too much time indoors. One, a good-looking woman, starts crying.

"I'll miss you, darling," he says, hugging her. "Don't cry on me now. It's all right."

Today is Andrew's sentencing. He has about an hour of freedom left. "Let's read some Keats," he announces. He opens his tablet: "'Poets and fanatics will be known…'" Ten minutes later: "I'm going inside. Let's roll."

Now we're in the elevator. I hear his girlfriend say, "As Bill Hicks said, 'Hitler had the right idea. He was just an underachiever.'" Then she adds, "I'm being facetious. You should not kill anyone."

We take our seats in court 5C. The ushers tell us to turn off our phones. It turns out that another man will be sentenced first. He appears from a door at the back. He's tiny, wearing a yellow jumpsuit emblazoned with the word "FEDERAL". Judge Wigenton takes her seat. She asks him if he has anything to say.

"Please forgive me for having committed this offence," he begins. "I'm totally repentant in my mind."

He says he loves America and wants to apologise to everyone in the room. This is unnecessary, because practically nobody here has any idea who he is. This crowd is for Andrew Auernheimer. The man has committed some drug-trafficking offence. He's already been in custody for 17 months. After some procedural business, the judge sentences him to time served. He's free to go. She wishes him luck. He looks ready to cry. While this is going on, eight very large men file in. They're US marshals. They're here because Andrew won't be released today.

Back in January, the young tech entrepreneur Aaron Swartz killed himself. His body was found in his Brooklyn apartment. He was facing prison for downloading a mass of copyright-protected academic journals belonging to the Massachusetts Institute of Technology. It was hardly the crime of the century, and he'd probably have got not much more than a warning had federal prosecutors not intervened. But they did. They announced their intention to send him to jail for up to 35 years. It was like drawing battle lines between the old world that valued copyright protection and privacy of information, and the new world that valued the opposite. Swartz, who had suffered from depression for years, hanged himself. He was 26. He left behind him, among other innovations, Reddit, the open-source social media site, which he helped build.

After his death, I became aware of lots of other Aaron Swartzes out there – hackers and pirates and activists facing prison for their ideology of internet freedom. It felt like a concerted worldwide prosecutorial effort to subdue a movement. So I began approaching them. I decided to contact only those people facing imminent imprisonment or trial. What in their lives had led them to that moment? How were they dealing with it?

Some of them ignored me. I suppose if I were facing prison, paranoid that prosecutors would be scrutinising my every careless word, I'd have ignored me, too. But Andrew Auernheimer emailed me back right away.

Now he stands up. Judge Wigenton asks him if he has anything to say. "I didn't come here today to ask for forgiveness," he begins. "The court should be making amends to me for the harm and the violence inflicted on my life. Many governments that have tried to restrict the freedom of the internet have ended up toppled."

Seventeen days earlier. I'm eating lunch with Andrew. He's telling me a story. "I had a friend," he's yelling. "He was one of the most brilliant engineers I knew. He stepped into an alley and got beaten up by four black dudes. He got brain damage. This guy, who was shrewd and brilliant, is not going to be shrewd and brilliant any more. I understood then that I would be totally OK with 100,000 people dying to preserve my one friend who was brilliant."

Considering Andrew's bleak future, I thought I'd take him somewhere fancy for lunch while I interviewed him. So I chose the elegant, wood-panelled Cafe Sabarsky on Manhattan's Upper East Side. I have come to regret this decision because of his propensity to say incredibly offensive things in a very loud voice. Fellow diners are glancing at us.

"Wow," I whisper. "You'd be fine to let 100,000 people die to preserve one especially clever person?" I'm talking in a pointedly quiet way in the hope that he gets the message and adapts his voice accordingly. "Where does that come from?" I whisper. "It's completely irrational."

"It's not completely irrational!" he shouts. "Society is driven forward by a very small number of innovators. I don't care about people who have nothing but contempt for innovators. I have no sympathy any more for the dregs of society. I don't care if they live or die."

Andrew Auernheimer was born in the Ozark Mountains of Arkansas in 1985. He grew up in a "crappy house in rural Missouri", hence, he says, his sanguine attitude towards prison. "Aaron was this wide-eyed, naive, liberal kid," he says. "I think that's why it hit him so hard. He thought he was from a special class of people that this just doesn't happen to. I've always known the government is out to get people like me. I'm poor and white and disaffected."

As Swartz entrepreneured, Andrew trolled. He actually calls himself a troll, which is rare for trolls. "Not many people are happy to refer to themselves as trolls," I say. "What does the word mean to you?"

"Trolling," he says, "is the use of rhetoric in a divisive manner. It's something very deeply connected to the western tradition. What did Socrates do? He asked fucking questions that were intended to inflame. And they killed him for it. And he's a hero of history."

"I have a problem with trolls," I whisper back.

"Why?" he yells.

"In satire," I say, "you only attack upwards. You only go for the powerful. Whereas trolls attack anyone."

Andrew nods, but he counters that trolls only attack "public figures". His definition of a public figure, though, is anyone with a Twitter account or a blog: "People want to be this shining star of the internet. And then they can't handle it when their public despises them! Ha ha!"

"Sometimes you meet trolls in real life and they're really meek," I whisper. "You're not that."

"Definitely not," Andrew yells.

"Andrew," I whisper, "the big difference between you and me is that I'm so much a part of polite society, I'm worried about the other people in this restaurant because you're being so loud."

"Oh, sorry," Andrew whispers. "I apologise. I'll take the volume down."

"I mean, look at everybody here," I whisper. "They're ladies who lunch."

"I have a gregarious personality," Andrew whispers.

For the rest of our interview, Andrew does indeed say everything in a considerate whisper. This is obviously difficult for him – anathema to his personality – and I start to feel bad for having mentioned it.

Andrew first realised he was a "person of interest" to federal authorities in 2006. He was, he whispers, "banging this girl at the time. We were in Vegas. We were going out to the fucking desert to shoot off our fucking AR-30s and whatnot, and she's like, 'We're being followed.' I'm, 'Are you're sure? Are you sure it's not the acid?' She's, 'I'm sure. I'm not tripping very hard.'"

"What's her name?" I ask, hoping I can contact her for fact-checking purposes.

"I do not reveal the identities of my sexual partners," Andrew replies. "This is not their struggle. Anyway, we started making a game. How can we ditch the surveillance in an as 'I'm better than you' a way as possible? So we take off in a fucking helicopter. We take speedboats to Catalina Island and wave."

I suspect all this – while unlikely-sounding – is true. When Andrew was interviewed by the New York Times in 2008, he turned up in a Rolls-Royce Phantom with "Claudia, a pretty college-age girl" in the front seat. (Today he turns up on the subway.)

"How did you afford all that ostentation?" I ask.

"I fucked rich girls and partied and networked," he replies. "If I wanted to, I could certainly make a suitcase full of money appear. I'm an innovative guy. Anyway, when they finally kicked in my door, the warrant said I'd been known as a troll and a hacker and a target since 2001, when I was 15. So, apparently, I was being surveilled when I was a fucking child."

The crime for which he's about to be imprisoned occurred in April 2010. Some security error in the AT&T web department meant that any technically adept snooper could download the email addresses of 114,000 people who had bought a particular iPad. Some were luminaries, such as New York mayor Michael Bloomberg and soon-to-be Chicago mayor Rahm Emanuel. "The technical content of this particular spectacle is probably the lowest I have ever done," Andrew says. "AT&T admitted at trial that they 'published' this data."

He and his friend Daniel Spitler thought it was hilarious. They sent each other messages debating what to do with the email addresses. Sell them? Phish them? (Phishing means trying to acquire information – usernames, passwords, credit card details, etc – for nefarious ends.) But instead they sent them to a journalist at Gawker. That would be embarrassment enough for AT&T, they figured. Within days they were arrested and charged under the Computer Fraud and Abuse Act – the same act that felled Aaron Swartz.

"That's the extent of your crime?" I say. "And they want to put you in jail for up to 10 years?"

"Without the possibility of parole," Andrew says. He shrugs. "Powerful people want to throw you in prison when you humiliate them. And I'm going to do it again and again, even from a prison cell, ha ha!"

"No parole means there's no incentive for you to be well-behaved," I say.

"I've never had any incentive to be well-behaved," he says.

Over in New Zealand, a similar fate threatens a very different man. Andrew Auernheimer says he's proud of his lack of assets. He doesn't believe in them. Kim Dotcom, by contrast, has more assets than practically any human alive. For years, this massively wealthy filesharing mogul was best known for the ridiculously flamboyant photographs he posted of himself online. Here was Kim Dotcom knee-deep in an azure ocean, pointing with one hand at a woman in a bikini splayed on the beach and with his other at his nearby giant yacht. Here was Kim Dotcom canoodling with a woman inside his beige private jet, and canoodling in a Jacuzzi, and dancing on a beach. His thumbs were forever aloft, his grin goofy. He looked like John Candy playing Al Pacino in a comedy remake of Scarface. And then he became famous for something else.

Kim Dotcom
Kim Dotcom: 'Prosecuting me is like putting a hand in a river. You can’t stop a river with your bare hands. Water just flows around them.' Photograph: Jessie Casson for the Guardian
In January 2012, New Zealand police launched, on behalf of the FBI, a dawn raid on his rented $30m Auckland mansion. They couldn't find Dotcom straight away. He was in his panic room. According to the Sydney Morning Herald, they seized $100m-worth of art, cash, bank accounts and cars – a 2008 Rolls-Royce Phantom Drophead Coupe with the licence plate GOD, some Mercedes-Benz, and so on. The FBI called Dotcom the world's biggest profiteer of piracy, costing copyright holders – movie studios and record companies – $620m in lost revenue. He wasn't, personally, a pirate. But he had created a file-sharing site – Megaupload – beloved of pirates everywhere. It was a safe house for pirates, a secure site for them to share files too large to be email attachments.

In retaliation for Dotcom's arrest, members of hacktivist collective Anonymous took down websites belonging to the US Department of Justice and the FBI. I ask Andrew if he's sympathetic to Kim Dotcom. He shakes his head. "I know a lot of artists who make their money living off the copyrighted content they generate," he says. "I respect their ability." His own war, he explains, is against those monolithic corporations that wield intellectual copyright laws to stifle scientific innovation. "This app called Instagram comes along and suddenly Kodak is bankrupt and they're very nervous and they're, 'Burn those witches over there. We are losing money. Make this stop.'"

Andrew's voice is rising to a yell again, but I don't mind because we've paid the bill and so everybody knows we're going to leave soon.

"So it's their inability to adapt?" I ask. "That's the problem?"

"Yes," Andrew says. "They are hellbent on maintaining their position. If they have to kill every single person like me, they will. And eventually we will realise that we'll have to point guns in their faces and rip them out of their big towers and perhaps beat and tar and feather them in the street to send out the message that liberty is more important than you!"

"And by liberty you mean the freedom to innovate on the internet?" I say.

"Yes!" Andrew says.

Kim Dotcom won't turn on his video Skype – "I am lying naked on my bed," he says – so we stick to audio. He's in his Auckland mansion. I've seen pictures of it. It's all giant fish tanks and novelty lampshades.

"When I see pictures of you, I'm reminded of Scarface," I tell him.

"Scarface?" He sounds hurt. "I wouldn't say Scarface."

Then he tells me about his childhood. And his weird love of bling suddenly starts to make sense. Kim Dotcom was born Kim Schmitz in Germany in 1974. "My father was an alcoholic. He really beat my mother up. He broke her bones. Many times we had to sleep in the bathroom, in the bathtub, in the cellar, just hiding away whenever my dad was drunk. She went to hospital many times. We had to spend nights in facilities for women who ran away from their violent partners." Kim pauses. "I still jump out of bed in the middle of the night from nightmares, thinking my dad is doing something to us again."

For this reason, Kim says, he has never tasted alcohol. "Would you believe that? Everybody thinks I'm this party animal. But it's not about the craziness of it. It's about the beauty of it. I love yachts. I love beautiful locations."

Kim's parents divorced when he was six. His mother took two jobs to keep them afloat. Kim stayed home watching James Bond movies.

"I felt so amazed by the glimpse of that lifestyle," he says. "People had their own islands! They lived in supertankers! I felt at home when I was watching this stuff. Those movies talked to me."

It doesn't dawn on me until later that the people in James Bond movies who have their own islands tend to be the super-villains.

Kim became a teenage hacker, finding ways to pirate video games: "In school you were highly popular when you could share the latest games with your friends. But you have phone bills. So then I heard about this program called Blue Box that allowed you to call for free. I was, 'Wow! This is an exciting world!' I got hooked."

One thing led to another, and soon Kim was hacking Nasa (where he failed to find evidence of aliens), running a messageboard for fellow hackers who wanted to swap information about the latest hacking tools. He took to engaging in epic battles with rival hackers. He'd hack into their bank accounts and change their debts, or their parents' debts. "Just messing a little bit with them."

"Oh my God," I say.

"Usually they'd contact me and say, 'OK, enough. I give up!'" Kim laughs. "And then the war was over and won."

I don't get many opportunities, during our two-hour conversation, to ask Kim questions. He is delivering a kind of sepia-tinted monologue. He couldn't be less like Andrew. Andrew is an ideologue, a desperado. Andrew says he's excited about prison. Kim really doesn't want to go to prison. He loves his wife and children. He loves his cars and yachts and beaches. Talking to me is a charm offensive born from desperate circumstance. And he is charming. By the end of his recitation – there are stretches lasting 25 minutes when I don't get a word in – I'm a jury member voting for acquittal. He says Megaupload is nothing more than a cloud service, beloved by pirates simply because it's good and easy to use. He says he's never personally uploaded an infringing file, and their terms of service were always clear: "'You can't share things that don't belong to you.' And now they are trying to blame me for third-party infringements. The entire internet is used by pirates. YouTube is the biggest hub for piracy in the world!"

He does have a point. In February 2011, NBC Universal commissioned a study to establish what percentage of internet traffic "involved the theft of digital assets". The answer: 23.8% worldwide.

"Prosecuting me is like putting a hand in a river," Kim says. "You can't stop a river with your bare hands. Water just flows around them."

But they're trying. In Sweden, the founders of the Pirate Bay website have each been imprisoned for a year. There are scores of others sharing people's data for different reasons – sometimes ideological, sometimes just for pranks, sometimes a mix of the two. There's Jeremy Hammond, facing life for hacking into Stratfor – a global intelligence company – and releasing millions of their private email exchanges. He was caught because another hacker, Sabu, the co-founder of the Anonymous offshoot LulzSec, had secretly been informing for the FBI since his arrest 10 months earlier. Sabu – whose real name is Hector Xavier Monsegur – was unemployed and caring full-time for his two young nieces from a housing project on the Lower East Side of Manhattan. When he was told he could face 124 years in prison, and that his nieces would go into care, he turned informant.

During the Stratfor hack, Anonymous members also stole subscribers' credit card details – to make $1m-worth of charitable donations with them, they said. An Anonymous person called Barrett Brown pasted a link to the Stratfor stolen credit card data in a chatroom and was arrested in September 2012. He's currently awaiting trial in a Texas jail. Between sharing the link and threatening an FBI agent in a YouTube video ("Robert Smith's life is over. And when I say his life is over, I'm not saying I'm going to kill him, but I am going to ruin his life and look into his fucking kids… How do you like them apples?"), Brown could be sentenced to 100 years.

In Shetland, another Anonymous/LulzSec hacker, Topiary – real name Jake Davis – was arrested in July 2011. A Daily Mail profile called him an autistic teen who was "so badly bullied [at school] that he left and retreated into a computer fantasy world". A few hours before his arrest, Davis deleted all his tweets except one: "You cannot arrest an idea."

There are civil actions, too. For example, John Wiley & Sons, publisher of the For Dummies books – including BitTorrent for Dummies – is suing BitTorrent users for illegally downloading its books (although that particular book is not named in its lawsuit).

A cloak-and-dagger meeting outside a subway station in Queens, New York. A battered car pulls up. The driver is young, white, of Spanish heritage and wears a big crucifix. I don't (still don't) know his real name. He says I should call him by his Anonymous name: Troy.

He takes me to a cafe where he grumbles about how things aren't like they used to be, about the good old days when you couldn't leave your mobile phone on a cafe table around here without it being stolen. I tell Troy that the good old days sound terrible to me, but he explains that with gentrification comes collateral damage. The police are forever stopping and frisking young people like him who don't look like preppy hipsters: "Going to the store, coming home from school, ruining your whole day. It's disgusting." He pauses. "It's dangerous to walk the borders around here."

It was these police inequities that compelled Troy to join Anonymous. I'm here to learn what the sustained prosecutorial bombardment is doing to the psyches of regular members such as him.

As we drink our coffees, Troy gives me a history lesson on Anonymous's endearingly ridiculous conception. In January 2008, a secret in-house Scientology promotional video starring Tom Cruise was leaked on to the internet. In it, Cruise announced that Scientologists were "the authorities on the mind" and only they were truly qualified to help car crash victims. The video went viral. Scientology lawyers swiftly sent take-down notices to every site they could, including the bulletin board 4chan.

"People's lives are too sad not to have that to laugh at," Troy says. "Scientology fucked up when they started pulling that shit down from our room."

That night, a bunch of 4chan users decided to form Anonymous. They instigated protests outside Scientology buildings and "DDoS actions" against their websites, taking them down: "Imagine sitting at a website and manually pressing refresh a few hundred thousand times," Troy says, explaining DDoS attacks. "People have made tools that do the same thing. You can set the refresh rate at, say, 800,000 times in a minute. Go off, have a cup of coffee, have a cigarette, two hours later the FBI's watching you. And all you did was press a button."

When Kim Dotcom was arrested, Anonymous retaliated by DDoSing the Department of Justice and the CIA. After Aaron Swartz's suicide, they DDoSed MIT. "My favourite," Troy says, "was Operation Payback." This was when they DDoSed PayPal for blocking donations to Wikileaks. "PayPal let you donate to the KKK but not to Wikileaks?" Troy says.

Fourteen people are currently facing up to 15 years in prison each for the PayPal DDoS attack, including a 21-year-old woman called Mercedes Haefer, whose Facebook pictures have her wearing bunny ears and a comedy false moustache.

"Good people are going to jail for ridiculous things," Troy says. "People who get their kicks watching a meme of a cat are going away for 24 months, 26 months."

He says anxiety and paranoia have wormed their way in. All his online activities are legal, he says, but sometimes he sleeps for only two nights in a month. He paces his room, researching countries that don't have extradition treaties with the US.

"If people cannot handle paranoia," he says, "then Anonymous is not the path to take."

Mercedes Haefer
Mercedes Haefer: 'Being arrested is really fun. You get to troll the FBI.' Photograph: Mark Mahaney for the Guardian
A vast and opulent loft apartment above an old grocery store in the Lower East Side of Manhattan. It belongs to the lawyer Stanley Cohen. He's spent his life representing anarchists and communists and squatter groups and Hamas. Sitting with him is his client, Mercedes Haefer. She's one of the PayPal 14. Her trial is impending. This is why, Stanley says, he has ground rules. He doesn't want the prosecution cherry-picking quotes to bolster the state's narrative. So he's going to sit in and listen. I give him a look to say, "I understand your position, but I'm worried your ground rules will make for a terrible interview."

"It's fine," Stanley reassures me.

"So," I say to Mercedes, "how did you get involved in Anonymous?"

"She's not involved in Anonymous," Stanley interrupts. "We don't acknowledge that there's an entity called Anonymous."

My heart sinks.

"This is one of their obligations in a conspiracy trial," Stanley continues, "to prove the existence of organised crime. They have to prove there's an entity called Anonymous. We don't say there is. In fact, we say there isn't. It is what it is." Stanley shrugs. "If you question her about how she got involved in internet activism, I don't have a problem with that."

Mercedes laughs throughout this. She told me earlier that she has to laugh because if she woke up every morning thinking about the possibility of 15 years in jail, she might as well throw herself off the Hoover Dam, which is near where she lives in Las Vegas, where she's studying sociology at the University of Nevada.

"How did you get involved in internet activism?" I ask her.

"4chan," she says. "It's full of funny stuff. It's full of people being the worst of people, which, even when it's disgusting, is fascinating." She tells me a story. There was a 4chan user "who is genuinely in love with his dog, and his dog went in heat, so he went around collecting samples, and injected them into his penis, and got her pregnant, and they're his puppies." Mercedes laughs. "That's the thread I told the FBI about when they asked me about 4chan, and some of the officers actually got up and left the room. Ha ha!" She tells me other – less grotesque – 4chan stories, such as the time they tracked down a boy who was posting videos of himself on YouTube physically abusing his cat "and daring people to stop him". 4chan users found him "and let the entire town know he was a sociopath. Ha ha! And the cat was taken away from him and adopted."

I ask about the attack on PayPal. She looks at Stanley. "You can say what they say you did," he tells her. So she does.

It was 6 December 2010. "Flyers had been posted on 4chan and Facebook saying an action was going on and people could join a chatroom to get updated details. The bulk of the case is that I was a chatroom moderator."

Stanley says the PayPal attack was not a success and that PayPal itself refers to it as "an attempt". "Being the macho pigs that those fuckers are," Stanley says, "PayPal sent their public spokesperson out there to basically say, 'They came for us, they're ridiculous, they did nothing, it didn't affect us, it didn't hurt us, we didn't lose a penny.' But then the Obama administration decided to make an example."

"Why?" I ask.

"This is the government's way of saying, 'We're going to be an 800lb gorilla until we figure out the next level of foolproof safeguards,'" he says. "'Until we figure it out, if you fuck with us, we're going to burn you.'"

The FBI showed up at Mercedes' apartment one morning at 6am. "I answered the door and they said, 'Mercedes, do you mind putting your pants on?'" She pauses. "To be honest, being arrested is really fun. You get to troll the FBI, you get to wear fancy handcuffs, you get to pick the music in the car. But the indictment was boring. I napped through it."

I imagine federal prosecutors hope that the war on internet outlaws won't be like the war on drugs – dragging along for ever and destroying countless lives needlessly through the legal process. I suspect they're counting on pirates and DDoSers being easy to subdue with a few draconian prosecutions. In fact, both Mercedes and Troy tell me that since Aaron Swartz's suicide, people have been thinking twice about doing illegal things. But when I ask Mercedes if she thinks these prosecutions are going to end the movement, her response is trenchant and sharp.

"The police are trying to claim the area," Mercedes says. By "area", she means the internet. "Just like in the cities. They gentrify the downtown, move all the poor people into ghettos and then start trolling the ghettos, stopping and frisking people. They're saying, 'Look at what we can do to you on your own turf. This is not your space. It's our space, and we're letting you exist here.' People socialise on Facebook because where do you go to loiter in New York any more? The internet is our space and they're trying to take it, and it's not going to happen because it's the internet."

"And you know more about how it works than they do?" I ask.

"Fuck them," she says. "They're idiots. If you understood medicine in Massachusetts at a certain time, you were a witch and they would burn you. There aren't a lot of people these days who can get past Facebook. So explain to them how a router works and you're a magician. You're a dark wizard. 'We need to lock them away for ever because we don't understand how else to stop them.'" She pauses. "Part of the reason all these kids have become experts on the internet is because they don't have power anywhere else. Skilled trade is shrinking. That's why they went there. And then, holy shit, it blew up."

And so to Newark district court, where Andrew Auernheimer is giving his final words before the judge hands down her sentence. He says he seeks no forgiveness and that they should be apologising to him. Then the prosecutor, Michael Martinez, has his turn. He says that contrary to the defence's assertion that no great technical wizardry occurred during the AT&T hack, it did take a brilliant brain, which is what Andrew has.

"He's tremendously talented with computers," Martinez says. "In fact, only last night he was answering questions on the online forum Reddit…"

Andrew's friends in the public benches howl with derisive laughter on hearing that his prosecutor was lurking on Reddit last night. He reads out some of the things Andrew wrote: "'I hack code in 11 different languages on a regular basis' and, 'I won't be nearly as nice next time.'He's going to reoffend," Martinez says, "and he won't be 'nearly as nice next time'. Those were his words last night."

But Andrew is only half-listening. Instead, he's tweeting.

"Put that phone away!" someone shouts. Andrew keeps typing. All at once, the room is filled with violent commotion. The US marshals rush over to him. Within an instant, his face is pressed down on the table, his arms pulled behind his back. His friends let out horrified, startled gasps.

"Stay there!" the Marshals warn them.

Five seconds later, Andrew is being pulled from the room. His girlfriend is crying loudly.

His final tweet of freedom reads, I later see, "No matter what the outcome, I will not be broken. I am antifragile."

There's a short adjournment. Then Andrew is brought back. His feet are chained together and the chain snakes up his legs and around his waist. With his black hoodie, he looks like a monk doing penance.

Martinez picks up where he left off. He tells a story about Andrew. One time, he says, Andrew published a page on a site for trolls called Encyclopedia Dramatica. The page was about someone called MG. MG begged Andrew to take the page down. It was "demeaning and embarrassing and hurtful", he emailed Andrew. Because of the page, MG had lost his job, and it was like being bullied in middle school all over again. "I am pleading with you," he wrote to Andrew. "Please at least take down the nude photos."

Andrew emailed MG back: "$500."

"His entire history puts his own interest above the interests of others," the prosecutor finishes. "What did 114,000 iPad owners do wrong to have their information given to a journalist at Gawker? Why should he be the master of our privacy?"

It's time for the judge to pass sentence. Andrew will go to federal prison for 41 months. After that, there will be three years of supervised release. There's a fine, too – $73,000 restitution to be paid to AT&T.

Afterwards, Andrew's friends point out that on this same day two teenage football stars convicted of drugging and raping a 16-year-old girl in Steubenville, Ohio – videotaping her and urinating on her – are receiving sentences of one and two years.

This all happens a few weeks before the Boston Marathon bombings – which turns out to be an enormously important moment for Anonymous. On 18 April, the police have the alleged attackers surrounded in Watertown, Massachusetts. An Anonymous Twitter feed – @youranonnews – is at least 20 minutes ahead of the mainstream media with the news of the ambush. With its access to police scanners and eyewitness reports, Anonymous is scooping the networks in terms of timing and accuracy. Great numbers of people are turning to them before they turn to CNN that night. I wonder whether actions such as Andrew's will become redundant as the group grows in credibility and maturity.

A few days after Andrew is sentenced, I ask Paul Fishman, the US attorney for New Jersey, how he ended up with 41 months. "His accepting no responsibility," he replies, "his attempt to tweet from a mobile device between his legs in the courtroom during sentencing, and then pushing away the marshal who was trying to get the device – none of that helped him." Forty-one months was the maximum sentence for a hack such as his, Fishman says. The minimum would have been 33 months. Andrew got the maximum because – a senior justice department official tells me – the court has a "mandate to consider the characteristics of the defendant". It didn't help, I'm sure, that Andrew told Gawker in November 2012 that Judge Wigenton was "a mean bitch, I hear. I can see it in her eyes, she's a black Baptist Bush appointee."

For the US attorney's office all this is, of course, black and white: "It's about stopping him, punishing him, and telling other people, 'You could be next,'" the justice department official tells me. "He can't walk into your house, look around, take a note you wrote to your wife, send it to the news media and say, 'Hey, Jon, you should have locked your door.' There's no altruism here. He's a self-aggrandising guy looking to enhance his own reputation by exploiting loopholes he's smart enough to identify." He pauses. "There's a perfectly fine argument to make for an open internet," he says, "but the law doesn't allow people to take matters into their own hands. There are basic rules. You can't steal other people's stuff. Even if you think people shouldn't own that stuff – that's not up to you."

Back in the court in Walnut Street, just after the judge passes sentence, Andrew is led away in shackles. Before he vanishes, he shoots a grin to his friends and calls out: "All hail Discordia!"

• This article was amended on 6 May 2013. The original said the publisher of the For Dummies books was suing BitTorrent users for illegally downloading its book BitTorrent for Dummies. That particular book is not named in its lawsuit. The original also described Aaron Swartz as co-owner of Reddit. That description has been amended to say he helped build Reddit.