Robert Morris, a mathematician and cryptographer who was among the top U.S. computer security experts and a leading developer of the widely used Unix operating system, died June 26 at a hospital in Lebanon, N.H. He was 78 and had complications from dementia.

Wizardly in his abilities and appearance – he had a scraggly, gray beard – Morris was the digital gatekeeper of the American government’s computer secrets.

Morris worked at the National Security Agency, the country’s code-making and code-breaking apparatus, from 1986 to 1994. As chief scientist with the NSA’s National Computer Security Center, he led a team that defended the military’s networks from outside attack.

He also played a crucial role in the military’s cyber offensive against the government of Iraqi leader Saddam Hussein. He helped disrupt Iraq’s computer systems in the months before the 1991 Persian Gulf War began.

At Bell Laboratories in the 1970s, Morris worked on the security protocols for the Unix computer operating system. Today, Unix-based operating systems can be found on millions of Apple iPhones and iPads.

While working at Bell Labs in New Jersey, Morris helped design a Navy computer that stalked enemy submarines by analyzing data from sonar systems. It was the highest-capacity computer built at the time.

He also wrote a program called Sky that tracked astronomical bodies and predicted their position at a given time. The program was so accurate that it was adopted by the U.S. Naval Observatory in Washington, his family said.

In 1983, Morris testified at a House committee hearing on the nascent phenomenon of computer viruses.

Morris said that the culprits behind the metastatic content were more often than not irresponsible youngsters.

“The notion that we are raising a generation of children so technically sophisticated that they can outwit the best efforts of the security specialists of America’s largest corporations and the military is utter nonsense,” Morris said.

He had apparently underestimated his son, Robert T. Morris.

Five years later, the younger Morris infected 6,000 computers connected to the Defense Department’s Internet with a program that paralyzed the machines’ activity.

Morris called his son’s exploit “the work of a bored graduate student.”

The case was investigated by the FBI, and the younger Morris was convicted under a federal computer crime law. He was sentenced to probation and fined $10,000. He is now a computer science professor at the Massachusetts Institute of Technology.

Morris, who taught his son much of his computer knowledge, said he was unaware of his son’s prolonged interest in programming.

“I had a feeling this kind of thing would come to an end the day he found out about girls,” Morris said in 1988. “Girls are more of a challenge.”

Robert Morris was born July 25, 1932, in Boston. He was a mathematics graduate of Harvard University, where he also received a master’s degree in applied math in 1960.

He then joined Bell Labs as a summer employee and stayed until he was recruited to the National Security Agency. He retired in 1994 and moved to Etna, N.H., from Arnold, Md.

In addition to his son, of Cambridge, Mass., survivors include his wife of 49 years, Anne Farlow Morris of Etna; two other children, Meredith Morris of Washington and Benjamin Morris of Ironia, N.J.; a brother; and two grandchildren.

At the NSA, Morris was considered a master cryptographer.

He liked to present colleagues and friends with the following sequence and have them guess the next number: 1, 11, 21, 1211, 111221.

The row of numbers is known as a look-and-say sequence. If the last number is read aloud as three “ones,” two “twos,” and one “one,” then the next number is 312211.