Two men who were secretly filmed mixing the type of explosive used in the 2005 London transit bombing were convicted Tuesday of preparing a terrorist attack.

COPENHAGEN, Denmark — Two men who were secretly filmed mixing the type of explosive used in the 2005 London transit bombing were convicted Tuesday of preparing a terrorist attack.

Hammad Khuershid, a Danish citizen of Pakistani origin, and Abdoulghani Tokhi, an Afghan, were sentenced to 12 and seven years in prison, respectively.

They were arrested in an anti-terror operation last year after Danish agents filmed them conducting a small test blast with triacetone triperoxide, which was used by the suicide bombers who killed 52 London commuters.

Prosecutors said it was not clear whether Khuershid and Tokhi were preparing an attack abroad or in Denmark, which has repeatedly been threatened by Islamic extremists over newspaper cartoons of the Prophet Muhammad.

Investigators found handwritten bomb-making manuals in the men’s homes that prosecutors said Khuershid had copied at the pro-Taliban Red Mosque in Islamabad.

They said he had ties to an al-Qaida operative and also had spent time in the Pakistani region of Waziristan, a militant stronghold near the Afghan border. Khuershid has admitted he was in Waziristan, but denies receiving any military training.

Khuershid and Tokhi, who were arrested in the Copenhagen area in September 2007, had pleaded innocent and said the explosive was to be used for fireworks. They sat motionless as the City Court in Glostrup, on the outskirts of Copenhagen, handed down the guilty verdict.

The court said during sentencing that Tokhi, who has a Danish residence permit, would be expelled after serving his sentence.

Defense lawyers for both suspects said they would appeal the ruling.

Denmark’s PET security police put the men under surveillance after receiving a tip from a foreign intelligence agency. PET agents monitored their phone and computer communication and installed video cameras in Khuershid’s home.

One surveillance video, played in court, showed the men mixing and testing a very small amount of triacetone triperoxide, or TATP.

PET chief Jakob Scharf said the ruling confirmed that the arrests had prevented a terrorist attack. He called it “a serious case which illustrates that there are people and circles in Denmark that have the will and the ability to commit acts of terror.”

Danish police say they have thwarted a series of terrorist attacks in recent years.

In February, police arrested two Tunisian men suspected of plotting to murder Danish cartoonist Kurt Westergaard, who drew one of the Muhammad caricatures that sparked violent protests in Muslim countries in 2006.

Neither suspect was prosecuted. One of them was deported and the other was released Monday after an immigration board rejected PET’s efforts to expel him from Denmark.