Created
November 23, 2013 03:35
-
-
Save jchysk/7610440 to your computer and use it in GitHub Desktop.
RSA key format conversions
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
def pub_to_e_n(key): | |
""" | |
Converts a .pub public key to an exponent and n value | |
:param key: String. Public key | |
:return: e as Int | |
:return: n as Long | |
""" | |
import base64 | |
import struct | |
keydata = base64.b64decode(key.split(None)[1]) | |
parts = [] | |
while keydata: | |
# read the length of the data | |
dlen = struct.unpack('>I', keydata[:4])[0] | |
# read in <length> bytes | |
data, keydata = keydata[4:dlen+4], keydata[4+dlen:] | |
parts.append(data) | |
e_val = eval('0x' + ''.join(['%02X' % struct.unpack('B', x)[0] for x in | |
parts[1]])) | |
n_val = eval('0x' + ''.join(['%02X' % struct.unpack('B', x)[0] for x in | |
parts[2]])) | |
return e_val, n_val | |
def der_to_e_n(key): | |
""" | |
Converts a DER public key to an exponent and n value | |
:param key: String. Public key | |
:return: e as Int | |
:return: n as Long | |
""" | |
from Crypto.Util import asn1 | |
from base64 import b64decode | |
key = key.replace("-----BEGIN RSA PUBLIC KEY-----", "") | |
key = key.replace("-----END RSA PUBLIC KEY-----", "") | |
key = b64decode(key) | |
seq = asn1.DerSequence() | |
seq.decode(key) | |
return int(seq[1]), seq[0] | |
def convert_pub_to_der(key): | |
""" | |
Converts a .pub public key to DER format | |
:param key: String. Public key | |
:return: String. The public key in DER format | |
""" | |
import base64 | |
from pyasn1.type import univ | |
from pyasn1.codec.der import encoder as der_encoder | |
e_val, n_val = pub_to_e_n(key) | |
pkcs1_seq = univ.Sequence() | |
pkcs1_seq.setComponentByPosition(0, univ.Integer(n_val)) | |
pkcs1_seq.setComponentByPosition(1, univ.Integer(e_val)) | |
pub_key = '-----BEGIN RSA PUBLIC KEY-----' | |
pub_key += base64.encodestring(der_encoder.encode(pkcs1_seq)) | |
pub_key += '-----END RSA PUBLIC KEY-----' | |
return pub_key | |
def convert_pub_to_pem(key): | |
""" | |
Converts a .pub public key to PEM format | |
:param key: String. Public key | |
:return: String. The public key in PEM format | |
""" | |
return convert_e_n_to_pem(pub_to_e_n(key)) | |
def convert_der_to_pem(key): | |
""" | |
Converts a DER public key to PEM format | |
:param key: String. Public key | |
:return: String. The public key in PEM format | |
""" | |
return convert_e_n_to_pem(der_to_e_n(key)) | |
def convert_e_n_to_pem(vals): | |
e_val, n_val = vals | |
from M2Crypto import RSA, BIO, m2 | |
bne = m2.hex_to_bn(str(hex(e_val)[2:])) | |
bnn = m2.hex_to_bn(str(hex(n_val))[:-1][2:]) | |
e = m2.bn_to_mpi(bne) | |
n = m2.bn_to_mpi(bnn) | |
rsa = RSA.new_pub_key((e, n)) | |
bio = BIO.MemoryBuffer() | |
rsa.save_key_bio(bio) | |
return bio.getvalue() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment