The UK’s ex-spy chief has warned the Home Secretary’s plans to force smart phone apps such as WhatsApp to hand over encrypted messages are unworkable and dangerous.

Robert Hannigan, the former head of GCHQ, ridiculed Amber Rudd’s threat to pass new laws to build “back doors” into secret messages as doomed to fail.

“Encryption is an overwhelmingly good thing - it keeps us all safe and secure,” Mr Hannigan said.

And he added: “Building in back doors is a threat to everybody and it’s not a good idea to weaken security for everybody to tackle a minority.”

The comments are a strong rebuke to Ms Rudd, who has argued for new laws to break into end-to-end encryption, if the tech companies refuse to act voluntarily.

The threat was issued in March, just days after Khalid Masood’s terror attack on Westminster, killing five people – having apparently sent WhatsApp messages just minutes earlier.

“It is completely unacceptable, there should be no place for terrorists to hide,” Ms Rudd said, of encryption.

“We need to make sure that organisations like WhatsApp, and there are plenty of others like that, don't provide a secret place for terrorists to communicate with each other.”

Threatening legislation, she added: “We have to have a situation where we can have our security services get into the terrorists’ communications. That’s absolutely the case.”

But Mr Hannigan said building back doors into apps was “technically difficult” and would not work.

“I can't see, particularly as many of these companies are US-based, that legislation is the answer on this,” he told BBC Radio 4’s Today programme.

“I don't think there is a magic solution where you can just legislate it away. Everyone would like a simple answer on encryption and unfortunately it is very difficult.”

Mr Hannigan described encryption on smartphone apps as “a good thing”, adding: “The best that you can do is work with the companies in a co-operative way to find ways around it.”

However, voluntary action looks unlikely, because Facebook-owned WhatsApp, which has a billion users worldwide, has said protecting private communication is one of its “core beliefs”.