Created
July 28, 2015 19:05
-
-
Save perks/147a4ced1b8bacccc750 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from M2Crypto import BIO, RSA | |
import base64 | |
def der_length(length): | |
"""DER encoding of a length""" | |
if length < 128: | |
return chr(length) | |
prefix = 0x80 | |
result = '' | |
while length > 0: | |
result = chr(length & 0xff) + result | |
length >>= 8 | |
prefix += 1 | |
return chr(prefix) + result | |
pubkey="""-----BEGIN RSA PUBLIC KEY----- | |
MIIBCgKCAQEApwotnfHT9RAmxnuaGEMdI3lYPYE4aaqSD9v4KbTh1E7Le3GNJQb7 | |
wCpmDe8+n8S5Kp/gBEpWiYuvsVA/T4KseoX7NMcacP+DJMwjmNd9U58USn2vLz0Z | |
TMtXpc/FUhW5PZdgCiuNzw6IFgGn9ZCCv85jjUIW3KD8fUNdrUfVSv4olDoL9NkR | |
dTRg3Os/znC6l0gv/mqnLaqj2bJ/tx47kUmj6Oq13JuEq34T+DVmsUCFVundQnRp | |
c/vVEqQot7Rvj9UmSvTi4WKt/qxiAnyZf3gXOdrXvxfVTGzD5I/Xg+By+a4C2JwB | |
A5RGvZP3fyfhkCnnhFDpfws5lc20FA6ryQIDAQAB | |
-----END RSA PUBLIC KEY----- | |
""" | |
pk = pubkey.split('\n') | |
pk = '\0' + base64.decodestring("".join(pk[1:-2])) | |
pk = '\x30\x0d\x06\x09\x2a\x86\x48\x86\xf7\x0d\x01\x01\x01\x05\x00\x03' + \ | |
der_length(len(pk)) + pk | |
pk = '\x30' + der_length(len(pk)) + pk | |
pk = '-----BEGIN PUBLIC KEY-----\n' + base64.encodestring(pk) + '-----END PUBLIC KEY-----' | |
bio = BIO.MemoryBuffer(pk) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment