The masked gunmen who killed 16 soldiers at an Egyptian border post in the Sinai peninsula, stole vehicles and blasted into Israel last week could not have foreseen the effects of their venture. They upped the ante so high, they might have spoiled Sinai for other extremists.

By killing soldiers rather than tourists and by striking when the men were breaking their Ramadan fast, the terrorists outraged the Egyptian public. By crossing the border, they rattled the Israelis. And because there were suspicions that some of the gunmen came from the Gaza Strip, they put Hamas, which controls Gaza, in a compromised position. In short, they gave Egypt, Israel and Hamas a common cause: to pacify the largely lawless Sinai.

It’s an important goal. Illegal tunnels from Gaza, dug to evade border security, have fueled criminality in Sinai and enabled terrorists to slip inside -- a task made easier by the paucity of police, many of whom fled the area 18 months ago when President Hosni Mubarak’s regime fell. Attacks on Israel from Sinai have tested the peace between Israel and Egypt, a keystone of U.S. diplomacy in the Mideast.

Egyptian President Mohamed Mursi was so riled by the Ramadan attack that he ordered fierce counterstrikes on suspected terrorist sites. He then forced out senior security officials including the intelligence chief, the military chief of staff and, most surprising, the defense minister, Mohamed Hussein Tantawi, head of the military council that took over after Mubarak’s ouster. Israel had told Egypt about an imminent attack. Apparently, the warning went unheeded.

Blasting terrorists with helicopter gunships sends a message of intent; it’s unlikely to have a lasting, beneficial effect, though. The mass arrests, beatings and torture that followed terror attacks on Red Sea resorts in 2004 and 2005 alienated locals and are widely thought to have increased support for Sinai jihadists. Calming Sinai today will require quiet security improvements, including enhanced intelligence sharing, as well as attention to the economic needs of both the Gaza Strip and Sinai.

To start, the Egyptians should restore the traditionally excellent state of their intelligence cooperation with Israel. They should also enhance their information-sharing with Hamas, which finds Sinai’s al-Qaeda style militants overly extreme. Mursi’s Egypt is the most powerful ally Hamas has ever had in the Arab world; Hamas cannot afford to offend it.

Shutting down the tunnels connecting Sinai to Gaza is crucial to containing the mayhem. Yet there are so many, the Egyptians can’t close them all for good unless the business model for the tunnels collapses. The tunnels were originally constructed to evade strict Israeli limits on what could enter Gaza. Although the bans have largely been lifted, the tunnels enable Gazans to import goods cheaply from Egypt, instead of buying legally via Israel. Plus, it remains illegal for Gazans to export most anything.

Israel could obviate the need for the tunnels by lifting its remaining import restrictions (mostly on construction materials) and by allowing more Gazan exports to flow through Israel. Egypt and Israel should agree to regularize the border crossing from Gaza into Egypt, allowing goods and not just people to go in and out. Israel would have to trust the Egyptians to monitor materials carefully, but as it is, the Israelis have no control over what goes across in the underground traffic.

At the same time, the Egyptians must invest in the Sinai economy, the least developed part of the country. Egypt’s public coffers are low, but small job-creation efforts, such as growing tomatoes and processing olives, might counter jihadist recruitment. The private sector has a role as well: For example, Sinai dwellers resent that jobs at the area’s Red Sea resorts have largely gone to outsiders from the Nile valley rather than locals.

Israel, Egypt and Hamas may not like or trust one another, but putting an end to the Sinai jihadists is something they can agree on.

Read more opinion online from Bloomberg View. Subscribe to receive a daily e-mail highlighting new View editorials, columns and op-ed articles.

Today’s highlights: In a signed editorial, Michael R. Bloomberg calls on the presidential candidates to support immigration policies that help the economy.

Also, Caroline Baum on how the Romney-Ryan ticket deflects the inevitable attacks; Margaret Carlson on whether Paul Ryan is the new Dick Cheney; Virginia Postrel on Ross Perot’s influence on the presidential race; Ramesh Ponnuru on why the gender pay gap isn’t a problem; Adam Kirsch on idealism and “All the King’s Men.”

To contact the Bloomberg View editorial board: view@bloomberg.net.