North Korea's foreign ministry said in a statement on Thursday, using the country's official name, the Democratic People's Republic of Korea (DPRK): "The DPRK will in the future conduct a nuclear test in a condition where safety is firmly guaranteed."
It is the first time that North Korea has announced its intent to conduct a nuclear test amid recent concerns that it may be preparing for such an action.
The statement gave no precise date as to when a test might be carried out.
The ministry statement, carried by the North Korea's official Korean Central News Agency, said: "The US extreme threat of a nuclear war and sanctions and pressure compel the DPRK to conduct a nuclear test, an essential process for bolstering nuclear deterrent, as a self-defence measure in response.''
Pyongyang's announcement comes as multilateral talks on its nuclear programme remain stalled for almost a year.
DPRK has boycotted the six-nation talks in protest over US financial restrictions imposed for its alleged illegal activity, including money-laundering and counterfeiting.
The ministry statement also said that its ultimate goal was "to settle hostile relations between the DPRK and the US and to remove the very source of all nuclear threats from the Korean Peninsula and its vicinity," accusing the US of posing a nuclear threat in the region.
North Korea's nuclear weapons were to protect its government from "the US threat of aggression" and "to safeguard peace and stability on the Korean peninsula," the statement said.
Pyongyang accuses Washington of trying to topple its government through a crackdown on its finances.
It wants this ended before it will return to international talks to end its nuclear weapons programme.
"The US extreme threat of a nuclear war and sanctions and pressure compel the DPRK to conduct a nuclear test, an essential process for bolstering nuclear deterrent, as a self-defence measure in response"
North Korean foreign ministry statement
The United States refuses to end the crackdown, which analysts say is causing Pyongyang's leadership real difficulties, or to hold direct talks with North Korea outside the six-country nuclear negotiations.
The talks among the two Koreas, China, Japan, Russia and the United States have been stalled since November.
The United States has voiced its alarm over North Korea's nuclear plan.
A nuclear weapons test by North Korea "would pose an unacceptable threat to peace and stability in Asia and the world", the US state department spokesman said in Cairo.
"A provocative action of this nature would only further isolate the North Korean regime and deny the people of the north the benefits they so rightly deserve," Sean McCormack said.
South Korea also expressed "deep regret and concern" and ordered a security alert.
"This poses a grave threat to peace on the Korean peninsula and it will have a decisively negative impact on inter-Korean relations," said Yang Chang-Seok, the unification ministry spokesman.
Britain said it would view any North Korean nuclear missile test as "highly provocative".
"A missile test launch will be viewed by the UK and the rest of the international community as a highly provocative act with serious consequences for the DPRK (North Korea)," a foregin ministry spokesman said.
"It would be a threat to peace. We would never be able to forgive such a move"
Japan's foreign minister
Taro Aso, Japan's foreign minister, called the North Korean idea unforgivable.
"It would be a threat to peace. We would never be able to forgive such a move," Aso told reporters. "It would gravely affect Northeast Asia, including Japan."
Aso said he did not doubt North Korea's resolve in testing a nuclear device.
"In the past, the country has done what it had said earlier. So I think it would be wrong automatically to think the country will not do this."