Пример #1
0
    def get_public_key_breakdown(cls, key):
        """Returns the breakdown of the given public key.

        :param str key: PEM encoded RSA public key.

        :return: Tuple of binary key, modulus and exponent.
        :rtype: (str, str, str)
        """
        # Strip the text part
        if not (key.startswith(cls.PUB_KEY_BEGIN)
                and key.endswith(cls.PUB_KEY_END)):
            raise RuntimeError('Given key is not Public key')
        key = key[len(cls.PUB_KEY_BEGIN):-len(cls.PUB_KEY_END)]

        # Convert to der format
        key_bin = base64.b64decode(key)
        key = nice_binary_to_string(key_bin, sep=' ',
                                    length=len(key_bin)).split(' ')

        # Parse the key
        key_objs = asn1.parse(key)

        # Get the modulus & exponent
        modulus = ' '.join(key_objs[0].value[2].value[0].value)
        exponent = ' '.join(key_objs[0].value[2].value[1].value)

        # return the keys in binary
        return (key_bin, nice_string_to_binary(modulus, sep=' '),
                nice_string_to_binary(exponent, sep=' '))
Пример #2
0
    def get_public_key_from_breakdown(cls, modulus, exponent):
        """Returns the public key created using the modulus and exponent.

        :param str modulus: Modulus string.
        :param str exponent: Exponent string.

        :return: PEM encoded RSA public key.
        :rtype: str
        """
        def con_bin_2_list(v):
            return nice_binary_to_string(v, sep=':', length=len(v)).split(':')

        # Convert to lists
        modulus = con_bin_2_list(modulus)
        exponent = con_bin_2_list(exponent)
        for i, val in enumerate(exponent):
            if int(val, 16) != 0:
                exponent = exponent[i:]
                break

        # Create the modulus, exponent objects
        modulus_obj, exponent_obj = asn1.AsnObject(), asn1.AsnObject()
        modulus_obj.set_data(modulus_obj.TYPE_INT, modulus)
        exponent_obj.set_data(exponent_obj.TYPE_INT, exponent)

        # Create the higher level objects
        tmp2 = asn1.AsnObject()
        tmp2.set_data(modulus_obj.TYPE_SEQ, [modulus_obj, exponent_obj])
        tmp1 = asn1.AsnObject()
        tmp1.set_data(modulus_obj.TYPE_BIT,
                      nice_string_to_list(tmp2.pack()),
                      use_eoc=True)
        tmp01 = asn1.AsnObject()
        tmp01.set_data(modulus_obj.TYPE_NULL, [])
        tmp00 = asn1.AsnObject()
        tmp00.set_data(modulus_obj.TYPE_OID,
                       ['2a', '86', '48', '86', 'f7', '0d', '01', '01', '01'])
        tmp0 = asn1.AsnObject()
        tmp0.set_data(modulus_obj.TYPE_SEQ, [tmp00, tmp01])
        tmp = asn1.AsnObject()
        tmp.set_data(modulus_obj.TYPE_SEQ, [tmp0, tmp1, tmp2])

        # Create the key
        key_list = nice_string_to_list(tmp.pack())
        key_bin = nice_string_to_binary(':'.join(key_list), sep=':')
        return (cls.PUB_KEY_BEGIN +
                nice_string_to_multiline(base64.b64encode(key_bin), 64) +
                cls.PUB_KEY_END)