views:

175

answers:

3

this is the code :

f = urllib.urlopen('http://pool.sks-keyservers.net:11371/pks/lookup?op=get&search= 0x58e9390daf8c5bf3') #Retrieve the public key from PKS

data = f.read()
decoded_bytes = base64.b64decode(data)
print decoded_bytes

i need to convert the key in MIME encoded form which is presently comes in (ascii armored) radix 64 format.for that i have to get this radix64 format in its binary form and also need to remove its header and checksum than coversion in MIME format but i didnt find any method which can do this conversion.

i used the base64.b64decode method and its give me error:

Traceback (most recent call last):
  File "RetEnc.py", line 12, in ?
    decoded_bytes = base64.b64decode(data)
  File "/usr/lib/python2.4/base64.py", line 76, in b64decode
    raise TypeError(msg)
TypeError: Incorrect padding

what to do i'didnt getting .can anybody suggest me something related to this......

thanks!!!!

A: 

By adding a print data statement, I see...:

'Error handling request\r\n

Error handling request

Error handling request: No keys found\r\n'

Kind of hard to make ANYthing out of THAT string, wouldn't you say...?-)

What do YOU see when you add a print data statement...?

Alex Martelli
this will definately retrieve the key when u hv the corrrect search parameter.
and problem is not in getting the key instead in conversion mechanism
A: 

For the encoding in the URL you give, see the-encoding-mechanism. It is a public PGP key encoded using Radix64 (OpenPGP's variant of Base64) -- "armored".

For a few ideas on dealing with PGP, see how-to-do-pgp-in-python-generate-keys-encrypt-decrypt.

Hopefully, the PGP module in M2Crypto will help.

gimel
A: 

For a start, when you do use a valid search value ("jay" returns an error stating "too many values"), you will receive an HTML page from which you need to extract the actual key. Trying a search value of "jaysh" I get the following response:

>>> print urllib.urlopen('http://pool.sks-keyservers.net:11371/pks/lookup?op=get&search=jaysh').read()
<html><head><title>Public Key Server -- Get ``jaysh ''</title></head>
<body><h1>Public Key Server -- Get ``jaysh ''</h1>
<pre>
-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: SKS 1.1.1

mQGiBEpz7VIRBADAt9YpYfYHJeGA6d+G261FHW1uA0YXltCWa7TL6JnIsuxvh9vImUoyMJd6
1xEW4TuROTxGcMMiDemQq6HfV9tLi7ptVBLf/8nUEFoGhxS+DPJsy46WmlscKH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tvYU
-----END PGP PUBLIC KEY BLOCK-----
</pre>
</body></html>

So you need to look only at the key which is wrapped by the <pre> HTML tags.

By the way, there are other issues that you will need to contend with such as multiple keys being returned because you are searching by "name", when you should be searching by keyID. For example, keyID 0x58E9390DAF8C5BF3 will return the public key for jaysh and only jaysh and the corresponsing URL is http://pool.sks-keyservers.net:11371/pks/lookup?op=get&amp;search=0x58E9390DAF8C5BF3.

This was mostly covered in my earlier answer to this question which I presume you also asked.

mhawke