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.