Apple will contest a court order to help FBI investigators access data on the phone used by San Bernardino gunman Syed Rizwan Farook.
The company had been ordered to help the FBI circumvent security software on Farook's iPhone, which the FBI said contained crucial information.
In a statement, Apple chief executive Tim Cook said: "The United States government has demanded that Apple take an unprecedented step which threatens the security of our customers."
"We oppose this order, which has implications far beyond the legal case at hand."
Since September 2014, data on the latest Apple devices - such as text messages and photographs - have been encrypted by default.
If a device is locked, the user's passcode is required to access the data. Entering an incorrect code 10 times will automatically erase the phone's data, if this option has been enabled.
Apple says even its own staff cannot access the data.
The FBI has asked Apple to do two things.
Firstly, it wants the company to alter Farook's iPhone so that investigators can make unlimited attempts at the passcode without the risk of erasing the data.
Secondly, it wants Apple to help implement a way to rapidly try different passcode combinations, to save tapping in each one manually.
The FBI wants to use what is known as a "brute force" attack, trying out every combination until stumbling across the correct one and unlocking the phone.
Farook is understood to have used a four-digit passcode which means there are 10,000 possible combinations.
Could Apple do what the FBI has asked? - Dr Steven Murdoch, cybersecurity expert
"It's difficult to say with any degree of certainty - Apple does not disclose enough about its operating system to know.
Other secure products such as smart cards are designed so that the security cannot be changed after they leave the factory.
Some devices such as bank computers wipe themselves if you try to change the software.
But phones are not designed primarily from a security perspective.
It's possible that forcing a new version of iOS onto the phone would wipe it. Apple may have designed it in this way - and it would certainly be the more secure way to do it."
Apple said the FBI's demands set "a dangerous precedent".
"The FBI wants us to make a new version of the iPhone operating system, circumventing several important security features, and install it on an iPhone recovered during the investigation," wrote Mr Cook.
"The government is asking Apple to hack our own users and undermine decades of security advancements that protect our customers.
"Opposing this order is not something we take lightly. We feel we must speak up in the face of what we see as an overreach by the US government."
Farook and his wife killed 14 people in the California city last December before police fatally shot them.
Analysis - Dave Lee, BBC North America Technology Reporter
Apple decided to enable encryption by default to avoid precisely this kind of ethical dilemma.
If it was impossible to access the data, the courts wouldn't even ask - so this approach by the FBI may have taken them by surprise.
What is striking about Apple's letter is that it does not say whether or not the court's demand is technically possible.
Some believe it is, via a process called a Device Firmware Upgrade.
In fighting the court order, Apple will have the support of its technology peers, and perhaps the majority of its users.
For the FBI, yet more frustration over an issue it feels Apple's skilled developers should be able to solve safely.
"We have no sympathy for terrorists," said Mr Cook.
"We are challenging the FBI's demands with the deepest respect for American democracy and a love of our country."