While upgrading Debian, you might experience difficulties like

# apt-get update

Get:1 http://ftp.dk.debian.org lenny Release.gpg [189B]
Ign http://ftp.dk.debian.org lenny/main Translation-en_DK
Hit http://ftp.dk.debian.org lenny Release
Hit http://ftp.dk.debian.org lenny/main Packages/DiffIndex
Hit http://ftp.dk.debian.org lenny/main Sources/DiffIndex
Fetched 1B in 4s (0B/s)

Reading package lists… Done

W: GPG error: http://ftp.dk.debian.org lenny Release: The following signatures couldn’t be verified because the public key is not available: NO_PUBKEY D5642BC86823D007
W: You may want to run apt-get update to correct these problems


This can be caused by a dist-upgrade or various other reasons, to keep it short, I will post

A few possible solutions to this.

#apt-key update

This will obtain the necessary keys and import them. No need to go through gpg directly.
if above doesnt do the trick, heres another solution(might fail tho, if apt wont let you update/install)

# apt-get install debian-archive-keyring

in case this doesnt do the trick neither, it might be caused by old expired keys which didnt get updated while installing debian-archive-keyring. so heres a way to take care of that..

#apt-key list

which will list currently active and expired keys.
then eg. do

#apt-key del 1DB114E0
#apt-key del 4F368D5D
#apt-key del 2D230C5F

or simply

#apt-key del

to erase all expired keys marked for deletion.

Then finally.

#apt-get remove –purge debian-archive-keyring
#apt-get install debian-archive-keyring

Now, if youre just plain unlucky, something fucks up, or you just want to try another way to solve it.
you can do it with gpg manually.

the procedure is.

#gpg –keyserver <keyserver> –recv-keys <pubkeynumber>
#gpg –armor –export <keynumber> | apt-key add -

eg.

#gpg –keyserver pgp.mit.edu –recv-keys D5642BC86823D007 # this being the pub_key you got earlier.

which should result in something like

gpg: requesting key 8722E71E from hkp server pgp.mit.edu
gpg: key 8722E71E: public key “secure-testing Archive key 2005-7 <katie@secure-testing.debian.net>” imported

gpg: no ultimately trusted keys found
gpg: Total number processed: 1
gpg: imported: 1

Then type something like.

#gpg –armor –export 8722E71E | apt-key add -

which would output something like.

gpg: no ultimately trusted keys found
OK

At this point, the gpg key is recorded in your local database, and you shouldn’t see the gpg error for that particular repository.

Some additional key servers:

keyring.debian.org
pgp.mit.edu
pgpkeys.pgp.net

wwkeys.uk.pgp.net
wwwkeys.pgp.net