0

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.

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 used the base64.b64decode method and it gives me the following 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

Why am I get this TypeError: Incorrect padding error, and how cn I fix it?

grizzthedj
  • 7,131
  • 16
  • 42
  • 62
  • @jaysh: You've updated the question so that now you're getting a valid response from the key server but this still fails because the response is an HTML document and you are blindly attempting to decode that as though it is base64 encoded data. You MUST extract the key from the HTML document and then decode that - although I have no idea what you plan to do with the decoded key??? – mhawke Sep 14 '09 at 06:47

1 Answers1

0

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+DPJsy46WmlscKHRIEdIkTYhp
uAIMim0q5HWymEqqAfBLwJTOY9sR+nelh0NKepcCqwCgvenJ2R5UgmAh+sOhIBrh3OahZEED
/2sRGHi4xRWKePFpttXfb2hry2/jURPae/wYfuI6Xw3k5EO593veGS7Zyjnt+7mVY1N5V/ey
rfXaS3R6GsByG/eRVzRJGU2DSQvmF+q2NC6v2s4KSzr5CVKpn586SGUSg/aKvXY3EIrpvAGP
rHum1wt6P9m9kr/4X8SdVhj7Jti6A/0TA8C2KYhOn/hSYAMTmhisHan3g2Cm6yNzKeTiq6/0
ooG/ffcY81zC6+Kw236VGy2bLrMLkboXPuecvaRfz14gJA9SGyInIGQcd78BrX8KZDUpF1Ek
KxQqL97YRMQevYV89uQADKT1rDBJPNZ+o9f59WT04tClphk/quvMMuSVILQaamF5c2ggPGph
eXNocmVlQGdtYWlsLmNvbT6IZgQTEQIAJgUCSnPtUgIbAwUJAAFRgAYLCQgHAwIEFQIIAwQW
AgMBAh4BAheAAAoJEFjpOQ2vjFvzS0wAn3vf1A8npIY/DMIFFw0/eGf0FNekAKCBJnub9GVu
9OUY0nISQf7uZZVyI7kBDQRKc+1SEAQAm7Pink6S5+kfHeUoJVldb+VAlHdf7BdvKjVeiKAb
dFUa6vR9az+wn8V5asNy/npEAYnHG2nVFpR8DTlN0eO35p78qXkuWkkpNocLIB3bFwkOCbff
P3yaCZp27Vq+9182bAR2Ah10T1KShjWTS/wfRpSVECYUGUMSh4bJTnbDA2MAAwUEAIcRhF9N
OxAsOezkiZBm+tG4BgT0+uWchY7fItJdEqrdrROuCFqWkJLY2uTbhtZ5RMceFAW3s+IYDHLL
PwM1O+ZojhvAkGwLyC4F+6RCE62mscvDJQsdwS4L25CaG2Aw97HhY7+bG00TWqGLb9JibKie
X1Lk+W8Sde/4UK3Q8tpbiE8EGBECAA8FAkpz7VICGwwFCQABUYAACgkQWOk5Da+MW/MAAgCg
tfUKLOsrFjmyFu7biv7ZwVfejaMAn1QXEJw6hpvte60WZrL0CpS60A6Q
=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&search=0x58E9390DAF8C5BF3.

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

Community
  • 1
  • 1
mhawke
  • 84,695
  • 9
  • 117
  • 138