New Zealand Prime Minister John Key was in damage-control mode Thursday following the revelation that his intelligence agency illegally spied on Kim Dotcom, the 38-year-old founder of  file-sharing site Megaupload, who is wanted by the U.S. Justice Dept. on charges of Internet piracy and racketeering.

Key said he was “appalled” to learn that the Government Communications Security Bureau (GCSB) considered Dotcom -- who is a permanent resident of the country -- a “foreign person” and therefore subject to warrantless wiretapping and other forms of intelligence gathering.

On Thursday, Key apologized publicly to Dotcom.

"If things had been done properly, it would have been quite clear [Dotcom] was protected," Key said, according to Auckland Now. "It is quite a basic error. They have failed at the lowest hurdle. It's quite frankly not good enough."

Subsequently, security chief Ian Fletcher apologized to Key.

On Sept. 17, Key asked Inspector General of Intelligence and Security Paul Neazor to provide details after he learned from GCSB that it violated Dotcom’s civil rights, as protected by the constitution, by not verifying his residency status before spying on him and his colleague Bram van der Kolk.

“Dotcom is not on my understanding a New Zealand citizen -- he is Finnish or German. He is, however, one of a category of people who is treated in New Zealand as if he ought to have protection against collection of his information,” Neazor wrote in his reply.

The prime minister has ordered an audit of past intelligence-gathering activities dating back to 2009 and will likely focus on members the team behind the file-sharing site, which has generated tens of millions of dollars of profit for Dotcom.

Dotcom (who changed his last name from Schmitz in 2005), along with others, is accused by the U.S. of operating a massive copyright-violation machine. Megaupload is an online data storage-and-sharing site that earns revenue through subscriptions. It offers higher bandwidth (faster transfer speeds) than some of its competitors and earns income from advertisements. It also pays users cash for generating traffic to the site by uploading content that is widely downloaded by others.

As with other file-sharing sites, such as Pirate Bay and isoHunt, the most popular downloads are for copyrighted materials, such as movies and software. The U.S. wants Dotcom extradited and has gone after him in particular due to the popularity of the site and its egregious profit-making aspect. The U.S. claims the site has generated $175 million in revenue since 2005. The site was the eleventh-most-visited site on the Web until it was blocked in the U.S. in 2010, the same year Dotcom became a legal resident of New Zealand.

The GCSB said it was under the impression that Dotcom had one more step to go before becoming a permanent resident and therefore believed it had the mandate to spy on him.

In January, helicopters descended on Dotcom’s residence in Coatsville, outside of Auckland. His home was raided and cars, computers and art were seized. Dotcom and colleagues Van der Kolk, Finn Batato and Mathias Ortmann were arrested at the request of the U.S. Federal Bureau of Investigation and the Justice Dept. They were later released on bail. In June, a judge said the warrants used in the seizure and arrests didn’t adequately define the offenses.

Last month a New Zealand court allowed Dotcom to borrow funds using a government bond as collateral and sell several of his cars in order to cover legal expenses.

Meanwhile, the team behind Megaupload used YouTube on Wednesday to tease the public about a new music sharing service called Megabox.

Dotcom's next extradition hearing is scheduled for March, according to Reuters.