Dina Temple-Raston is NPR’s Counter-terrorism correspondent and the author of four non-fiction books, including “The Jihad Next Door: Rough Justice in the Age of Terror.”

Not long ago, a former National Security Agency director sat down with a mathematician who had come to work for him — and a pop quiz quickly ensued.

“What’s the most important thing about encryption?” the mathematician asked the head of the NSA.

“Scrambled text,” his boss replied, thinking that preventing the enemy from reading a coded message was paramount.

“Wrong,” the visitor said. “The single most important thing is attribution.”

"Cyberspies: The Secret History of Surveillance, Hacking, and Digital Espionage" by Gordon Corera (Pegasus)

Attribution is so important, he continued, because only one person can give the order to fire nuclear weapons — the president — so it is critical to know that the order is coming from him and no one else.

“What’s the second most important thing about encryption?” the mathematician continued.

“Scrambled text,” the director said hopefully.

“No. It is the integrity of the data.” That’s because, he explained, a corrupted message, with a single digit wrong, could cause a nuclear missile to be launched at the wrong target.

So begins just one of the fascinating episodes that appear in BBC journalist Gordon Corera’s terrifically engrossing new book, “Cyber Spies: The Secret History of Surveillance, Hacking, and Digital Espionage.”

In this new digital age, he reports, scrambling a message so someone can’t read it — once the staple of any self-respecting spy organization — doesn’t even rank in the top four most important things about encryption. In Corera’s telling, non-repudiation and availability (a system that can’t go down) have become much more important than what was traditionally a top priority: writing a message others can’t read.

The NSA director story offers an important glimpse into the challenges facing modern-day spies. Computers may offer new, clever ways to hide messages and steal secrets, but Corera argues that they cut both ways. “Few outside the intelligence world understand the extent to which spies in the US and Britain percieve technology as an existential threat to their work,” Corera writes. “An arms race is on between spy services to exploit technology. Only those who adapt will survive.”

“Cyber Spies” goes a long way toward making sense of this evolution. The account is chronological, so it is blissfully easy to follow. It opens inside the barracks of Bletchley Park, as Corera masterfully re-creates the anguish Britain’s code-breakers felt as they tried to unravel German code during World War II.

He chronicles the accidental release of the first global computer virus, known as the Morris worm — unleashed by an unsuspecting MIT student in November 1988 (there’s a twist here, but I won’t ruin it). “There’s a simple equation here,” Corera writes, explaining why such an event was inevitable. “The easier you made it for machines to talk to one another, the easier you made it for something bad to spread among them.”

As the BBC’s security correspondent, Corera clearly has developed a knack for dissecting the complicated and making it comprehensible. Some of the book’s most powerful sections explain some infamously complex hacks, from Stuxnet, the cyberattack that destroyed centrifuges in an Iranian nuclear facility, to North Korea’s foray through Sony Pictures’ servers, to China’s alleged ingestion of some 21 million government personnel files from the U.S. Office of Personnel Management.

Had “Cyber Spies” been given a fall publication date instead of a summer one, Corera is sure to have included the latest breach: the hacking of email systems at the Democratic National Committee and the Democratic Congressional Campaign Committee. While U.S. officials have yet to say publicly that Moscow was behind the intrusion, Corera helpfully presents readers with an easy guide to international hackers.

“China’s programmers are said to employ well-known tools; Russians are more likely to write their own code,” Corera reports. “China’s hacking is often sloppier and easier to spot (hence all the attention), while Russia’s hackers are more expert and operate below the radar.”

He provides a crafty but sinister example in which Russians began trolling for information about a particular executive whom the intelligence services had determined was gay but not out of the closet. “The hackers then sent him an email from a gay rights organization which they suspected he would open since it looked as if it was sent to him, but in fact held malware,” Corera writes. “They then counted on the fact that, even if the executive did suspect it was malware, he would not be willing to go to his company’s IT department or security team for fear it would reveal his sexuality. This is classic, high level, targeted Russian espionage.”

Russian hackers also differ from Chinese ones, Corera reports, in that it is widely understood that leaders in Moscow will leave them alone on two conditions: first, that they don’t attack Mother Russia, and second, that when the state calls upon them, they will do its bidding.

“It has even been claimed that Russian hackers who are convicted are offered the chance to work for the intelligence services rather than go to jail,” he writes. “All of this would provide a significant but also largely deniable capability for the Russian state, wielded in conjunction with intelligence services.”

Not to be outdone, the United States and Britain also have their own hacking teams. Britain’s elite cyberspies are part of the Government Communications Headquarters’ Joint Threat Research Intelligence Group, and, Corera writes, they have moved beyond simply gobbling up an enemy’s data.

They also have the “capacity to deny, disrupt, degrade or deceive a target online,” he writes. “This might involve taking an opponent’s computer offline, or it could involve manipulating information using all the old-fashioned tricks. . . . This is what spies have always done. But now they do it online.”

One effort called, rather colorfully, “Operation Cupcake” involved changing an article in an al-Qaeda publication. Instead of a recipe for a bomb that would help would-be jihadists build a device on their own, the Brits “had its content replaced with garbled code which proved to be recipes for cupcakes.”

Cyber-sabotage is becoming increasingly common. Defense Secretary Ashton Carter has made clear that cyberattacks on the Islamic State will be a major part of the operation to retake the Iraqi city of Mosul. He has talked about disrupting the group’s “command and control, to cause them to lose confidence in their networks, to overload their network so they can’t function.” Carter told NPR in an interview that this was “something that’s new in this war.” He added, without providing detail, that some of the cyberattacks will be “surprising” when they are finally made public.

Surprising to some, maybe, but perhaps less so to readers of “Cyber Spies.” In laying out such an accessible, comprehensive history, Corera has prepared us for the future — when espionage, diplomacy, science and technology all come crashing ashore. He makes clear that when the power of computers is fused with the imagination of the human mind, anything — for good or ill — can happen.

Cyber Spies
The Secret History of Surveillance, Hacking, and Digital Espionage

By Gordon Corera

Pegasus. 431 pp. $29.95