Kim DotCom/Greg Sandoval/CNET

Officials in New Zealand's government apparently believed the law gave them the right to spy on MegaUpload founder Kim DotCom because he was a foreign national.

They were wrong.

In New Zealand today, Prime Minister John Key apologized to DotCom for the spying conducted against him by the Government Communications Security Bureau (GCSB).

The United States has accused DotCom of criminal copyright violations. In January, New Zealand police raided his home and arrested him. Just ahead of the raid, the GCSB began collecting intelligence against DotCom to see if he posed any danger to the police who would later swoop in by helicopter to arrest him.

Turns out that the GCSB isn't allowed to conduct such surveillance on New Zealand citizens, and the agency was under the false impression that DotCom, who was born in Germany, was not yet a citizen.

This meant that the spying was unlawful.

"I apologize to Mr. Dotcom," Key said, according to a report from New Zealand news site TVNZ.co.nz. "I apologize to New Zealanders because every New Zealander...is entitled to be protected from the law when it comes to the GCSB, and we failed to provide that appropriate protection for him."

It's not clear yet how this will affect the charges filed against DotCom by U.S. officials or their attempts to extradite him and six other associates to this country. MegaUpload was once among the largest cloud-storage services in the world. But U.S. officials claim the company was a criminal enterprise, and officials here are trying to extradite DotCom and the other defendants.

Increasingly, it appears less and less likely the United States will ever get DotCom or the other defendants out of New Zealand. DotCom is becoming something of a folk hero in that country, and many there don't like the way it appears their government caved in to demands from the United States.

The U.S. Attorney's Office alleges that DotCom and the other defendants encouraged users to upload copyrighted movies, music, and other media to the cloud-storage service. They then profited from ad sales and by charging for premium services.

DotCom, who has denied guilt, said today via Twitter: "Numerous unlawful acts against us by the NZ Gov have been exposed. It's time for a full, transparent & independent inquiry."

This is only the latest victory for DotCom. In June, a New Zealand court granted his request to be given access to evidence against him. The U.S. has appealed the decision. A judge then found that warrants used to seize his property were invalid.