The company said that could potentially undermine encryption for millions of other users.
Cook's response, posted early today on the company's website, set the stage for a legal fight between the federal government and Silicon Valley with broad implications for digital privacy and national security.
US Magistrate Judge Sheri Pym had ordered Apple to help the FBI break into an iPhone belonging to Syed Farook, one of the shooters in the December 2 attack that killed 14 people. Farook and his wife, Tashfeen Malik, died in a gun battle with police.
The FBI wants to be able to try different combinations in rapid sequence until it finds the right one.
More From This Section
The Obama administration has embraced stronger encryption as a way to keep consumers safe on the Internet but has struggled to find a compelling example to make its case.
Cook said "this moment calls for public discussion, and we want our customers and people around the country to understand what is at stake."
"In the wrong hands, this software, which does not exist today, would have the potential to unlock any iPhone in someone's physical possession," Cook wrote.
"The FBI may use different words to describe this tool, but make no mistake: Building a version of iOS that bypasses security in this way would undeniably create a back door. And while the government may argue that its use would be limited to this case, there is no way to guarantee such control."
Under US law, a work phone is generally the property of a person's employer. The magistrate judge told Apple in Tuesday's proceeding to provide an estimate of its cost to comply with her order, suggesting that the government will be expected to pay for the work.