A federal judge ruled Monday that the U.S. Justice Department cannot use a 227-year-old law to force Apple to provide the FBI with access to locked iPhone data, dealing a blow to the government in its battle with the company over privacy and public safety.

The ruling, by U.S. Magistrate Judge James Orenstein, applied narrowly to one Brooklyn drug case, but it gives support to the company's position in its fight against a California judge's order that it create specialized software to help the FBI hack into an iPhone linked to the San Bernardino terrorism investigation.

Orenstein belittled some government arguments, saying attorneys were stretching an old law "to produce impermissibly absurd results."

He rejected government claims that Apple was only concerned with public relations. He said he found no limit on how far the government would go to require a person or company to violate the most deeply-rooted values.

And he said claims that Apple must assist the government because it reaped the benefits of being an American company "reflects poorly on a government that exists in part to safeguard the freedom of its citizens."

Both cases hinge partly on whether a law written long before the computer age, the 1789 All Writs Act, could be used to compel Apple to co-operate with efforts to retrieve data from encrypted phones.

"Ultimately, the question to be answered in this matter, and in others like it across the country, is not whether the government should be able to force Apple to help it unlock a specific device; it is instead whether the All Writs Act resolves that issue and many others like it yet to come," Orenstein wrote. "I conclude that it does not."

Congress to hear testimony on encryption

Apple's opposition to the government's tactics has evoked a national debate over digital privacy rights and national security. On Thursday, the Cupertino, Calif.-based company formally objected to the California order, accusing the federal government of seeking "dangerous power" through the courts and of trampling on the company's constitutional rights.

The separate California case involves an iPhone 5C owned by San Bernardino County and used by Syed Farook, who was a health inspector. Farook and his wife Tashfeen Malik killed 14 people during a Dec. 2 attack that was at least partly inspired by the Islamic State of Iraq and Syria (ISIS) group. The couple died later in a gun battle with police.

Orenstein, ruling with an eye to the California case, referenced it multiple times in a 50-page ruling and noted that the government request there was far more "intrusive."

The New York case features a government request far less onerous for Apple and its cellphone technology; the extraction technique exists for that older operating system and it's been used before some 70 times before to assist investigators.

Since late 2014, that physical extraction technique hasn't existed on newer iPhones. In California, U.S. Magistrate Judge Sheri Pym ordered investigators to create specialized software to help the FBI bypass security protocols on the encrypted phone so investigators can test random passcode combinations in rapid sequence to access its data.

The court ruling comes one day before a Tuesday congressional hearing that will include testimony from FBI Director James Comey and Apple General Counsel Bruce Sewell on encryption and "balancing Americans' security and privacy."

Orenstein said he was offering no opinion on whether in the instance of this case or others, "the government's legitimate interest in ensuring that no door is too strong to resist lawful entry should prevail against the equally legitimate societal interests arrayed against it here."

He noted Congress has not adopted legislation that would achieve the result sought by the government and said it must be discussed by "legislators who are equipped to consider the technological and cultural realities of a world their predecessors could not begin to conceive."

The Justice Department said in a statement that it's disappointed in the ruling and plans to appeal in coming days. It said Apple had previously agreed many times prior to assist the government and "only changed course when the government's application for assistance was made public by the court."

A senior Apple executive said company policy has been to give the government information when there's a lawful order to do so, but that in New York the judge never issued an order and instead asked attorneys about the constitutionality of the government's use of the All Writs Act. The executive spoke on condition of anonymity to discuss a pending legal matter.

Apple has since declined to co-operate in a dozen more instances in four states involving government requests to aid criminal probes by retrieving data from individual iPhones.