The FBI director wants the keys to your private conversations on your smartphone to keep terrorists from plotting secret attacks.

But on Tuesday, the former head of the U.S. National Security Agency -- the supreme experts on communications -- said that would be a terrible idea.

General Michael Hayden, now retired, was speaking at a cybersecurity conference in Miami Beach. He expressed his unwavering support for encryption, a feature that protects voice calls or texts by turning data into nonsensical, indecipherable code.

"I disagree with [FBI director] Jim Comey," Hayden said in a speech. "I actually think end-to-end encryption is good for America."

At issue here is whether companies like Apple (AAPL) and Google (GOOGL) should offer encryption to customers. It safeguards their devices from anyone trying to break in, whether it's criminal hackers or snooping federal agents.

The Obama administration, initially irked by the challenge posed to surveillance, has dropped any plans to push for laws that bar or limit encryption.

But FBI Director James Comey continues to rail on tech companies for helping terrorists "go dark." He criticizes how tech companies have handed customers the only key to unlock their devices and chat logs. Comey wants companies to retain keys -- that open a "back door" in devices -- so that law enforcement can access them when needed to solve crimes.

On Tuesday, the ex-NSA director weighed into the encryption debate. He broke away from the view carried by law enforcement. He instead sided with most academics who study this subject.

There's a consensus on this among mathematicians and cybersecurity experts. Encrypting data protects everything. It raises a wall that keeps out everyone. Forcing companies to keep an extra set of keys to unlock customer data makes them a target for foreign spies and criminal hackers too.

"I know encryption represents a particular challenge for the FBI," Hayden said. "But on balance, I actually think it creates greater security for the American nation than the alternative: a backdoor."

Hayden's support for encryption could be seen as an awkward victory for privacy advocates. They typically oppose the efforts of NSA, the agency that hacks everyone and spies on the world's communications. And yet its former boss says a widespread privacy measure is a benefit that shouldn't be undermined.

Hayden likened the fight against encryption to the battles local governments are having against the ride-sharing service Uber.

"I can just win this argument on practical grounds," Hayden said. "When was the last time you saw the success of legislation designed to prevent technological progress? It's just not gonna happen."

One of the attendees at the conference, Swedish cybersecurity expert Robert Malmgren, said the ex-NSA director recognizes a simple fact: It's impossible to regulate a free, widely available software.

"The bad guys will break the law anyway," Malmgren said. "If encryption is outlawed, they don't care. They don't care about laws."

At times, the debate over encryption is overblown. In some cases, police actually have the capability to overcome encryption anyway. Law enforcement is known to use special hardware tools to crack the passcode on phones and obtain the data inside.

The latest example is explosive news this week that the Netherlands Forensic Institute, an arm of law enforcement there, figured out how to hack into BlackBerry devices -- which have a reputation for being incredibly secure.