Lawmakers of both parties echoed those worries on Tuesday, threatening to take action if the companies didn’t satisfy their concerns.

“You’re going to find a way to do this, or we’re going to do this for you,” said Senator Lindsey Graham, Republican of South Carolina and the chairman of the Judiciary Committee. “You’re either the solution or you’re the problem.”

If Mr. Barr wants to push the issue with Facebook or another tech company, he could take the issue to court, as the government did during the fight over encryption with Apple in 2016. In that case, the Justice Department had secured a search warrant for the phone of an attacker in the San Bernardino shooting. Prosecutors successfully pursued a court order compelling Apple’s assistance. Apple opposed the order. But when the agency found another way to unlock the phone, it dropped the case.

Throughout the hearing on Tuesday, Facebook and Apple representatives said the companies were committed to working with law enforcement. The witness from Facebook detailed how the company could detect malicious content despite encryption.

Encrypting its messaging products is the central aspect of Facebook’s plan to rebrand itself as privacy focused, after being battered for years by revelations that it mishandled user data. But it has also put the company, which is already the subject of consumer privacy and antitrust investigations, on another collision course with governments around the world.