Jailing suspected terrorists without trial is an "appropriate response" to the post-11 September terrorist threat, the House of Lords has heard.
Lord Goldsmith is representing the government at the appeal hearing
Attorney General Lord Goldsmith QC, representing the Home Office, said the measures are justifed and protect the rights of suspects and UK residents.
Nine Law Lords are hearing a challenge to the government's right to detain foreign terror suspects without trial.
Nine men held without charge argue their human rights have been infringed.
Lord Goldsmith said the decision to abandon the right to a fair trial was "not a step taken lightly".
He told the House of Lords: "The government believes it was a legitimate and appropriate response to protect the human rights of the suspected international terrorists by not exporting them to death and torture, and to protect the human rights of citizens here."
He added: "We took the view that, if these people cannot be deported, they should not be allowed to roam free on the streets and so they had to be detained."
The House of Lords - which has 12 full-time Law Lords, or senior judges - is the final court of appeal in the UK.
Nine Law Lords, rather than the usual five, are sitting because of the constitutional importance of the legal challenge.
The case focuses on nine men, some of whom have been held for up to three years.
Their legal team argues it is wrong for them to be held without charge indefinitely and wrong to single out foreign nationals.
The government believes the measure is justified because of the scale of the terrorist threat after 11 September.
"These events required the government of this country to assess the threat to
the UK and what measures it was necessary to take to protect its citizens from
terrorist attacks of unparalleled magnitude," said Lord Goldsmith.
He argued the 11 September attacks are "a new sort of terrorism" which represent "an emergency threatening the life of the nation".
The Anti-Terrorism, Crime and Security Act allows the imprisonment without trial of foreign nationals certified by the home secretary as a threat to national security, but who cannot be sent home because they might face death or torture.
The government opted out of Article 5 of the European Convention on Human Rights (ECHR), the right to liberty and security of person, in order to introduce the new anti-terrorism laws.
A legal challenge was mounted but in August, three Court of Appeal judges decided by a two-to-one majority that the government was legally entitled to hold the men.
The nine suspects are challenging that decision.
Eight of the appellants are currently in custody. The ninth, an Algerian man known as "D", was released in September.