EL ALAMEIN, Egypt — Two attacks on Egyptian military positions in the Sinai Peninsula on Friday killed at least 31 soldiers, according to security officials and the state news media.

The first attack killed at least 28 soldiers, making it the deadliest assault on the Egyptian military in many years and the biggest defeat in its 15-month battle against Sinai-based Islamist militants that began with the military’s ouster of President Mohamed Morsi of the Muslim Brotherhood in July 2013. At least 28 others were wounded, the state news media said.

The scale of the attack underscored the difficult challenge the Egyptian government continues to face in re-establishing firm control of northern Sinai, near the border with Israel and the Gaza Strip. Egyptian officials have said repeatedly that they have largely contained the insurgency there, but the complexity of Friday’s attack, said to involve multiple vehicles and heavy weapons, suggested that the militants were growing more sophisticated.

President Abdel Fattah el-Sisi, who led the military takeover last year, convened an emergency meeting of Egypt’s top generals in response to the attack. He declared a state of emergency in parts of Sinai, including a curfew from 5 p.m. to 7 a.m.